Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: GraphAPIError: Invalid OAuth access token

I'm making a web app in python using the Flask framework to request the access token from Facebook using the SDK supplied in their site.

The access token is returned and it is correctly set in the GraphAPI object. However, it is returning the following error:

GraphAPIError: Invalid OAuth access token.

If I query the graph API from my local python environment using the same access token, it works just fine. The problem seems to be when executing in the webserver.

See code snippet below:

@app.route('/facebook')
def fb():
  if 'token' in session:
    graph = facebook.GraphAPI(session['token'])
    return graph.get_object("me")


@app.route('/facebook/login')
def fblogin():
  code = request.args.get('code','')
  if(code == ""):
    args = dict(client_id=app_id, redirect_uri=request.base_url)
    #args = dict(client_id=app_id, redirect_uri=request.url_root + 'facebook')
    return redirect(
    "https://graph.facebook.com/oauth/authorize?" +
    urllib.urlencode(args))
  else:
    token = facebook.get_access_token_from_code(code, request.base_url, app_id, app_secret)
    session['token'] = [token.itervalues().next()]
    return redirect (request.url_root + 'facebook')

Has anyone faced this before and/or can provide some insights?

like image 379
Cassio Avatar asked Nov 26 '25 05:11

Cassio


1 Answers

Ok, 2 issues that I have managed to correct in this code and get it working:

1) The following line of code makes a list, that why the GraphAPI object is not able to identify a valid access token:

session['token'] = [token.itervalues().next()]

2) The following line of code gives an error stating that 'dict' is not callable. This is because the returned variable is a dictionary and, in order to be returned as a view, one must first transform it into a string:

return graph.get_object("me")
like image 86
Cassio Avatar answered Nov 27 '25 18:11

Cassio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!