Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 377
user1462558 Avatar asked Jun 18 '12 01:06

user1462558


1 Answers

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) 
like image 131
Xavier Martinez-Hidalgo Avatar answered Oct 21 '22 17:10

Xavier Martinez-Hidalgo