【问题标题】:callback from OAuth not call onResume()来自 OAuth 的回调不调用 onResume()
【发布时间】:2011-11-19 08:55:40
【问题描述】:

我使用 oauth-signpost 作为我的 OAuth 库。当我将用户定向到授权页面时,用户登录并可以授权我的应用程序。发生这种情况时,应用程序将返回焦点并启动 onCreate() 方法而不是 onResume() 方法。

我的代码如下:

private static final String             CONSUMER_KEY      = "---";
private static final String             CONSUMER_SECRET   = "---";

private static String                   ACCESS_KEY        = null;
private static String                   ACCESS_SECRET     = null;

private static final String             REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token";
private static final String             ACCESS_TOKEN_URL  = "https://api.twitter.com/oauth/access_token";
private static final String             AUTH_URL          = "https://api.twitter.com/oauth/authorize";
private static final String             CALLBACK_URL      = "myApp://Tweets";

private static CommonsHttpOAuthConsumer consumer          = new CommonsHttpOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);

private static CommonsHttpOAuthProvider provider          = new CommonsHttpOAuthProvider(REQUEST_TOKEN_URL,ACCESS_TOKEN_URL,AUTH_URL);


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String authUrl = null;
    try {
        authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
    } catch(OAuthMessageSignerException e) {
        e.printStackTrace();
    } catch(OAuthNotAuthorizedException e) {
        e.printStackTrace();
    } catch(OAuthExpectationFailedException e) {
        e.printStackTrace();
    } catch(OAuthCommunicationException e) {
        e.printStackTrace();
    }
    Log.d("OAuthTwitter", "authUrl" + authUrl);

    WebView webview = new WebView(this);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setVisibility(View.VISIBLE);
    setContentView(webview);
    webview.loadUrl(authUrl);

}

    @Override
public void onResume() {
    super.onResume();
    Uri uri = this.getIntent().getData();

    if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
        Log.d("OAuthTwitter", uri.toString());
        String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
        Log.d("OAuthTwitter", verifier);
        try {

            provider.retrieveAccessToken(consumer, verifier);
            ACCESS_KEY = consumer.getToken();
            ACCESS_SECRET = consumer.getTokenSecret();

            Log.d("OAuthTwitter", ACCESS_KEY);
            Log.d("OAuthTwitter", ACCESS_SECRET);

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

Manifest.xml

    <application
    android:icon="@drawable/icon"
    android:label="@string/app_name">
    <activity
        android:name=".Tweets"
        android:label="@string/app_name">
        <intent-filter>
            <action
                android:name="android.intent.action.MAIN" />
            <category
                android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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="myApp"
                android:host="Tweets" />

        </intent-filter>

    </activity>

如何让我的应用程序调用 onResume() 以便我可以继续 OAuth 流程?

【问题讨论】:

    标签: java android twitter oauth


    【解决方案1】:

    在这种情况下,请尝试将您的活动启动模式设置为singleTask

    查看链接了解更多信息:http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      相关资源
      最近更新 更多