【问题标题】:How to get facebook oauth token using python?如何使用 python 获取 facebook oauth 令牌?
【发布时间】: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


    【解决方案1】:

    您需要向 Internet 公开您的开发服务器,并为 Facebook 提供一种联系您进行回调的方式。你不能使用本地主机。

    全部:

    • 画布网址
    • 安全的画布 URL
    • 有效的 OAuth 重定向 URI

    ...应该是您的服务器的域/URL,可以通过 Internet 访问。

    【讨论】:

    • 为什么我不能使用我的本地主机来重定向 uri
    • 因为“localhost”普遍意味着“这台机器”。因此,当您将这些值设置为“localhost”时,将处理 oauth 流的 facebook 服务器将尝试重定向到 localhost(从它的角度来看,它本身就是),因此您不会使用传递到的令牌获得回调你的服务器。流量是(或多或少)[Your Server:User click Log In With Facebook]->[Facebook:User approves or not]->[Your Server:Callback from Facebook with approval/rejection]。 Facebook 必须能够联系到您,以便使用令牌(或拒绝)向您提供回调。
    • 如果我想在本地测试我的应用程序,我该怎么做?
    • 如果你真的需要,我认为你可以通过在你要浏览的机器上创建一个/etc/hosts条目来强制重定向,比如127.0.0.1 somedoma.in .然后将您的重定向 URL 设置为 somedoma.in。然后 FB 应该将流返回到您的本地计算机。不过,我对此不是 100% 确定。
    猜你喜欢
    • 2015-05-05
    • 2011-02-28
    • 2013-04-09
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 2017-09-15
    • 2013-03-16
    相关资源
    最近更新 更多