[英]Capture access_token from facebook's server-side authentication in Django
I want to capture the access_token returned by this url(below) 我想捕获此URL返回的access_token(如下)
https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&redirect_uri=YOUR_REDIRECT_URI&client_secret=YOUR_APP_SECRET&code=CODE_GENERATED_BY_FACEBOOK
https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&redirect_uri=YOUR_REDIRECT_URI&client_secret=YOUR_APP_SECRET&code=CODE_GENERATED_BY_FACEBOOK
But if I HttpResponseredirect, it takes me to a blank page with access_token and expiry secs printed. 但是,如果我使用HttpResponseredirect,它将带我进入一个空白页面,上面打印了access_token和到期秒数。 I want to capture the returned access_token and use it later.
我想捕获返回的access_token并在以后使用。 Below is my code
下面是我的代码
def fb_return(request):
code = request.GET.get('code')
fb_id = settings.FB_ID
fb_s = settings.FB_SECRET
url = 'https://graph.facebook.com/oauth/access_token?client_id=%(id)s&redirect_uri=http://127.0.0.1:8000/facebook/return&client_secret=%(secret)s&code=%(code)s'%{'id':fb_id,'secret':fb_s,'code':code}
return HttpResponseRedirect(url)
You can use urllib
to perform the request: 您可以使用
urllib
执行请求:
import urllib2
url = 'https://graph.facebook.com/oauth/access_token?client_id=%(id)s&redirect_uri=http://127.0.0.1:8000/facebook/return&client_secret=%(secret)s&code=%(code)s'%{'id':fb_id,'secret':fb_s,'code':code}
response = urllib2.urlopen(url)
html = response.read()
If the response is json, you can decode it like so: 如果响应是json,则可以将其解码为:
import simplejson
json = response.read()
dict = simplejson.load(json)
Here's a similar question dealing with this 这是一个类似的问题
Depending on what you are trying to do, there are probably easier ways to interact with Facebook: 根据您尝试做的事情,可能有更简单的与Facebook交互的方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.