我正在尝试使用 Twitter4j 库从我的 android 应用程序登录 twitter,但是在调用回调时它会崩溃......并出现这个屏幕:页面不是 disponible
这是我的代码:
public class TwitterLogin extends Activity {
/** Called when the activity is first created. */
Twitter twitter;
RequestToken requestToken;
/*
* Calls the OAuth login method as soon as its started
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
OAuthLogin();
}
/*
* - Creates object of Twitter and sets consumerKey and consumerSecret
* - Prepares the URL accordingly and opens the WebView for the user to provide sign-in details
* - When user finishes signing-in, WebView opens your activity back
*/
void OAuthLogin() {
try {
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(TwitterConstants.CONSUMER_KEY,TwitterConstants.CONSUMER_SECRET_KEY);
requestToken = twitter.getOAuthRequestToken(TwitterConstants.CALLBACK_URL);
Log.w("TwitterLogin requestToken callbackUrl: "+requestToken.toString());
// requestToken = twitter.getOAuthRequestToken(OAuth.OUT_OF_BAND);
// String pin = requestToken.getParameter(OAuth.OAUTH_VERIFIER);
String authUrl = requestToken.getAuthenticationURL();
Log.w("TwitterLogin requestToken authUrl: "+authUrl);
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
Log.w("TwitterLogin despres del startActivity intent");
} catch (TwitterException ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
Log.e("in Main.OAuthLogin", ex.getMessage());
}
}
/*
* - Called when WebView calls your activity back.(This happens when the user has finished signing in)
* - Extracts the verifier from the URI received
* - Extracts the token and secret from the URL
*/
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.w("TwitterLogin dintre del onNewIntent");
Uri uri = intent.getData();
Log.w("TwitterLogin uri: "+uri.toString());
if (uri != null && uri.toString().startsWith(TwitterConstants.CALLBACK_URL)){
try {
String verifier = uri.getQueryParameter("oauth_verifier");
Log.w("TwitterLogin","onNewIntent oauth_verifier: "+verifier);
AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
Log.w("TwitterLogin","onNewIntent accesToken: "+accessToken.toString());
String token = accessToken.getToken(), secret = accessToken.getTokenSecret();
displayTimeLine(token, secret); //after everything, display the first tweet
} catch (TwitterException ex) {
Log.e("Main.onNewIntent", "" + ex.getMessage());
}
}
}
/*
* Displays the timeline's first tweet in a Toast
*/
@SuppressWarnings("deprecation")
void displayTimeLine(String token, String secret) {
if (null != token && null != secret) {
List<Status> statuses = null;
try {
twitter.setOAuthAccessToken(token, secret);
statuses = twitter.getFriendsTimeline();
Toast.makeText(this, statuses.get(0).getText(), Toast.LENGTH_LONG)
.show();
} catch (Exception ex) {
Toast.makeText(this, "Error:" + ex.getMessage(),
Toast.LENGTH_LONG).show();
Log.d("Main.displayTimeline",""+ex.getMessage());
}
} else {
Toast.makeText(this, "Not Verified", Toast.LENGTH_LONG).show();
}
}
}
如果有人可以帮助我,我已经尝试在 Twitter 上登录 2 天,但我不知道如何解决这个问题。
谢谢