簡體   English   中英

使用Android Volley獲取Instagram訪問令牌

[英]Get Instagram Access Token with android volley

我使用此代碼向instagram發布請求並獲得accesstoken。

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

嘗試查看本教程:

Android中使用Volley的異步HTTP請求在我第一次將volley應用於項目時,就使用了它。 Volley非常易於使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM