Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

java - 从http请求android获取内容,内容格式错误(Getting content from http request android, conent ill-formed)

I am trying to connect to the twitter API using OAuth2 method toget the bearer token.

(我正在尝试使用OAuth2方法连接到twitter API以获取承载令牌。)

I am using an AsyncTask making the call to Twitter.

(我正在使用AsyncTask拨打Twitter。)

My request seems to be accepted and return an error code 200 , but when I try to read the content which is supposed to contain the token that I want, but the format is odd, as well as the content-length of the response.

(我的请求似乎已被接受并返回error code 200 ,但是当我尝试读取应该包含我想要的令牌的内容时,但格式为奇数以及响应的内容长度。)

I am building it according to https://developer.twitter.com/en/docs/basics/authentication/overview/application-only

(我正在根据https://developer.twitter.com/zh-CN/docs/basics/authentication/overview/application-only构建它)

Here is the AsyncTask I am using to make the request:

(这是我用来发出请求的AsyncTask :)

private class RequestBearerTockenTask extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground(String... string) {

        // On récupère les keys passées en paramètre
        String consumerKey = string[0];
        String secretKey = string[1];

        // On encode en base64 le bearerToken en accord avec la doc twitter
        String base64encodedBearerToken = Base64.encodeToString((consumerKey + ":" + secretKey).getBytes(), Base64.NO_WRAP);

        // Variables locales de la fonction
        String bearerTocken = "";
        URL url = null;
        HttpURLConnection urlConnection = null;

        try {
            // On formate la requete HTTP
            url = new URL("https://api.twitter.com/oauth2/token");
            urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Host", "api.twitter.com");
            urlConnection.setRequestProperty("User-Agent", " My Twitter App v1.0.23");
            urlConnection.setRequestProperty("Authorization", "Basic " + base64encodedBearerToken);
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            urlConnection.setRequestProperty("Content-Length", "29");
            urlConnection.setRequestProperty("Accept-Encoding", "gzip");

            String requestBody =  "grant_type=client_credentials";
            byte[] outputInBytes = requestBody.getBytes("UTF-8");
            OutputStream os = urlConnection.getOutputStream();
            os.write( outputInBytes );
            os.close();

            // On lit l'entete de la reponse
            int responseCode = urlConnection.getResponseCode();

            switch (responseCode) {
                case 200:
                case 201:

                    BufferedReader rd = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                    String line;
                    while ((line = rd.readLine()) != null) {
                        bearerTocken += line;
                    }
                    break;
                default:
                    bearerTocken = "";
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bearerTocken;
    }
}

The bearerToken at the end is like:

(最后的bearerToken类似于:)

V* N?/ ,HU RJJM,JR QJLNN-. K / rW03 5 +81 $ =?[ ) ? B ?а 2 )+ $ 2 ? ?7 ; 0 2 995= -=/$$ " 30T 9 ?

( V* N?/ ,HU RJJM,JR QJLNN-. K / rW03 5 + 81 $ =?[ ) ? B ? 2 )+ $ 2 ? ?7 ; 0 2 995= -== / $$ ''...30T 9 ? )

with a content length of 149 instead of 140 according to the documentation.

(内容长度为149,而不是文档中的140。)

Do you have any clue why I am getting such a response?

(您是否有任何线索为什么我会收到这样的答复?)

  ask by M. Boutin translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...