0
我想弄清楚我的Android應用程序的Twitter OAuth。我的問題是,當Twitter OAuth瀏覽器打開,我點擊Authorize App
按鈕,但它指向我的回調URL,在那裏暫停,並不會回到我的應用程序,隨後不會調用onNewIntent
/onResume
方法(我試過既使用這兩種方法,也不調用)。下面是我的活動代碼和清單XML文件。真的很感謝任何幫助或暗示。謝謝。Twitter身份驗證瀏覽器不回到我的Android應用程序
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>
謝謝您的答覆,我上面已經嘗試按你的建議,並在我的回調URL仍然暫停。 – vie
@vie看看我發佈的鏈接 –
仍然無法正常工作,並得到同樣的問題,它暫停在我的回調網址中。我已經在您提供的鏈接上覆制了所有代碼。順便說一句,我找不到在該鏈接中提到的Twitter註冊頁面。我現在只是在twitter中創建應用程序,並提供與我在活動和清單代碼中使用的回調URL相同的回調URL。無論如何,我可以在哪裏找到Twitter註冊頁面?當我點擊鏈接時,它只是直接到我的電子郵件訂閱頁面。 – vie