从代码中获取access_token

时间:2017-01-06 11:58:33

标签: android instagram-api

我有code,现在我希望从我收到的access_token获得code
我尝试以这种方式获取code,但我没有在username或其他变量中得到任何结果!

try {
    String client_secret = "aeddb1f83bd44eb08ebc3cfc357cb462";
    String tokenURLString = "https://api.instagram.com/oauth/access_token" + "?client_id=" + client_id + "&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri + "&grant_type=authorization_code";
    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=" + client_id +
            "&client_secret=" + client_secret +
            "&grant_type=authorization_code" +
            "&redirect_uri=" + "http://www.mycodingexperience.blog.ir/" +
            "&code=" + code);

    outputStreamWriter.flush();
    String response = streamToString(httpsURLConnection.getInputStream());
    JSONObject jsonObject = (JSONObject) new JSONTokener(response).nextValue();
    String accessTokenString = jsonObject.getString("access_token"); //Here is your ACCESS TOKEN
    String id = jsonObject.getJSONObject("user").getString("id");
    String username = jsonObject.getJSONObject("user").getString("username");
} catch (Exception e) {
    e.printStackTrace();
}  

如何在Explicit中获取access_token

0 个答案:

没有答案