8

AndroidアプリにOAuthを設定しています。それをテストするために、私は次のことを行いました:signpost-core-1.2.1.1.jarとsignpost-commonshttp4-1.2.1.1.jarをプロジェクトに追加し、変数「CommonsHttpOAuthConsumerconsumer」と「CommonsHttpOAuthProviderprovider」を追加しました。ボタンがクリックされた:

consumer = new CommonsHttpOAuthConsumer("xxx", "yyy");
provider = new CommonsHttpOAuthProvider("https://api.twitter.com/oauth/request_token", 
                    "https://api.twitter.com/oauth/access_token", 
                    "https://api.twitter.com/oauth/authorize");

oauthUrl = provider.retrieveRequestToken(consumer, "myapp://twitterOauth");
persistOAuthData();
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(oauthUrl)));

persistOAuthData()は次のことを行います。

protected void persistOAuthData()
{
    try
    {
        FileOutputStream providerFOS = this.openFileOutput("provider.dat", MODE_PRIVATE);
        ObjectOutputStream providerOOS = new ObjectOutputStream(providerFOS);
        providerOOS.writeObject(this.provider);
        providerOOS.close();

        FileOutputStream consumerFOS = this.openFileOutput("consumer.dat", MODE_PRIVATE);
        ObjectOutputStream consumerOOS = new ObjectOutputStream(consumerFOS);
        consumerOOS.writeObject(this.consumer);
        consumerOOS.close();
    }
    catch (Exception e) { }
}

したがって、ここで説明するように、コンシューマーとプロバイダーはブラウザーを開く前に保存されます。

onResume()メソッドで、プロバイダーとコンシューマーのデータをロードし、次のことを行います。

    Uri uri = this.getIntent().getData();
    if (uri != null && uri.getScheme().equals("myapp") && uri.getHost().equals("twitterOauth"))
    {
        verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
        if (!verifier.equals(""))
        {
            loadOauthData();
            try
            {
                provider.retrieveAccessToken(consumer, verifier);
            }
            catch (OAuthMessageSignerException e) {
                e.printStackTrace();
            } catch (OAuthNotAuthorizedException e) {
                e.printStackTrace();
            } catch (OAuthExpectationFailedException e) {
                e.printStackTrace();
            } catch (OAuthCommunicationException e) {
                e.printStackTrace();
            }            
        }
    }

したがって、機能するものは次のとおりです。1)requestTokenとrequestSecretを取得します。2)oauthUrlを取得します。3)アプリを認証するためにブラウザページに移動します。4)アプリにリダイレクトされます。5)ベリファイアを取得します。ただし、retrieveAccessToken(consumer、verifier)の呼び出しは、「サービスプロバイダーとの通信に失敗しました:null」というOAuthCommunicationExceptionで失敗します。

誰かが理由が何であるか知っていますか?requestTokenの取得に問題があるように見える人もいますが、それは問題なく機能します。私のアプリに、マルチパートアップロードに必要なapache-mime4j-0.6.jarとhttpmime-4.0.1.jarも含まれているのは問題ではないかと思います。

4

2 に答える 2

13

さて、私はそれを理解しました。多分これは他の人に役立ちます:

まず、コンシューマーオブジェクトとプロバイダーオブジェクト全体を保存する必要はありません。あなたがする必要があるのはrequestTokenとrequestSecretを保存することです。幸いなことに、これらは文字列であるため、ディスクなどに書き込む必要はありません。それらをsharedPreferencesなどに保存するだけです。

これで、ブラウザーによってリダイレクトされ、onResume()メソッドが呼び出されたら、次のようにします。

//The consumer object was lost because the browser got into foreground, need to instantiate it again with your apps token and secret.
consumer = new CommonsHttpOAuthConsumer("xxx", "yyy"); 

//Set the requestToken and the tokenSecret that you got earlier by calling retrieveRequestToken.
consumer.setTokenWithSecret(requestToken, tokenSecret);

//The provider object is lost, too, so instantiate it again.
provider = new CommonsHttpOAuthProvider("https://api.twitter.com/oauth/request_token", 
                                "https://api.twitter.com/oauth/access_token", 
                                "https://api.twitter.com/oauth/authorize");     
//Now that's really important. Because you don't perform the retrieveRequestToken method at this moment, the OAuth method is not detected automatically (there is no communication with Twitter). So, the default is 1.0 which is wrong because the initial request was performed with 1.0a.
provider.setOAuth10a(true);

provider.retrieveAccessToken(consumer, verifier);

これで、getToken()とgetTokenSecret()を使用してトークンとシークレットを受け取ることができます。

于 2010-07-15T13:19:50.247 に答える
0

こんにちはマヌエル私はあなたがOAuthocalypseでも避けているのを見ます!これは、ソリューションのように、sharedPreferencesを使用してrequestTokenとrequestSecretを保存するTwitter用のOAuthを実装する良い例です。 http://github.com/brione/Brion-Learns-OAuth by Brion Emde

こちらがビデオです

これが他の開発者に役立つことを願っています=)

于 2010-07-15T18:11:14.703 に答える