6

私は研究用の Android アプリケーションに取り組んでおり、開発プロセスの一部でもある Web サービスからユーザー データにアクセスするために OAuth (道しるべライブラリ) を使用しています。OAuth の一般的な手順を実行することができ、Uri (アプリへのコールバック用) を使用し、デバイス ブラウザーを呼び出す手順に進むことができ、アプリを検証することを選択し、次の手順はサポートされています。ブラウザをアプリケーションにリダイレクトします....

代わりに、「開く権限がありません:

appSchema://appName?authorizationSensitiveInfo..." '?' の後の付属物 サービスからの oauth_token と oauth_verifier です (リダイレクトが「正しい」までのすべてのステップを想定できます)。

パーツ内に問題がある可能性がありますappSchema://appName。私の理解では、これは、電話のブラウザーを使用してアプリケーションを見つけ、 onResume() メソッドを呼び出すように Uri に指示するリダイレクト URL です。の値はappSchema://appNameどこから来るのですか (マニフェストで定義されていますか? もしそうならどこから来ますか?)。

なぜ許可の問題なのですか?アプリにアクセスするには、Uri のアクセス許可を設定する必要がありますか? 私は迷っています...コードスニペットが必要な場合は、返信してください.コードは含まれていません.それが物事を理解しやすくするなら、コード化してください。ここで本当に私の頭を打ちます...

すばらしい回答に応えて、履歴書での私の扱い方は次のとおりです

protected void onResume() {
    super.onResume();       
    Uri uri = this.getIntent().getData();
    if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
        Log.d("StepGreenM", uri.toString());
        String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
        Log.d("StepGreenM", verifier);
        try {

            provider.retrieveAccessToken(consumer, verifier);
            TOKEN = consumer.getToken();
            REQUEST_SECRET = consumer.getTokenSecret();

            Log.d("StepGreenM", TOKEN);
            Log.d("StepGreenM", REQUEST_SECRET);

        } catch (OAuthMessageSignerException e) {
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            e.printStackTrace();
        }
    }

    uri = getIntent().getData();
    if (uri != null && CALLBACK_URI.getScheme().equals(uri.getScheme())) {
        String token = settings.getString(HomeScreen.REQUEST_TOKEN, null);
        String secret = settings.getString(HomeScreen.REQUEST_SECRET, null);
        Intent i = new Intent(Intent.ACTION_VIEW); // Intent to go to the action view

        try {
            if(!(token == null || secret == null)) {
                consumer.setTokenWithSecret(token, secret);
            }
            String otoken = uri.getQueryParameter(OAuth.OAUTH_TOKEN);
            String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);

            // We send out and save the request token, but the secret is not the same as the verifier
            // Apparently, the verifier is decoded to get the secret, which is then compared - crafty
            // This is a sanity check which should never fail - hence the assertion
            Assert.assertEquals(otoken, consumer.getToken());

            // This is the moment of truth - we could throw here
            provider.retrieveAccessToken(consumer, verifier);
            // Now we can retrieve the goodies
            token = consumer.getToken();
            secret = consumer.getTokenSecret();
            //Save it to a settings file
            HomeScreen.saveAuthInformation(settings, token, secret);
            // Clear the request stuff, now that we have the real thing
            HomeScreen.saveRequestInformation(settings, null, null);
            i.putExtra(USER_TOKEN, token);
            i.putExtra(CONSUMER_SECRET, secret);

            //GO TO APPLICATION

        } catch (OAuthMessageSignerException e) {
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            e.printStackTrace();
        } finally {
            startActivity(i); // we either authenticated and have the extras or not, but are going to the action view
            this.setContentView(R.layout.indivaction);
            finish();
        }
    }
}

これが本当にバラバラになる理由はわかりません...しかし、私が言ったように、このメソッドが呼び出されると強制的に閉じます。httpSniffer を使用してサーバーとの間のメッセージをチェックしているため、リダイレクトを通過することはわかっています...

4

1 に答える 1

10

コールバック URI が適切に機能するためには、使用するアクティビティのマニフェストに次のようなインテント フィルターを追加する必要があります。

   <intent-filter>          
    <action android:name="android.intent.action.VIEW"/>     
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:scheme="appSchema" android:host="appName"/> 
   </intent-filter>

ここで、アクティビティが singleInstance/singleTask を使用している場合は、次のようなものを使用する必要があります。

@Override
public void onNewIntent(Intent intent) {

    super.onNewIntent(intent);
    Uri uri = intent.getData();
    String oauthToken = uri.getQueryParameter("oauth_token");
    String oauthVerifier = uri.getQueryParameter("oauth_verifier");

    //...do what you need with the parameters
}

singleTask または singleInstance を使用していない場合は、次のことができます。

@Override
public void onResume() {

    super.onResume();
    Intent intent = getIntent();
    Uri uri = intent.getData();
    String oauthToken = uri.getQueryParameter("oauth_token");
    String oauthVerifier = uri.getQueryParameter("oauth_verifier");

    //...do what you need with the parameters
}

私はこれがうまくいくと信じています。

また、私が間違っていなければ、提供するコールバック URL には ? を含める必要があるため、「appSchema://appName?」

于 2011-04-05T00:42:58.603 に答える