【发布时间】:2014-09-15 21:24:01
【问题描述】:
我想从 facebook 获取访问令牌。我正在使用 facepy 库。我在下面发布了sn-ps。
# 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']
这是我的 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'),
)
这是我的意见.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 应用基本设置
canvas url: http://localhost:8000
secure canvas url: https://localhost:8000
app_domain: localhost:8000
Facebook 应用高级设置
有效的 OAuth 重定向 URI
http://localhost:8000/oauth/access_token/
当我运行我的服务器时,它会引发错误
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.
如何获取访问令牌? 我做错了什么?
【问题讨论】:
标签: python django facebook oauth