Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python获取facebook oauth令牌?_Python_Django_Facebook_Oauth - Fatal编程技术网

如何使用python获取facebook oauth令牌?

如何使用python获取facebook oauth令牌?,python,django,facebook,oauth,Python,Django,Facebook,Oauth,我想从facebook获取访问令牌。我正在使用facepy库。我在下面贴了一些片段 # Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django

我想从facebook获取访问令牌。我正在使用facepy库。我在下面贴了一些片段

# 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']
这是我的URL.py

这是我的观点

Facebook应用程序基本设置

Facebook应用程序高级设置

有效的OAuth重定向URI

http://localhost:8000/oauth/access_token/
当我运行服务器时,它会引发一个错误

如何获取访问令牌?
我做错了什么?

您需要将您的开发服务器公开到Internet上,并为Facebook提供一种联系您进行回调的方式。您不能使用localhost

所有:

画布URL 安全画布URL 有效的OAuth重定向URI
…应该是服务器的域/URL,可以通过Internet访问。

为什么我不能使用我的localhost进行重定向,因为localhost通常指这台机器。因此,当您将这些值设置为localhost时,将处理oauth流的facebook服务器将尝试重定向到localhost,从它的角度来看,localhost本身就是localhost,因此您不会得到带有传递到服务器的令牌的回调。流程或多或少是[您的服务器:用户单击使用Facebook登录]->[Facebook:用户是否批准]->[您的服务器:通过批准/拒绝从Facebook回调]。Facebook必须能够联系到你,以便给你回叫令牌或拒绝。因此,如果我想在本地测试我的应用程序,我可以如何做?如果你真的必须这样做,我认为你可以通过在你将要浏览的机器上创建/etc/hosts条目来强制重定向,比如127.0.0.1 somedoma.in。然后将重定向URL设置为。然后,FB应将流返回到您的本地机器。不过,我不是100%肯定这一点。
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" )
canvas url: http://localhost:8000 
secure canvas url: https://localhost:8000
app_domain: localhost:8000
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.