Facebook OAuth Issues
I am following the directions found here:开发者_StackOverflow中文版
http://developers.facebook.com/docs/authentication/
Trying to connect to the facebook graph API server side. I am using Django and basically copying the same code found here:
https://github.com/facebook/python-sdk/blob/master/examples/oauth/facebookoauth.py
Here is my code
def get_code(request):
c = RequestContext(request)
verification_code = request.GET.get('code',None)
args = dict(client_id=FACEBOOK_APP_ID, redirect_uri=REDIRECT)
if verification_code:
args["client_secret"] = FACEBOOK_SECRET_KEY
args["code"] = verification_code
response = cgi.parse_qs(urllib.urlopen(
"https://graph.facebook.com/oauth/access_token?" +
urllib.urlencode(args)).read())
print "------------------------"
print response
#access_token = response["access_token"][-1]
#print access_token
print "------------------------"
else:
http.HttpResponseRedirect("https://graph.facebook.com/oauth/authorize?" + urllib.urlencode(args))
Anyways, I am able to successfully get the authorization code, but when I try to use it to get my access_token, i see the following response:
{
"error": {
"type": "OAuthException",
"message": "Error validating verification code."
}
}
I have no clue what is going on, but I am following directions on both the facebook site, and the code they provide and this is not working in Python nor from a browser. Interestingly enough, If i use the client-side flow with the same credentials, I am able to get the access_token via a hash-tag, but that is not useful to me.
Also, I am testing locally @ http://127.0.0.1:8000 and have that configured correctly via my facebook app settings.
Thanks
UPDATE:
I fixed it, it turns out both redirect_urs have to be identical i was using
^/facebook/auth/
and
^facebook/auth/token/
as soon as i used both
facebook/auth/ + facebook/auth/
it worked
I see you found an solution to your problem. I wanted to also mention a library I maintain that could possibly make your life easier when consuming OAuth providers with Python, it's called rauth. In particular there's an example getting connected to Facebook's Graph API using Flask that might be useful.
This may not be useful to you now that you've already got it working, but maybe something to consider next time you need to consume an OAuth provider in Python?
精彩评论