Search code examples
javaandroidfacebook-graph-apifacebook-pagefacebook-insights

How to convert string access token to AccessToken object in Facebook SDK


I found my page access token by using the graph api. Now i would like to use my page access token and look up insights for that page. Problem is the page access token returned by the graph api is just a string not a AccessToken object.

I have looked everywhere and could not find anything about converting the found page access token string into a AccessToken object. Is there anyway to take the found page access token string and use it with the Graph api?

Function used to find page access token

public void getPageAccessToken(Context context, final Callback callback)
{

    prefs = PreferenceManager.getDefaultSharedPreferences(context);
    pageId = prefs.getString("CurrentPageID","nullValues");
    Log.d("viewmodel",pageId);


    accessToken = AccessToken.getCurrentAccessToken();
    GraphRequest request = GraphRequest.newGraphPathRequest(
            accessToken,
            "/"+ pageId+"?fields=access_token" ,
            new GraphRequest.Callback() {
                @Override
                public void onCompleted(GraphResponse response) {
                    // Insert your code here

                    Log.d("viewmodel",""+response.getRawResponse());
                    try {
                        pageAccessToken = response.getJSONObject().getString("access_token");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Log.d("viewmodel"," " + pageAccessToken);
                    callback.findPageAccessCallback(pageAccessToken);
                }
            });

    request.executeAsync();
}

Solution

  • You can construct the object yourself using the String:

    AccessToken accessToken = new AccessToken(TOKEN_STRING, APPLICATION_ID, USER_ID, null, null, null, null, null)
    
    

    Except for the first 3 parameters, rest can be null and will be updated when refreshed. On documentation, parameters are listed:

    AccessToken(String, String, String, Collection, Collection, AccessTokenSource, Date, Date)

    enter image description here