Invalid OAUTH access token, when using an applicat

2019-07-27 10:30发布

问题:

As the title says, when I try to request to get the friends list with the field installed:

"me/friends?Fields=installed&access_token=..."

I get the following error in my logcat:

"Invalid OAuth access token"

When looking on the facebook api I see that installed needs to take an application access token. So I generated the application access token using the appId and app Secret using:

https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=APP_ID&client_secret=APP_SECRET

Below is the code:

try {

 JSONObject obj = Util.parseJson(mFacebook.request("me/friends?fields=installed&access_token=..."));

  Log.d("json Response", obj.toString());
  JSONArray array = obj.optJSONArray("data");
  if (array != null) {
                      for (int i = 0; i < array.length(); i++) {
                        //  String name = array.getJSONObject(i).getString("name");

                          String id = array.getJSONObject(i).getString("id");

                          Log.d("Friends: ",id);
                      }
                  } 
  }catch (Exception e){
    Log.d("Friends:", e.getMessage());
  }

Any one any ideas why its doing this I have been searching for ages.

Cheers

回答1:

What you are getting after authentication is App Access Token.

Quoting : Authenticating as an App allows you to obtain an access token which allows you to make request to the Facebook API on behalf of an App rather than a User. This is useful, for example to modify the parameters of your App, create and manage test users, or read your application's insights for example. App access tokens can also be used to publish content to Facebook on behalf of a user who has granted a publishing permission to your application

from facebook docs.

What you need for your case is an user access token. Refer this. A user needs to authenticate and grant access to your app for you have access to his friends list.

EDIT

For checking for a single user the url is https://graph.facebook.com/{uid}?fields=installed&access_token=< ACCESS_TOKEN >

You are trying to get user friend list and then check if the app is installed. I don't think getting user friend list is possible without user access token.

EDIT 2

Assuming from your comment that you have the list of frind's. Then you need not call for each user instead you can use FQL.

https://graph.facebook.com/fql?q=SELECT uid,is_app_user from user where uid IN (uid1,uid2)

And if you have the user access token then you can directly do.

https://graph.facebook.com/fql?q=SELECT uid,is_app_user from user where uid IN (SELECT uid2 FROM friend WHERE uid1 = me())

Hope this solves your problem.



回答2:

Don’t request /me/friends – because with an app access token the API won’t know who „me” is supposed to be – request /userid/friends instead.

Sorry, I was wrong before – the way to go is with an user access token, and just setting the request up against /me/friends…