简体   繁体   English

在GAE中将javascript传递给python

[英]passing javascript to python in GAE

I am planning to run an app in GAE that initializes the Facebook login sequence in Javascript and passes the access token to a Python script to do server-side work. 我计划在GAE中运行一个应用程序,该应用程序将初始化Javascript中的Facebook登录序列,并将访问令牌传递给Python脚本以完成服务器端工作。 The javascript looks something like this JavaScript看起来像这样

// Additional init code here

        FB.login(
        function(response) {
            if (response.authResponse) {
                var access_token =   FB.getAuthResponse()['accessToken'];
                console.log('Access Token = '+ access_token);

What would be the easiest way to pass access_token to a Python class (say named fbProcessor) that is imported in main.py? 将access_token传递到在main.py中导入的Python类(例如,名为fbProcessor)的最简单方法是什么?

Since the javascript is executed on the front end (client's browser) you can't really "pass" the access_token to python. 由于javascript是在前端(客户端的浏览器)上执行的,因此您无法真正将access_token“传递”给python。 You COULD make a http (AJAX) request from javascript to your python app with the access_token. 您可以使用access_token从javascript向python应用程序发出http(AJAX)请求。

I think that if you are making a request to python you could just use facebook's python SDK to retrieve the information (including access token) for the current FB authenticated user... IDK the benefits of either way 我认为,如果您要向python发送请求,则可以使用Facebook的python SDK来检索当前FB身份验证用户的信息(包括访问令牌)... IDK两种方法的好处

the python facebook SDK has google app engine example's python facebook SDK具有google app引擎示例的

If you are using the module within a web application with the JavaScript SDK, you can also use the module to use Facebook for login, parsing the cookie set by the JavaScript SDK for logged in users. 如果您要通过JavaScript SDK在Web应用程序中使用该模块,则还可以使用该模块来使用Facebook登录,从而解析JavaScript SDK为登录用户设置的cookie。 For example, in Google AppEngine, you could get the profile of the logged in user with: 例如,在Google AppEngine中,您可以通过以下方式获取登录用户的个人资料:

user = facebook.get_user_from_cookie(self.request.cookies, key, secret)
if user:
    graph = facebook.GraphAPI(user["access_token"])
    profile = graph.get_object("me")
    friends = graph.get_connections("me", "friends")

You can see a full AppEngine example application in examples/appengine. 您可以在examples / appengine中看到完整的AppEngine示例应用程序。

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

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