[英]How to get facebook oauth token using python?
I want to get access token from facebook. 我想从Facebook获取访问令牌。 I am using facepy library. 我正在使用Facepy库。 I have posted snippets below. 我在下面发布了摘要。
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'face',
'facepy',
)
FACEBOOK_APPLICATION_INITIAL_PERMISSIONS = ['publish_actions', 'publish_stream', 'manage_pages']
this is my urls.py 这是我的urls.py
urlpatterns = patterns('',
url(r'^$', 'face.views.authorize_application', name='authorize_application'),
url(r'^rag_the_app/$', 'face.views.get_token', name='get_token'),
url(r'^oauth/access_token/$', 'face.views.manage_pages', name='manage_pages'),
)
this is my views.py 这是我的views.py
import urllib
from facepy import GraphAPI
from facepy.utils import get_extended_access_token
def authorize_application(request):
query = {
'client_id': FACEBOOK_APPLICATION_ID,
'redirect_uri': 'http://%s:8000/%s' % (FACEBOOK_APPLICATION_DOMAIN, FACEBOOK_APPLICATION_NAMESPACE),
'scope': FACEBOOK_APPLICATION_INITIAL_PERMISSIONS
}
return render(
request = request,
template_name = 'authorization.html',
dictionary = {
'url': 'https://www.facebook.com/dialog/oauth?%s' % urllib.urlencode(query)
},
status = 401
)
def get_token(request):
code = request.GET.get('code', '')
query = {
'client_id': FACEBOOK_APPLICATION_ID,
'redirect_uri': 'http://%s:8000/%s' % (FACEBOOK_APPLICATION_DOMAIN, FACEBOOK_APPLICATION_NAMESPACE),
'client_secret': FACEBOOK_APPLICATION_SECRET_KEY,
'code': code
}
return render(
request = request,
template_name = 'authorization.html',
dictionary = {
'url': 'https://graph.facebook.com/oauth/access_token?%s' % urllib.urlencode(query)
},
)
request.session['access_token'] = response['access_token']
def manage_pages(request):
oauth_access_token = get_extended_access_token(access_token, FACEBOOK_APPLICATION_ID, FACEBOOK_APPLICATION_SECRET_KEY)
graph = GrahAPI(oauth_access_token)
page_id = '263880043816054'
og_path = "%d/feed" %page_id
graph.post( path = og_path, message = "hello page" )
Facebook App basic setting Facebook App基本设置
canvas url: http://localhost:8000
secure canvas url: https://localhost:8000
app_domain: localhost:8000
Facebook App advanced settings Facebook App高级设置
Valid OAuth redirect URIs 有效的OAuth重定向URI
http://localhost:8000/oauth/access_token/
When I run my server, it raises an error 当我运行服务器时,它会引发错误
Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains.
How can I get the access token? 如何获得访问令牌? what am i doing wrong? 我究竟做错了什么?
You need to expose your dev server to the Internet and give Facebook a way of reaching you for that callback. 您需要将开发服务器公开到Internet,并为Facebook提供一种回调方法。 You can't use localhost. 您不能使用localhost。
All of: 所有的:
...should be the domain/URL of your server, as reachable to the Internets. ...应该是您的服务器的域/ URL,可以通过互联网访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.