g-signin2无法使用webapp2(谷歌应用引擎,users.get_current_user())

时间:2017-04-08 18:31:03

标签: python angularjs google-app-engine google-signin

所以,这是示例python代码:

import webapp2
from google.appengine.api import users

class HomePage(webapp2.RequestHandler):
    def post(self):
       #getting the email through a http call from the frontend
        email = json.loads(self.request.body).get("email")
        user = users.get_current_user()
        print "incoming: ", email, user

“email”打印正确的登录用户(这意味着我的g-signin2正在运行)但“user”变量为null(这意味着webapp2不了解谁登录)

以下是我的HTML代码:

<script src="https://apis.google.com/js/platform.js" async defer></script>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id"
      content="<myclientid>.apps.googleusercontent.com">

<google-sign-in-button button-id="uniqueid" options="options"></google-sign-in-button>

以下是我的角度指令(对这个问题大多无关,但无论如何只是添加这个以确定):

app.controller('GoogleCtrl', ['$http', '$scope', function ($http, $scope) {

$scope.signOut = function () {
    var auth2 = gapi.auth2.getAuthInstance();
    auth2.signOut().then(function () {
        console.log('User signed out.');
    });
};

$scope.options = {
    'onsuccess': function (response) {
        console.log(response);
        var profile = response.getBasicProfile();
        data = {
            id: profile.getId(),
            name: profile.getName(),
            imageUrl: profile.getImageUrl(),
            email: profile.getEmail()
        }
        $scope.commsApp(data)
    }
}


//communication
$scope.commsApp = function (data) {
    console.log("sendToApp; data: ", data);
    $http({
        method: 'POST',
        url: '/',
        data: data
    })
        .then(function successCallback(response) {
                console.log("success. response: ", response);

            },
            function errorCallback(response) {
                console.log("failure. response: ", response);
            });
    };
}]);

我能够将配置文件信息传递给我的后端,当用户通过g-signin2默认按钮登录时获取该信息。但是,当我使用users.get_current_user()时,我没有得到任何个人资料信息。我如何使这个工作?

1 个答案:

答案 0 :(得分:1)

您正在混合两种不同的身份验证机制:

用户在GAE应用之外的某些应用上使用google-signin登录,并将一些正确的信息(从外部应用程序的角度来看)放入请求正文并不意味着用户也已在中登录到您的GAE应用,该应用使用了来自Users API的users.get_current_user()

要让users.get_current_user()不返回None,用户必须正确登录通过users.create_login_url()获取的网址。