简体   繁体   中英

how to oauth twitter with Twitter4J api

I want to authenticate twitter with Twitter4j api but it gives following error:

   09-13 12:12:09.223: WARN/System.err(677): oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.
09-13 12:12:09.233: WARN/System.err(677):     at oauth.signpost.basic.DefaultOAuthProvider.retrieveToken(DefaultOAuthProvider.java:74)
09-13 12:12:09.233: WARN/System.err(677):     at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:66)
09-13 12:12:09.233: WARN/System.err(677):     at com.facebook.android.OAuthTwitter.onCreate(OAuthTwitter.java:45)
09-13 12:12:09.233: WARN/System.err(677):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
09-13 12:12:09.233: WARN/System.err(677):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
09-13 12:12:09.233: WARN/System.err(677):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
09-13 12:12:09.233: WARN/System.err(677):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
09-13 12:12:09.243: WARN/System.err(677):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
09-13 12:12:09.243: WARN/System.err(677):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 12:12:09.243: WARN/System.err(677):     at android.os.Looper.loop(Looper.java:123)
09-13 12:12:09.243: WARN/System.err(677):     at android.app.ActivityThread.main(ActivityThread.java:4203)
09-13 12:12:09.243: WARN/System.err(677):     at java.lang.reflect.Method.invokeNative(Native Method)
09-13 12:12:09.243: WARN/System.err(677):     at java.lang.reflect.Method.invoke(Method.java:521)
09-13 12:12:09.243: WARN/System.err(677):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-13 12:12:09.243: WARN/System.err(677):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
09-13 12:12:09.243: WARN/System.err(677):     at dalvik.system.NativeStart.main(Native   method)

and i use the below code;

public class OAuthTwitter extends Activity {
public final static String CALLBACK_URL = "callback://twitter"; // ----
                                                                // (4)

private CommonsHttpOAuthConsumer commonHttpOAuthConsumer;
private OAuthProvider authProvider;

private Twitter twitter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    commonHttpOAuthConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,
            CONSUMER_SECRET); // ----- (5)
    authProvider = new DefaultOAuthProvider(
            "https://api.twitter.com/oauth/request_token",
            "https://api.twitter.com/oauth/access_token",
            "https://api.twitter.com/oauth/authorize");
    try {
        String oAuthURL = authProvider.retrieveRequestToken(
                commonHttpOAuthConsumer, CALLBACK_URL);

        this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                .parse(oAuthURL)));

    } catch (OAuthMessageSignerException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } catch (OAuthNotAuthorizedException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } catch (OAuthExpectationFailedException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } catch (OAuthCommunicationException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

protected void onNewIntent(Intent intent) { // ---- (6)
    super.onNewIntent(intent);

    Uri uri = intent.getData(); // ---- (7)
    if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
        String verifier = uri
                .getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
        try {
            authProvider.retrieveAccessToken(commonHttpOAuthConsumer,
                    verifier); // ---- (8)

            AccessToken accessToken = new AccessToken(
                    commonHttpOAuthConsumer.getToken(),
                    commonHttpOAuthConsumer.getTokenSecret()); // ---- (9)

            twitter = new TwitterFactory().getInstance(); // ---- (10)
            twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);

            twitter.setOAuthAccessToken(accessToken); // ---- (11)

            // Tweet message to be updated.
            String tweet = "Hiee there, This is send from my application - OAuth, Twitter";
            twitter.updateStatus(tweet); // ---- (12)
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_LONG);
        }
    }
}
}

using the jars: signpost-commonshttp4-1.2.1.1.jar,signpost-core-1.2.jar,twitter4j-core-android-2.2.4.jar plz anyone tell me where i am wrong.

错误是用户密钥...意味着它可能是错误的。.或者您已经正确地创建了注释,因为您必须单击dev.twitter.com上的Create a app链接,然后使用您的Twitter帐户登录后,在下一页上输入名称等详细信息,您将获得安全密钥和使用者密钥,回调URL等...将该密钥放入代码中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM