Question

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?

Was it helpful?

Solution

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")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top