Search code examples
rfacebook-graph-apiaccess-token

Invalid acces token - Graph Api with R


Until a month ago I was using the Rfacebook library with no problems. When I load the token I had generated and I want to use some function for example

getUsers("me",fb_oauth) #fb_oauth is my token

it gives me the following error:

Error in callAPI(url = url, token = token) : 
  Error validating access token: Session has expired on Tuesday, 04-Apr-17 13:24:59 PDT. The current time is Tuesday, 02-May-17 06:33:21 PDT.

To try to solve it I generate a new password in the app and generate a new token and I get the new token correctly:

fb_oauth <- fbOAuth(app_id="12345678", app_secret="xxxx")

Copy and paste into Site URL on Facebook App Settings: http://localhost:1410/ 
When done, press any key to continue...
Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.

But when I want to use the same function it gives me error

Error in callAPI(url = url, token = token) : 
  An access token is required to request this resource.

Also configure the login in the app but it does not work either Anyone know how to generate the new token with the new 2.9 update?

Regards


Solution

  • As CBroe mentioned, the access token return format has changed and therefore the problem seems to be, that the credentials (including the access token) returned when authorizing with facebook is saved as the field name/key instead of the field value.

    So a fix to this would be the following:

    fb_oauth <- fbOAuth(app_id, app_secret, extended_permissions = FALSE,legacy_permissions = FALSE) 
    
    fb_oauth_credentials <- fromJSON(names(fb_oauth$credentials))
    

    and then to make a request, such as getting a page would be;

    fb_page <- getPage(page = "FBUserNameHere",
                       token = fb_oauth_credentials$access_token)