简体   繁体   English

在Django中从Facebook的服务器端身份验证捕获access_token

[英]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.

相关问题 Facebook身份验证:服务器端与客户端。 的Python / Django的 - Facebook authentication: server-side versus client-side. Python/Django 获取“ facebook代码”以从服务器端获取长期访问令牌。 Django的 - Getting the “facebook code” to obtain long-lived-access-token from server side. Django Facebook从access_token开始创建浏览器会话 - Facebook create a browser session starting from access_token Python 带身份验证的请求 (access_token) - Python request with authentication (access_token) 无法获取Facebook access_token - failed to get facebook access_token 使用Django包facepy创建facebook通知:[15](#15)必须使用app access_token调用此方法 - Create a facebook notification with Django package facepy : [15] (#15) This method must be called with an app access_token 使用access_token向Facebook的Graph API发出POST请求 - Using an access_token to make a POST request to Facebook's Graph API Facebook内容在服务器端获取 - Facebook content fetching on server-side 没有PHP的Facebook服务器端登录 - Facebook server-side login without PHP django社交认证从Google oauth2获得错误的access_token - django social auth get wrong access_token from google oauth2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM