获取Instagram访问令牌与安卓凌空



我使用此代码向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()));

我怎样才能用安卓凌空抽射做到这一点?

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

    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);
}

尝试查看本教程:

Android 中使用凌空的异步 HTTP 请求我在第一次在项目上应用凌空抽射时使用了它。凌空抽射使用起来非常简单。

最新更新