Android 版 Twitter 的 oAuth 错误

发布于 2024-12-16 17:01:49 字数 4177 浏览 1 评论 0原文

我正在使用一个应用程序使用 Twitter4j 库对 Twitter 中的用户进行授权。我想合并一项功能,我的移动应用程序打开。它有一个登录按钮,单击该按钮会出现 Twitter 登录对话框,让您输入登录信息。登录完成后,将打开另一个屏幕。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

public class AndTweetVJActivity extends Activity {
    /** Called when the activity is first created. */


    Twitter twitter;
    RequestToken requestToken;
    public final static String consumerKey = "myKey"; // "your key here";
    public final static String consumerSecret = "myKey"; // "your secret key here";
    private final String CALLBACKURL = "T4JOAuth://main";  //Callback URL that tells the WebView to load this activity when it finishes with twitter.com. (see manifest)


        //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(consumerKey, consumerSecret);
                requestToken = twitter.getOAuthRequestToken(CALLBACKURL);
                String authUrl = requestToken.getAuthenticationURL();
                this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                        .parse(authUrl)));
            } 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);
            Uri uri = intent.getData();
            try {
                String verifier = uri.getQueryParameter("oauth_verifier");
                AccessToken accessToken = twitter.getOAuthAccessToken(requestToken,verifier);
                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());
            }

        }
    }

但是,在运行此应用程序时,它在 logcat 中给出错误:

11-18 10:36:27.727: E/in Main.OAuthLogin(282): 401:Authentication credentials (https://dev.twitter.com/docs/auth) were missing or incorrect. Ensure that you have set valid conumer key/secret, access token/secret, and the system clock in in sync.
11-18 10:36:27.727: E/in Main.OAuthLogin(282): <?xml version="1.0" encoding="UTF-8"?>
11-18 10:36:27.727: E/in Main.OAuthLogin(282): <hash>
11-18 10:36:27.727: E/in Main.OAuthLogin(282):   <error>Desktop applications only support the oauth_callback value 'oob'</error>
11-18 10:36:27.727: E/in Main.OAuthLogin(282):   <request>/oauth/request_token</request>
11-18 10:36:27.727: E/in Main.OAuthLogin(282): </hash>

我相信我没有设置回调 URL,但我也对

I am using an application to authorize a user in Twitter using Twitter4j library. I want to incorporate a feature, my mobile app opens. It has a login button on click of which Twitter login dialog appears and lets you enter the login information . after the login is complete, another screen opens.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

public class AndTweetVJActivity extends Activity {
    /** Called when the activity is first created. */


    Twitter twitter;
    RequestToken requestToken;
    public final static String consumerKey = "myKey"; // "your key here";
    public final static String consumerSecret = "myKey"; // "your secret key here";
    private final String CALLBACKURL = "T4JOAuth://main";  //Callback URL that tells the WebView to load this activity when it finishes with twitter.com. (see manifest)


        //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(consumerKey, consumerSecret);
                requestToken = twitter.getOAuthRequestToken(CALLBACKURL);
                String authUrl = requestToken.getAuthenticationURL();
                this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                        .parse(authUrl)));
            } 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);
            Uri uri = intent.getData();
            try {
                String verifier = uri.getQueryParameter("oauth_verifier");
                AccessToken accessToken = twitter.getOAuthAccessToken(requestToken,verifier);
                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());
            }

        }
    }

however on running this application, it gives me error in logcat :

11-18 10:36:27.727: E/in Main.OAuthLogin(282): 401:Authentication credentials (https://dev.twitter.com/docs/auth) were missing or incorrect. Ensure that you have set valid conumer key/secret, access token/secret, and the system clock in in sync.
11-18 10:36:27.727: E/in Main.OAuthLogin(282): <?xml version="1.0" encoding="UTF-8"?>
11-18 10:36:27.727: E/in Main.OAuthLogin(282): <hash>
11-18 10:36:27.727: E/in Main.OAuthLogin(282):   <error>Desktop applications only support the oauth_callback value 'oob'</error>
11-18 10:36:27.727: E/in Main.OAuthLogin(282):   <request>/oauth/request_token</request>
11-18 10:36:27.727: E/in Main.OAuthLogin(282): </hash>

I believe i had not set up callback URL, but i did that as well to https://dev.twitter.com/pages/welcome-anywhere
in my app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原野 2024-12-23 17:01:49

确保您尚未在 Twitter 应用程序注册时在桌面应用程序类别中注册您的应用程序。

Make sure that you have not register your app in Desktop application category at twitter application registration.

清君侧 2024-12-23 17:01:49

我的错...我的 Web 服务器宕机了,直到 5 分钟前我才注意到。感谢您的所有建议。我已经做了这些事情。

My Bad... My Web server was down and i did not noticed it until 5 minutes ago. thanks for all suggestions. I have already done these things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文