Nov 8, 2018

Example of Java JSON parser

This is an Example of Java JSON parser to access Instagram access token. In this example we are going to use a common Java toolkit for JSON JSON.simple

<dependency>
           <groupId>com.googlecode.json-simple</groupId>
           <artifactId>json-simple</artifactId>
           <version>1.1.1</version>
</dependency>

Instagram returns a neatly packaged OAuth Token that you can use to make authenticated calls to Instagram API. It also includes the user who just authenticated

{
   "access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",
   "user": {
       "id": "1574083",
       "username": "snoopdogg",
       "full_name": "Snoop Dogg",
       "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg"
   }
}

Java code to get the access_token:

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

JSONParser jsonParser = new JSONParser();
       JSONObject jsonObject = (JSONObject) jsonParser.parse(responseBody);
       String token=String.valueOf(jsonObject.get("access_token"));