【问题标题】:Provider is null when doing oauth after turning back from web page从网页返回后执行 oauth 时提供者为空
【发布时间】:2011-03-08 18:25:32
【问题描述】:

我在android中使用signpost-oauth做oauth,网页登录后返回Activity,provider有时为null。

public class LoginActivity extends Activity implements View.OnClickListener {

private static OAuthConsumer consumer;
private static OAuthProvider provider;

private static final Uri CALLBACK= Uri.parse("xxx://callback?");

...

@Override
protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        final Uri uri = intent.getData();
        if (uri != null && uri.toString().startsWith(CALLBACK)) {

            final String pin = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);

            try {
                //provider.setOAuth10a(true);
                provider.retrieveAccessToken(consumer, pin);
                    } catch (final Exception e) {

            }
            }
}
...

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.oAuthLoginBtn:
      new Thread(new Runnable() {
        public void run() {
          LoginActivity.consumer = new CommonsHttpOAuthConsumer(
                            apiKey, appSecret);
          LoginActivity.provider = new CommonsHttpOAuthProvider(
                            oauthUrl + Data.REQUEST_TOKEN_URL, oauthUrl
                                    + Data.ACCESS_TOKEN_URL, oauthUrl
                                    + Data.AUTHORIZE_URL);

    LoginActivity.provider.setOAuth10a(true);
           try {
                        final String authUrl = LoginActivity.provider
                                .retrieveRequestToken(LoginActivity.consumer,
                                        CALLBACK);

                        final Uri uri = Uri.parse(authUrl);
                        final Intent it = new Intent(Intent.ACTION_VIEW, uri);
                        LoginActivity.this.startActivity(it);
        } catch (final Exception e) {

           }
         }
        }).start();
        break;
    }

}

和 manifest.xml

<activity android:name=".ui.LoginActivity" android:label="@string/activity_login"
            android:launchMode="singleInstance" android:configChanges="orientation|keyboardHidden">
            <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="xxx" />
            </intent-filter>
        </activity>

我不知道为什么提供者有时为空。

【问题讨论】:

    标签: android oauth


    【解决方案1】:

    我认为您没有收到正确的 oauth_token 和 oauth_verifier。定义一个主机

    <data android:scheme="xxx" android:host="twitt" />
    

    您将收到正确的值。

    xxx://twitt?oauth_token=it23W5yOy13x6s1ox6kpaOow2aM70OKTW3WTy4h8Q4&oauth_verifier=gRuGGE4osgjTwltooi9MG4u7a4EbxAsIxvVjZqd9dA
    

    【讨论】:

    • 嗨,我用这个: xxx://callback?oauth_verifier=IMoNvawE3nA56zW8dkPl9L%2FR&oauth_token=4%2FuvcAWpzjMNcRBvLKwW4xn9GZDp2a --- ----------------------------- 有时我会测试一下它是否仍然为空,谢谢。
    • 嗨,我已经移动了这一行 Provider.setOAuth10a(true);并将 CALLBACK 定义为静态变量!,我有一个问题,你为什么不使用 onResume() 而不是 onNewIntent()?
    • 嗨,我发现它有时仍然为空。我在 manifest.xml 中使用了 launchMode="singleInstance",所以必须使用 onNewIntent() 来接收它。
    【解决方案2】:

    也许&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt; 和/或&lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt; 不见了?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-20
      • 2013-10-08
      • 2014-04-18
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多