136

There's only thing that server has to do; just check any access token's validity.

Clients send to the server user id and access token obtained by FB.getLoginStatus. As I expected, there would be any URL that checks access token's validity, like http://xxx.facebook.com/access_token?=xxxxxxxxxxxxxxxxxxxxxxxxxxxx.

That returns whether it's available one or not or is there any API (server side) for that?

4

7 Answers 7

153

The officially supported method for this is:

GET graph.facebook.com/debug_token?
     input_token={token-to-inspect}
     &access_token={app-token-or-admin-token}

See the check token docs for more information.

An example response is:

{
    "data": {
        "app_id": 138483919580948, 
        "application": "Social Cafe", 
        "expires_at": 1352419328, 
        "is_valid": true, 
        "issued_at": 1347235328, 
        "metadata": {
            "sso": "iphone-safari"
        }, 
        "scopes": [
            "email", 
            "publish_actions"
        ], 
        "user_id": 1207059
    }
}
18
  • 26
    I think it's misleading to say that facebook is more likely to introduce breaking changes. They don't state that anywhere and their official docs make it clear that this is the way to validate the access token
    – Ed Sykes
    Nov 4, 2013 at 22:37
  • 1
    @rynop, well, the name of the API endpoint is "debug_token", and it is described in a section of the Facebook API documentation labelled Getting Info about Tokens and Debugging. This section of the documentation is referred to by HTML anchor #debug, and states that the API is the back-end for their debug tool. Seems pretty clear to me, but you are right that technically, nowhere is it clearly and directly stated that the function is not intended for production use... :-) Apr 29, 2014 at 2:58
  • 7
    The main issue here is that using the me?access_token method is just plain wrong if the data is coming from the client side; since any site can fish for tokens then use them to authenticate into your site by accessing your api.
    – srcspider
    Jul 2, 2015 at 11:09
  • 4
    The OP wanted to check the user ID associated with the token. The /me endpoint returns the user ID, but only if the access token is valid (because, after all, the token is used to determine which "me" to return). So, grab /me and compare user IDs. It must be kept in mind that each app gets its own specially-scoped user IDs, so you can't compare IDs from a different source with the /me you get with your own app's token. Dec 12, 2015 at 19:41
  • 3
    The docs might in the past have referencing using this for debugging. But currently it suggest that this is exactly the use case.
    – AndHeiberg
    Jan 13, 2016 at 18:13
85

You can simply request https://graph.facebook.com/me?access_token=xxxxxxxxxxxxxxxxx if you get an error, the token is invalid. If you get a JSON object with an id property then it is valid.

Unfortunately this will only tell you if your token is valid, not if it came from your app.

9
55

Just wanted to let you know that up until today I was first obtaining an app access token (via GET request to Facebook), and then using the received token as the app-token-or-admin-token in:

GET graph.facebook.com/debug_token?
    input_token={token-to-inspect}
    &access_token={app-token-or-admin-token}

However, I just realized a better way of doing this (with the added benefit of requiring one less GET request):

GET graph.facebook.com/debug_token?
    input_token={token-to-inspect}
    &access_token={app_id}|{app_secret}

As described in Facebook's documentation for Access Tokens here.

7
  • 10
    Thank you. Note for others: the literal "|" character must be included (that doesn't indicate 'or') as shown on the page linked to in the answer: developers.facebook.com/docs/facebook-login/…
    – Mike S
    Oct 26, 2017 at 0:29
  • 1
    Isn't this unsecure? Sending the app-secret via URL query parameters exposes it to anyone "in the middle" between your server and Facebook and HTTPS won't help, since URLs are not encrypted. Anyone could just "listen for" (sniff) requests with URLs in debug_token format and steal Facebook app-secrets.
    – Simeon
    Jul 9, 2018 at 9:43
  • 1
    @Simeon stackoverflow.com/questions/499591/are-https-urls-encrypted It is quite secure.
    – Xeing
    Aug 9, 2018 at 13:16
  • @Xeing thanks, I see I was under the wrong impression :) It seems just the host part of the URL is not encrypted.
    – Simeon
    Aug 9, 2018 at 14:40
  • 1
    Adding the '|' with the app secret got me going finally. Otherwise this API doesn't work. Aug 16, 2018 at 13:15
8

Simply request (HTTP GET):

https://graph.facebook.com/USER_ID/access_token=xxxxxxxxxxxxxxxxx

That's it.

2

The app token can be found from this url.

https://developers.facebook.com/tools/accesstoken

1

I found this official tool from facebook developer page, this page will you following information related to access token - App ID, Type, App-Scoped,User last installed this app via, Issued, Expires, Data Access Expires, Valid, Origin, Scopes. Just need access token.

https://developers.facebook.com/tools/debug/accesstoken/

0

Exchange Access Token for Mobile Number and Country Code (Server Side OR Client Side)

You can get the mobile number with your access_token with this API https://graph.accountkit.com/v1.1/me/?access_token=xxxxxxxxxxxx. Maybe, once you have the mobile number and the id, you can work with it to verify the user with your server & database.

xxxxxxxxxx above is the Access Token

Example Response :

{
   "id": "61940819992708",
   "phone": {
      "number": "+91XX82923912",
      "country_prefix": "91",
      "national_number": "XX82923912"
   }
}


Exchange Auth Code for Access Token (Server Side)

If you have an Auth Code instead, you can first get the Access Token with this API - https://graph.accountkit.com/v1.1/access_token?grant_type=authorization_code&code=xxxxxxxxxx&access_token=AA|yyyyyyyyyy|zzzzzzzzzz

xxxxxxxxxx, yyyyyyyyyy and zzzzzzzzzz above are the Auth Code, App ID and App Secret respectively.

Example Response

{
   "id": "619XX819992708",
   "access_token": "EMAWdcsi711meGS2qQpNk4XBTwUBIDtqYAKoZBbBZAEZCZAXyWVbqvKUyKgDZBniZBFwKVyoVGHXnquCcikBqc9ROF2qAxLRrqBYAvXknwND3dhHU0iLZCRwBNHNlyQZD",
   "token_refresh_interval_sec": XX92000
}

Note - This is preferred on the server-side since the API requires the APP Secret which is not meant to be shared for security reasons.

Good Luck.

Not the answer you're looking for? Browse other questions tagged or ask your own question.