后天吃什么 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 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))