"Missing redirect_uri parameter" response from Facebook with Python/Django

11,441

In your first example (the one using HTTPSConnection), you are passing params in the body of the request:

conn.request("GET", "/oauth/access_token", params)

This is incorrect (GET requests should have no body). Instead, parameters should be passed as the query string part of the URL:

conn.request("GET", "/oauth/access_token?" + params) 
Share:
11,441
user1462558
Author by

user1462558

Updated on June 05, 2022

Comments

  • user1462558
    user1462558 almost 2 years

    This is probably a very dumb question, but I have been staring at this for hours and can't find what I'm doing wrong.

    I am trying to use Python to authenticate with the Facebook API, but am having issues requesting a user access token. After receiving a code, I make a request to https://graph.facebook.com/oauth/access_token like so:

    conn = httplib.HTTPSConnection("graph.facebook.com")
    params = urllib.urlencode({'redirect_uri':request.build_absolute_uri(reverse('some_app.views.home')),
                               'client_id':apis.Facebook.app_id,
                               'client_secret':apis.Facebook.app_secret,
                               'code':code})
    conn.request("GET", "/oauth/access_token", params)
    response = conn.getresponse()
    response_body = response.read()
    

    In response, I receive

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

    Any ideas what could be going wrong? I have already verified that the redirect_uri that is being passed is on the app domain, but could it be an issue that this is being hosted locally and that domain is just redirected to localhost by my hosts file?

    Thanks for your help!

    edit:

    I got this working using the requests library:

    params = {'redirect_uri':request.build_absolute_uri(reverse('profiles.views.fb_signup')),
                               'client_id':apis.Facebook.app_id,
                               'client_secret':apis.Facebook.app_secret,
                               'code':code}
    
    r = requests.get("https://graph.facebook.com/oauth/access_token",params=params)
    

    However, I would still prefer to be dependent on a library when this should be supported natively without too much difficulty. Maybe this is asking too much...