简体   繁体   English

Twitter OAUTH的503 HTTP响应代码

[英]503 HTTP response code with Twitter OAUTH

I try to get authorization token from Twitter (app-only), the code almost fully follows oficial guide, but get 503 Service unavailable code. 我尝试从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;
    } 

What may be the possible reason? 可能是什么原因?

It required me to write a simple server on localhost to find out what HTTP request was actually generated. 它要求我在localhost上编写一个简单的服务器,以找出实际生成的HTTP请求。

The problem was with colons: .header("Authorization:" resulted in Authorization:: in the network request! 问题出在冒号: .header("Authorization:"在网络请求中导致Authorization::

After I removed colons from the header key values, both HttpUrlConnection and OkHttp code variants worked seamlessly. 从标题键值中删除冒号后,HttpUrlConnection和OkHttp代码变体都可以无缝工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM