简体   繁体   English

通过Python SDK获取facebook OAuth access_token似乎不起作用,有什么想法吗?

[英]Getting facebook OAuth access_token through Python SDK does not seem to be working, any ideas?

According to http://github.com/facebook/python-sdk/blob/master/src/facebook.py , In my canvas application, i can do the following call to get my access_token, which will work because my user has used facebook to login: 根据http://github.com/facebook/python-sdk/blob/master/src/facebook.py的说明 ,在我的canvas应用程序中,我可以执行以下调用以获取我的access_token,该操作将起作用,因为我的用户已使用Facebook登录:

import facebook 

myDict = facebook.get_user_from_cookie(cookies, app_id, app_secret)
# my access_token is myDict["access_token"]

myDict is alwas None, any ideas? myDict一直是None,有什么想法吗?

EDIT: I am running on app engine, By "canvas", i mean a facebook app which is ran inside of the facebook platform. 编辑:我在应用程序引擎上运行,通过“画布”,我的意思是在Facebook平台内运行的Facebook应用程序。 Ie, it is'nt on my webiste, iphone or a desktop app. 即,它不在我的网站,iPhone或桌面应用程序上。 I realize that it is up to the javascript SDK to set the cookie, but to my understanding, if the application is running inside of facebook, then the cookies should allready be set. 我意识到,由JavaScript SDK来设置cookie,但是据我所知,如果应用程序在facebook内部运行,则应该已经设置了cookie。

For this to work, you need to use the Javascript SDK to init the session at which point the python-sdk is able to read the cookies as set by the javascript-sdk. 为此,您需要使用Javascript SDK来初始化会话,届时python-sdk可以读取javascript-sdk设置的cookie。

Since you are using an IFrame canvas, the cookies set by facebook are not readable by you. 由于您使用的是IFrame画布,因此无法读取facebook设置的cookie。 In the python-sdk, there is a sample appengine oauth example that includes most of what you need to make it work. 在python-sdk中,有一个示例appengine oauth示例,其中包含使它正常工作所需的大部分内容。

You need to have the Facebook javascript SDK in the bottom of your html-file... the code is: 您需要在html文件的底部安装Facebook javascript SDK。代码为:

<!-- loads the facebook javascript API asyncroneously and saves the token in a cookie for use in the python code -->
    <div id="fb-root"></div>
    <script>
        window.fbAsyncInit = function() {
            FB.init({appId: '{{ facebook_app_id }}', status: true, cookie: true, xfbml: true});
        FB.Event.subscribe('auth.logout', function(response) {
        });
        FB.Event.subscribe('auth.login', function(response) {
        });
    };

    (function() {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    }());
</script>

Then, in your python handler you can do the 然后,您可以在python处理程序中执行

cookie = facebook.get_user_from_cookie(self.request.cookies, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET)
graph = facebook.GraphAPI(cookie["access_token"])
current_user = graph.get_object("me")

This is a working example from my own app 这是我自己的应用程序中的一个有效示例

In your facebook app, go to "Edit Settings" => "Migrations" and enable "Canvas Session Parameter". 在您的Facebook应用程序中,转到“编辑设置” =>“迁移”并启用“画布会话参数”。 Now you can get the access token from request GET parameters. 现在,您可以从请求GET参数获取访问令牌。 And, then use python-sdk to access graph API with the access token. 然后,使用python-sdk访问带有访问令牌的图形API。

To use crossdomain cookies you must set P3P policies. 要使用跨域cookie,您必须设置P3P策略。

For app engine you must set header: 对于应用引擎,您必须设置标头:

self.response.headers['P3P']='CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'

And also you must use Facebook Javascript SDK with such params: 而且,您还必须将Facebook Javascript SDK与以下参数一起使用:

FB.init({
    appId  : '***********',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true,  // parse XFBML
    session: 'json_encoded_sessio_getted_on_backen',
    channelUrl : location.href+'/channel.html'
});

I found that if you do not session here, FB will not send cookies to you iframe. 我发现,如果您不在此处进行会话,FB不会将cookie发送给您iframe。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM