triste_who
后天吃什么
采纳率0%
2016-06-26 17:07

Android Twitter授权登录post请求request token返回401

2

拜求大神解答。。。


                                String requestMethod = "POST";
                /** 构建 */
                String oauthSignatureMethod = "HMAC-SHA1";
                String oauthNonce = UUID.randomUUID().toString().replaceAll("-","");
               // String oauthTimeStamp = String.valueOf(System.currentTimeMillis());
                String oauthTimeStamp =new Long(Calendar.getInstance().getTimeInMillis()/1000).toString();
                String authHeader = "oauth_consumer_key=" + TWITTER_KEY +
                        "&oauth_nonce=" + oauthNonce +
                        "&oauth_signature_method=" + oauthSignatureMethod +
                        "&oauth_timestamp=" + oauthTimeStamp +
                        "&oauth_version=1.0";
                String headerDebug = authHeader;

                String twitter_endpoint = "https://api.twitter.com/oauth/request_token";
                String twitter_endpoint_host = "api.twitter.com";
                String twitter_endpoint_path = "/oauth/request_token";

                String signature_base_string = requestMethod + "&"+ encode(twitter_endpoint) + "&" + encode(authHeader);

                String oauth_signature = "";
                try {
                    oauth_signature = computeSignature(signature_base_string, TWITTER_SECRET + "&");  // note the & at the end. Normally the user access_token would go here, but we don't know it yet for request_token
                } catch (GeneralSecurityException e) {
                    e.printStackTrace();
                }
                catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                // each request to the twitter API 1.1 requires an "Authorization: BLAH" header. The following is what BLAH should look like
                String authorizationHeaderString = "OAuth oauth_consumer_key=\"" + TWITTER_KEY + "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" +
                        oauthTimeStamp + "\",oauth_nonce=\"" + oauthNonce + "\",oauth_version=\"1.0\",oauth_signature=\"" + encode(oauth_signature) + "\"";
                System.out.println("authorization_header_string=" + authorizationHeaderString);

               String timestamp = String.valueOf(System.currentTimeMillis());
                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), "");
                OkHttpClient okHttpClient = new OkHttpClient();
                final Request request = new Request.Builder()
                        .url("https://api.twitter.com/oauth/request_token")
                       .addHeader("Authorization",authorizationHeaderString)
                        .post(requestBody)
                        .build();
                List<Protocol> protos = new LinkedList<>();
                protos.add(Protocol.HTTP_1_1);
                okHttpClient.setProtocols(protos);
                okHttpClient.newCall(request).enqueue(new com.squareup.okhttp.Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {

                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                            int code = response.code();

                        String result = response.body().string();

                    }
                });

            }
        });

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • ziyejinwei1994 ziyejinwei1994 5年前

    一般来说该错误消息表明您首先需要登录(输入有效的用户名和密码)。 如果你刚刚输入这些信息,立刻就看到一个 401 错误,就意味着,无论出于何种原因您的用户名和密码其中之一或两者都无效(输入有误,用户名暂时停用,等) 。

    点赞 评论 复制链接分享

相关推荐