Python g-signin2未与webapp2一起使用(谷歌应用程序引擎,用户。获取当前用户()
下面是示例python代码:Python g-signin2未与webapp2一起使用(谷歌应用程序引擎,用户。获取当前用户(),python,angularjs,google-app-engine,google-signin,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
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>
我能够将用户通过g-signin2默认按钮登录时获得的配置文件信息传递到我的后端。但是,当我使用users.get_current_user()时,我不会获得任何配置文件信息。如何使其工作?您混合了两种不同的身份验证机制:
google sign
登录,并将一些可能正确的信息(从该外部应用程序的角度)放入请求正文,这并不意味着用户也登录到您的GAE应用程序,该应用程序使用用户。从用户API获取当前用户()
对于用户。获取\u current\u user()
以不返回None
用户必须正确登录通过用户获得的URL。创建\u login\u URL()
多谢各位。让它工作起来。这些中哪一个更好?我看到webapp2登录会将用户带到另一个屏幕并重新加载,而g-signin2则不用重定向(我认为这样更好)。
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);
});
};
}]);