我正在尝试使用此代码在我的Android应用程序中获取twitter访问令牌。 方法“provider.retrieveRequestToken(consumer,CALLBACK_URL)”导致异常,日志是“与服务提供者的通信失败:null”。
以下代码是我正在使用的类:
public class MainActivity extends Activity {
private Twitter twitter;
private static final String CONSUMER_KEY = "***";
private static final String CONSUMER_SECRET = "*******";
private static final String REQUEST_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 = "https://twitter.com";
private static CommonsHttpOAuthConsumer consumer;
private static CommonsHttpOAuthProvider provider;
//private static OAuthProvider provider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getBaseContext(), "onCreate", Toast.LENGTH_LONG).show();
Button btn = (Button) findViewById(R.id.btn_tweet);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
askOAuth();
}
});
}
private void askOAuth() {
try {
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
provider = new CommonsHttpOAuthProvider(REQUEST_URL, ACCESS_TOKEN_URL, AUTH_URL);
String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
Toast.makeText(this, authUrl, Toast.LENGTH_LONG).show();
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
try {
provider.retrieveAccessToken(consumer, verifier);
AccessToken a = new AccessToken(consumer.getToken(), consumer.getTokenSecret());
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.panpan"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permisssion.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permisssion.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.panpan.MainActivity"
android:label="@string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateVisible"
android:launchMode="singleInstance">
<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="https" android:host="twitter.com" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
在单独的AsyncTask中执行provider.retrieveRequestToken。这应该有用。
答案 1 :(得分:0)
我有同样的问题(这只发生在Android 4.X上)。您可以使用StrictMode.enableDefaults();或者您可以使用下一个AsyncTask(它更好,因为它不会阻止UI线程)。
mAuthTask = new RequestTask();
mAuthTask.execute((Void) null);// Execute background task
/**
* Represents an asynchronous login/registration task used to authenticate
* the user.
*/
public class RequestTask extends AsyncTask<Void, Void, Boolean> {
String content;
protected void onPreExecute() {
}
protected Boolean doInBackground(Void... params) {
httpOauthConsumer = new CommonsHttpOAuthConsumer(
TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
httpOauthprovider = new DefaultOAuthProvider(REQUEST_URL,
ACCESS_URL, AUTHORIZE_URL);
try {
authUrl = httpOauthprovider.retrieveRequestToken(
httpOauthConsumer, CALLBACK_URL);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
return false;
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
return false;
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
return false;
} catch (OAuthCommunicationException e) {
e.printStackTrace();
return false;
}
return true;
}
@Override
protected void onPostExecute(final Boolean success) {
if (success == true) {
Log.e("LoginUsingTwitterActivity", "onPostExecute");
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
}
}
@Override
protected void onCancelled() {
mAuthTask = null;
}
}