[Answer]-KeyError: access token

1👍

Obviously, your request is not successful and the response doesn’t have an access token. According to facebook docs, when a request isn’t good, it returns a response with an error element, something like:

{
  error: {
    message: "Missing redirect_uri parameter.",
    type: "OAuthException",
    code: 191
  }
}

So, in your function, you should do something like:

class FacebookAccessException(Exception): pass

def get_profile(request, token=None):
    ...
    response = json.loads(urllib_response)
    if 'error' in response:
        raise FacebookAccessException(response['error']['message'])

    access_token = response['access_token'][-1]
    return access_token 

PS:

Try to use better urllib. You should try Requests.

Leave a comment