我正在尝试为我的Android应用程序找出Twitter OAuth。我的问题是当Twitter OAuth浏览器打开时,我点击了Authorize App
按钮,但它会指向我的回调网址,暂停并且不会返回我的应用,随后又不会调用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(注意:已设置uses-permission 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>
答案 0 :(得分:2)
尝试使用android:launchMode="singleInstance"
。
private final String CALLBACKURL = "sosInternational:///Test";
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:scheme="sosInternational" android:host="TwitterLoginActivity"></data>
</intent-filter>
请查看此链接以获取更多详细信息。