本文介绍了获得401请求与Android之内的指路牌访问令牌时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code,我不断收到一个例外授权失败(服务器回答一个401)。这可能是因为消费者关键是不正确或不签名不匹配。在这条线provider.retrieveAccessToken(消费者,验证);。我有三重检查我的使用者密钥和机密,我的Twitter应用程序设置为浏览器,并尝试设置provider.setOAuth10a(真),我一直挣扎在这2天!我使用的路标1.2.1.1(核心及放大器; commonshttp4),如果有人可以帮助!请即时绝望

 私有静态最后弦乐CONSUMER_KEY =MY_CONSUMER_KEY;
    私有静态最后弦乐CONSUMER_SECRET =MY_CONSUMER_SECRET;    私有静态最后弦乐CALLBACK_URL =鸣叫映射器:// mainactivity    私有静态最后弦乐REQUEST_URL =htt​​ps://api.twitter.com/oauth/request_token;
    私有静态最后弦乐ACCESS_TOKEN_URL =htt​​ps://api.twitter.com/oauth/access_token;
    私有静态最后弦乐AUTH_URL =htt​​ps://api.twitter.com/oauth/authorize;    私有静态最后弦乐preFERENCE_FILE =twitter_oauth preFS。    私有静态CommonsHttpOAuthConsumer消费;
    私有静态CommonsHttpOAuthProvider提供商;    私人静态字符串ACCESS_KEY;
    私人静态字符串ACCESS_SECRET;    私人Twitter的微博;
    / **
     *当第一次创建活动调用。
     * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        loginViaOAuth();    }    私人无效loginViaOAuth(){
        尝试{
            消费=新CommonsHttpOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
            provider.setOAuth10a(真);
            供应商=新CommonsHttpOAuthProvider(REQUEST_URL,ACCESS_TOKEN_URL,AUTH_URL);
            字符串authURL = provider.retrieveRequestToken(消费者,CALLBACK_URL);
            this.startActivity(新意图(Intent.ACTION_VIEW,Uri.parse(authURL)));
        }赶上(例外五){
            e.printStackTrace();
            Toast.makeText(这一点,e.getMessage(),Toast.LENGTH_LONG).show();
        }
    }    @覆盖
    保护无效onResume(){        super.onResume();        。URI URI = this.getIntent()的getData();
        如果(URI = NULL&放大器;!&安培; uri.toString()startsWith(CALLBACK_URL)){
            字符串验证= uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
            Log.d(验证,验证);
            尝试{                provider.setOAuth10a(真);
                provider.retrieveAccessToken(消费者,验证);
                ACCESS_KEY = consumer.getToken();
                ACCESS_SECRET = consumer.getTokenSecret();                一个的accessToken =新的accessToken(ACCESS_KEY,ACCESS_SECRET);                //初始化Twitter4J
                叽叽喳喳=新的Twitter();
                twitter.setOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
                twitter.setOAuthAccessToken(一);
                字符串鸣叫=#OAuth通过Android应用程序的工作!                twitter.updateStatus(鸣叫);
                Toast.makeText(这一点,鸣叫,Toast.LENGTH_LONG).show();            }赶上(例外五){
                e.printStackTrace();
                Toast.makeText(这一点,e.getMessage(),Toast.LENGTH_LONG).show();
            }
        }


解决方案

刚刚发现了一个可能的解决方案:你需要设置一个回调网址上你的Twitter应用程序的帐户

Here is my code, i keep getting an exception "Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match." on this line 'provider.retrieveAccessToken(consumer, verifier);'. I have triple checked my consumer key and secret and my twitter application is set as a Browser and tried setting provider.setOAuth10a(true), i have been struggling on this for 2 days!! I am using signpost 1.2.1.1 (core & commonshttp4), If anyone can help! Please im desperate

    private static final String CONSUMER_KEY = "MY_CONSUMER_KEY";
    private static final String CONSUMER_SECRET = "MY_CONSUMER_SECRET";

    private static final String CALLBACK_URL = "tweet-mapper://mainactivity";

    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 PREFERENCE_FILE = "twitter_oauth.prefs";

    private static CommonsHttpOAuthConsumer consumer;
    private static CommonsHttpOAuthProvider provider;

    private static String ACCESS_KEY;
    private static String ACCESS_SECRET;

    private Twitter twitter;


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

        loginViaOAuth();

    }

    private void loginViaOAuth() {
        try {
            consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            provider.setOAuth10a(true);
            provider = new CommonsHttpOAuthProvider(REQUEST_URL, ACCESS_TOKEN_URL, AUTH_URL);
            String authURL = provider.retrieveRequestToken(consumer, CALLBACK_URL);
            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authURL)));
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onResume() {

        super.onResume();

        Uri uri = this.getIntent().getData();
        if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
            String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
            Log.d("verifier:", verifier);
            try {

                provider.setOAuth10a(true);
                provider.retrieveAccessToken(consumer, verifier);
                ACCESS_KEY = consumer.getToken();
                ACCESS_SECRET = consumer.getTokenSecret();

                AccessToken a = new AccessToken(ACCESS_KEY, ACCESS_SECRET);

                // initialize Twitter4J
                twitter = new Twitter();
                twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
                twitter.setOAuthAccessToken(a);
                String tweet = "#OAuth working via android app!";

                twitter.updateStatus(tweet);
                Toast.makeText(this, tweet, Toast.LENGTH_LONG).show();

            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
解决方案

Just found out a possible solution: You need to set a Callback URL on your twitter application account.

这篇关于获得401请求与Android之内的指路牌访问令牌时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 16:49