skip to Main Content

I am using Graph API to retrieve the access token of my facebook page..I used the code below to get a JSON respnse which contains the token for my facebook page.

 AccessToken token=AccessToken.getCurrentAccessToken();

        GraphRequest request = GraphRequest.newGraphPathRequest(
                token,
                "/me/accounts",
                new GraphRequest.Callback() {
                    @Override
                    public void onCompleted(GraphResponse response) {
                        // Insert your code here
                        try {

                         //what should I do here.....

                        }catch (JSONException e)
                        {
                            e.printStackTrace();
                        }
                    }
                });

        request.executeAsync();

I got this JSON as response

 {
  "data": [
   {
    "access_token": "<access_token_is_here>",
    "category": "Clothing",
    "name": "<page_name_is_here>",
    "id": "<id_is_here>",
    "perms": [
    "EDIT_PROFILE",
    "CREATE_CONTENT",
    "MODERATE_CONTENT",
    "CREATE_ADS",
    "BASIC_ADMIN"
   ]
 }
],
"paging": {
"cursors": {
  "before": "MjkzMDAyNzM0MTYwNTQ1",
  "after": "MjkzMDAyNzM0MTYwNTQ1"
    }
  }
}

My Question is..How can I get the accessToken from this JSON response? I know that I can get the token as String..But I need it as Access Token itself so that I can use it to post status onto my facebook page….I am a beginner in this so kindly Help me…Thanks

2

Answers


  1.      JSONObject response=new JSONObject(resp);
    
    JSONArray data=response.getJSONArray("data");
    
           String accessToken="";
    
    for(int i=0;i<data.lenght();i++){
    
    JSONObject temp=data.getJSONObject(i);
    
    if(temp.has("access_token"){
    
    accessToken=temp.getString("access_token");
    
    break;
    
    }
    
    }
    

    Now you can also store it into sharedPref or sqlite wherever you want if you want to persist it.

    Login or Signup to reply.
  2. String accesstoken = response.getJsonArray("data").getJSONObject(0).getString("access_token");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search