2017-04-08 24 views
0

所以,这是样品Python代码的工作:克-signin2不与webapp2的(谷歌应用程序引擎,users.get_current_user())

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 

“电子邮件”打印正确的登录用户(其中意味着我的G-signin2工作),但“用户”变量为空(这意味着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> 

以下是我的角度指令(大多irreleven吨至这个问题,但无论如何,只需添加这是肯定):

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

你混合2个不同的身份验证机制:

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

对于users.get_current_user()有来无回None用户必须正确登录信息在通过users.create_login_url()获得的URL。

+0

谢谢。得到它的工作。哪个更好?我看到webapp2登录将用户带到另一个屏幕并重新加载,而g-signin2不用重定向(我认为这更好)。 –