1
我使用oauth-signpost作爲我的OAuth庫。當我將用戶引導至授權頁面時,用戶登錄並授權我的應用程序。發生這種情況時,應用程序返回焦點並啓動onCreate()方法,而不是onResume()方法。來自OAuth的回調未調用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的過程繼續下去嗎?
這不應該被接受爲答案嗎? –