【问题标题】:503 HTTP response code with Twitter OAUTH带有 Twitter OAUTH 的 503 HTTP 响应代码
【发布时间】:2018-02-25 15:06:46
【问题描述】:

我尝试从 Twitter 获取授权令牌(仅限应用程序),代码几乎完全遵循官方指南,但得到 503 服务不可用代码。

        @Override
    protected Boolean doInBackground(Void... params) {
        String cred_enc = tw_cons_key + ":" + tw_priv_cons_key;
        cred_enc = Base64.encodeToString(cred_enc.getBytes(), Base64.NO_WRAP);

        Request request = new Request.Builder().url("https://api.twitter.com/oauth2/token")
                .header("Authorization:", "Basic " + cred_enc)
                .header("Content-Type:", "application/x-www-form-urlencoded;charset=UTF-8")
                .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                ResponseBody body = response.body();
                if (response.isSuccessful()) {
                    Headers headers = response.headers();

                    //response check
                    for (int i = 0; i < headers.size(); i++) {
                        System.out.println("Headers: " + i + " " + headers.name(i) + " : " + headers.value(i));
                    }
                    System.out.println(body.string());

                    try {
                        JSONObject jsonObject = new JSONObject(body.string());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    throw new IOException("Unexpected code" + response);
                }
                body.close();
            }
        });
        return true;
    } 

可能的原因是什么?

【问题讨论】:

  • 在 NetBeans 中运行的相同代码返回 403 Forbidden...
  • HttpUrlConnection 也会导致 403,但 curl 有效! Java 出了什么问题?

标签: android twitter oauth twitter-oauth


【解决方案1】:

这需要我在 localhost 上编写一个简单的服务器来找出实际生成的 HTTP 请求。

问题在于冒号:.header("Authorization:"resulted in Authorization:: in the network request!

从标题键值中删除冒号后,HttpUrlConnection 和 OkHttp 代码变体都可以无缝运行。

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 2012-02-21
    • 1970-01-01
    • 2017-12-23
    • 2011-04-10
    • 2016-02-24
    • 2012-02-08
    • 2015-03-22
    • 1970-01-01
    相关资源
    最近更新 更多