登录时出现Android Tweet Api错误

时间:2013-04-03 05:55:09

标签: android oauth twitter

我在许多教程中遇到错误,现在我正在尝试this tutorial,问题是当浏览器打开时,我登录到twitter,当twitter要求授权我点击登录按钮,但没有任何反应,就像我说的,我在许多教程中都有这个问题,不仅如此,我做错了什么?

BTW,我添加了这个类来修复NetworkOnUI错误:

private class Logintw extends AsyncTask<Void, Void, Uri> {

    @Override
    protected Uri doInBackground(Void... params) {
        try {
            requestToken = twitter
                    .getOAuthRequestToken(TWITTER_CALLBACK_URL);
        } catch (TwitterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return Uri.parse(requestToken.getAuthenticationURL());
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
     */
    @Override
    protected void onPostExecute(Uri result) {
        Intent in = new Intent(Intent.ACTION_VIEW, result);
        in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplicationContext().startActivity(in
                );
        super.onPostExecute(result);
    }

}

2 个答案:

答案 0 :(得分:0)

private void loginToTwitter() {
    // Check if already logged in
    if (!isTwitterLoggedInAlready()) {
        try {
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
        builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
        Configuration configuration = builder.build();
        TwitterFactory factory = new TwitterFactory(configuration);
        twitter = factory.getInstance();
            TrequestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);
            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(TrequestToken.getAuthenticationURL())));
        } catch (TwitterException e) {
            e.printStackTrace();
        }
    } else {
        Toast.makeText(getApplicationContext(),"Already Login to twitter!-----", Toast.LENGTH_SHORT).show();
        TwitterLogin=true;
        checkloginstatus();

    }
}

答案 1 :(得分:0)

protected void onNewIntent(Intent intent) {
    Uri uri = intent.getData();
        if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) {
            String verifier = uri
                .getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);
            try {
                // Get the access token
                AccessToken accessToken = twitter.getOAuthAccessToken(TrequestToken, verifier);
                // Shared Preferences
                mPrefs=getPreferences(MODE_PRIVATE);
                Editor e = mPrefs.edit();
                // After getting access token, access token secret store them in application preferences
                e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken());
                e.putString(PREF_KEY_OAUTH_SECRET,accessToken.getTokenSecret());
                // Store login status - true
                e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
                e.commit(); // save changes
                long userID = accessToken.getUserId();
                User user = twitter.showUser(userID);
                String username = user.getName();
                TwitterLogin=true;
                checkloginstatus();
                new updateTwitterStatus().execute(username+ " is using http://AllPagesManager.com , Try it Now!!!");
                } catch (Exception e) {
                    // Check log for login errors
                    Log.e("Twitter Login Error", "> " + e.getMessage());
                    }