后天吃什么 2016-06-26 17:07 采纳率: 0%
浏览 2231
已结题

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

拜求大神解答。。。


                                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 2016-06-27 07:21
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名