How to get facebook user access token in android?

风格不统一 提交于 2019-11-30 08:57:52

Using the Facebook SDK the better way to manage the authentication of the user is by means of the Session class. When you have a valid instance of the Session class you just have to call the getAccessToken() on it in order to obtain the String representing the access token.

Session session = Session.getActiveSession();
if (session != null && session.getState().isOpened()){
     Log.i("sessionToken", session.getAccessToken());
     Log.i("sessionTokenDueDate", session.getExpirationDate().toLocaleString());
}

I was having the same problem using the latest Facebook Android SDK (4.0.1).

I used AccessToken.getCurrentAccessToken() and it worked:

    GraphRequest request = GraphRequest.newMeRequest(
            AccessToken.getCurrentAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject jsonObject, GraphResponse response) {
                    Log.d(LOG_TAG,"onCompleted jsonObject: "+jsonObject);
                    Log.d(LOG_TAG,"onCompleted response: "+response);
                    // Application code
                }
            });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,link,cover,email");
    request.setParameters(parameters);
    request.executeAsync();

GET /oauth/access_token? client_id={app-id} &client_secret={app-secret} &grant_type=client_credentials

Answer to the original question has already been entered as update-1. But it was only working for admin users.

Actually during app creation, I had wrongly selected 'sandboxed' mode which restricts app's access only to developers added in app configuration page. So after disabling this mode, I was able to generate access token for other users too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!