我正在尝试为我的 Android 应用程序找出 Twitter OAuth。我的问题是当 Twitter OAuth 浏览器打开时,我单击Authorize App
按钮,但它指向我的回调 URL,在那里暂停并且不返回我的应用程序,随后不调用任何一个onNewIntent
/onResume
方法(我尝试使用这两个这两种方法都没有被调用)。我的活动代码和清单 XML 文件如下。非常感谢任何帮助或提示。谢谢你。
public class TwitterLoginActivity extends SherlockPreferenceActivity {
private static final String TAG = "TwitterDemo";
private static String CONSUMER_KEY = ""; // filled with my consumer key
private static String CONSUMER_SECRET = ""; //filled with my consumer secret
private static final String CALLBACK_SCHEME = "http";
private String CALLBACK_URL= CALLBACK_SCHEME+"://mjelajahprd.appspot.com/twitter/index";
private OAuthSignpostClient oauthClient;
private OAuthConsumer mConsumer;
private OAuthProvider mProvider;
private Twitter twitter;
private SharedPreferences prefs;
private String authUrl;
private WebView view;
private Boolean status;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
mConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,
CONSUMER_SECRET);
mProvider = new DefaultOAuthProvider(
"http://api.twitter.com/oauth/request_token",
"http://api.twitter.com/oauth/access_token",
"http://api.twitter.com/oauth/authorize");
prefs = PreferenceManager.getDefaultSharedPreferences(this);
String token = prefs.getString("token", null);
String tokenSecret = prefs.getString("tokenSecret", null);
if (token != null && tokenSecret != null) {
mConsumer.setTokenWithSecret(token, tokenSecret);
oauthClient = new OAuthSignpostClient(CONSUMER_KEY,
CONSUMER_SECRET, token, tokenSecret);
} else {
Log.d(TAG, "onCreate. Not Authenticated Yet ");
new OauthAuthorizedTask().execute();
}
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
private class OauthAuthorizedTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
//String authUrl;
String message = null;
Log.d(TAG, "OAuthAuthorizeTask mConsumer: " + mConsumer);
Log.d(TAG, "OAuthAuthorizeTask mProvider: " + mProvider);
try {
authUrl = mProvider.retrieveRequestToken(mConsumer,
CALLBACK_URL);
Intent oauthIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(authUrl));
oauthIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
oauthIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
oauthIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
startActivity(oauthIntent);
} catch (OAuthMessageSignerException e) {
message = "OAuthMessageSignerException";
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
message = "OAuthNotAuthorizedException";
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
message = "OAuthExpectationFailedException";
e.printStackTrace();
} catch (OAuthCommunicationException e) {
message = "OAuthCommunicationException";
e.printStackTrace();
}
return message;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result != null) {
Toast.makeText(TwitterLoginActivity.this, result,
Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d(TAG, "intent: " + intent);
// Check if this is a callback from OAuth
Uri uri = intent.getData();
if (uri != null && uri.getScheme().equals(CALLBACK_SCHEME)) {
Log.d(TAG, "callback: " + uri.getPath());
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
Log.d(TAG, "verifier: " + verifier);
Log.d(TAG, " xxxxxxxxxxx mConsumer access token: " + mConsumer.getToken());
Log.d(TAG, " xxxxxxxxxxxx mConsumer access token secret: " + mConsumer.getTokenSecret());
Log.d(TAG, " xxxxxxxxxxxxx OAuth.OAUTH_TOKEN: " + OAuth.OAUTH_TOKEN);
Log.d(TAG, " xxxxxxxxxxxxx OAuth.OAUTH_TOKEN_SECRET: " + OAuth.OAUTH_TOKEN_SECRET);
new RetrieveAccessTokenTask().execute(verifier);
}
}
class RetrieveAccessTokenTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String message = null;
String oauthVerifier = params[0];
try {
// Get the token
Log.d(TAG, " RetrieveAccessTokenTask mConsumer: " + mConsumer);
Log.d(TAG, " RetrieveAccessTokenTask mProvider: " + mProvider);
Log.d(TAG, " RetrieveAccessTokenTask verifier: " + oauthVerifier);
mProvider.retrieveAccessToken(mConsumer, oauthVerifier);
String token = mConsumer.getToken();
String tokenSecret = mConsumer.getTokenSecret();
mConsumer.setTokenWithSecret(token, tokenSecret);
Log.d(TAG, String.format(
"verifier: %s, token: %s, tokenSecret: %s", oauthVerifier,
token, tokenSecret));
// Store token in prefs
prefs.edit().putString("token", token)
.putString("tokenSecret", tokenSecret).commit();
// Make a Twitter object
oauthClient = new OAuthSignpostClient(CONSUMER_KEY,
CONSUMER_SECRET, token, tokenSecret);
twitter = new Twitter(null, oauthClient);
Intent mainIntent = new Intent(TwitterLoginActivity.this, MainActivity.class);
startActivity(mainIntent);
Log.d(TAG, "token: " + token);
} catch (OAuthMessageSignerException e) {
message = "OAuthMessageSignerException";
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
message = "OAuthNotAuthorizedException";
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
message = "OAuthExpectationFailedException";
e.printStackTrace();
} catch (OAuthCommunicationException e) {
message = "OAuthCommunicationException";
e.printStackTrace();
}
return message;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result != null) {
Toast.makeText(TwitterLoginActivity.this, result,
Toast.LENGTH_LONG).show();
}
}
}
}
Manifest.xml(注意:已经设置使用权限 android:name="android.permission.INTERNET")
<activity
android:name=".TwitterLoginActivity"
android:label="@string/twitter"
android:launchMode="singleTask" >
<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:host="mjelajah-prd.appspot.com/twitter/index"
android:scheme="http" >
</data>
</intent-filter>
</activity>