使用 android volley 获取 Instagram 访问令牌

Get Instagram Access Token with android volley

我将此代码用于 post 对 Instagram 的请求并获取访问令牌。

URL url = new URL(tokenURLString);
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
            httpsURLConnection.setRequestMethod("POST");
            httpsURLConnection.setDoInput(true);
            httpsURLConnection.setDoOutput(true);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpsURLConnection.getOutputStream());
            outputStreamWriter.write("client_id="+CI +
                    "&client_secret="+ CS +
                    "&grant_type=authorization_code" +
                    "&redirect_uri="+CALLBACK_URL+
                    "&code=" + requestToken);
            outputStreamWriter.flush();
            JSONObject jsonObject = new JSONObject(StreamToString.get(httpsURLConnection.getInputStream()));

我如何使用 android 截击来做到这一点?

尝试查看本教程:

Asynchronous HTTP Requests in Android Using Volley 我第一次在项目上应用 volley 时使用了它。 Volley 使用起来非常简单。

我刚刚自己实际实现了这个。这是我使用的代码。

    public void requestAccessToken(final String code) {
    StringRequest request = new StringRequest(Request.Method.POST, TOKENURL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.e("Success Response = ", response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Error Response = ", error.toString());
        }
    }) {
        @Override
        protected Map<String,String> getParams(){
            Map<String, String> params = new HashMap<String, String>();
            params.put("client_id", CLIENTID);
            params.put("client_secret", CLIENTSECRET);
            params.put("grant_type", "authorization_code");
            params.put("redirect_uri", REDIRECTURL);
            params.put("code", code);
            return params;
        }
    };

    Volley.newRequestQueue(this).add(request);
}