0

I am using the curl command below to get access token from instagram api after getting authorization code.

curl \-F 'client_id=cf07d1a2c69940e59420b6db4c936f4a' \
-F 'client_secret=fb0a975ca2024a1592459308df5ead47' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=http://localhost:8080/Insta_SMI_M1/accessToken/' \
-F 'code=fcf66e5f09bf43a18ab15e5f1e0ae75f' 
\https://api.instagram.com/oauth/access_token/

Output:

{"access_token": "5351945621.cf07d1a.1d35647e22f24ed0885f65545f3f1b0b", "user": {"id": "5351945621", "username": "abhaykumar", "profile_picture": "https://scontent-amt2-1.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg", "full_name": "Quantum Four", "bio"}

Curl Url:

https://api.instagram.com/oauth/access_token?client_id=cf07d1a2c69940e59420b6db4c936f4a&client_secret=fb0a975ca2024a1592459308df5ead47&grant_type=authorization_code&redirect_uri=http://localhost:8080/Insta_SMI_M1/auth&code=2c5d97c6d6454b8592816d7d39efb935

The above url neither giving any error nor showing output(its blank line) while using in postman or browser.

Below is the code for same.

 @RequestMapping(value="/auth", method=RequestMethod.GET)
 public String getAuthCode(HttpServletRequest request, HttpServletResponse response)
 {   
        String code = request.getParameter("code");
        System.out.println("code is: "+ code);


        String url = "https://api.instagram.com/oauth/access_token?"
                + "client_id=" + Constants.CLIENT_ID
                + "&client_secret=" + Constants.CLIENT_SECRET
                + "&grant_type=authorization_code"
                + "&redirect_uri=" + Constants.REDIRECT_URI_AUTH
                + "&code="+code;

        System.out.println("Access Token URL: "+ url);
         StringBuffer result = null;
        try {
            System.out.println("1");
            @SuppressWarnings({ "resource", "deprecation" })
            HttpClient client = new DefaultHttpClient();
             HttpGet request1 = new HttpGet(url);
            System.out.println("2");

             HttpResponse response1 = client.execute(request1);
             BufferedReader rd = new BufferedReader(
                     new InputStreamReader(response1.getEntity().getContent()));
                System.out.println("3");

             result = new StringBuffer();
             String line = "";
             while ((line = rd.readLine()) != null) {
                    System.out.println("line " + line);
                 result.append(line);
                    System.out.println("3");

             }
        } catch (UnsupportedOperationException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        System.out.println(result.toString()); 
        return result.toString();    
 }

Can any body help me with this? Thanks

iamabhaykmr
  • 1,199
  • 11
  • 33
  • If the URL isn't working properly in Postman, that's a sign that there's something wrong with your URL. – betseyb Apr 26 '17 at 15:56

2 Answers2

1

The curl is doing a POST request where you are doing a GET request form java. Follow this example about how to make a POST request using java (with http-client). You can consider the following piece of code to set your parameters:

params.add(new BasicNameValuePair("client_id", "cf07d1a2c69940e59420b6db4c936f4a"));
params.add(new BasicNameValuePair("client_secret", "fb0a975ca2024a1592459308df5ead47"));
Community
  • 1
  • 1
Sabuj Hassan
  • 35,286
  • 11
  • 68
  • 78
0

Since I din't find a complete code to get access token from instagram api on internet , I will put the code below which worked for me.

 public String accessTkn (String code)
 {
     try {
         HttpClient httpclient = HttpClients.createDefault();
         HttpPost httppost = new 
         HttpPost("https://api.instagram.com/oauth/access_token");

         // Request parameters and other properties.
         List<NameValuePair> params = new ArrayList<NameValuePair>(2);
         params.add(new BasicNameValuePair("client_id", Constants.CLIENT_ID));
         params.add(new BasicNameValuePair("client_secret", Constants.CLIENT_SECRET));
         params.add(new BasicNameValuePair("grant_type", "authorization_code"));
         params.add(new BasicNameValuePair("redirect_uri",  Constants.REDIRECT_URI_AUTH));
         params.add(new BasicNameValuePair("code",  code));


         httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

         //Execute and get the response.
         HttpResponse response = httpclient.execute(httppost);
         HttpEntity entity = response.getEntity();
         System.out.println("entity "+ entity.getContent());

         if (entity != null) {
             InputStream instream = entity.getContent();
             try {

                return (getStringfromStream(instream));
                 // do something useful
             } finally {
                 instream.close();
             }
         }
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedOperationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return "Abhay";

 }

Output:

{"access_token": "5351945621.cf07d1a.1d35647e22f24ed0885f65545f3f1b0b", "user": {"id": "5351945621", "username": "abhaykumar", "profile_picture": "https://instagram.fsgn2-1.fna.fbcdn.net/t51.2885-19/11906329_960233084022564_1448528159_a.jpg", "full_name": "Abhay Kumar", "bio": "", "website": ""}}
iamabhaykmr
  • 1,199
  • 11
  • 33