2017-04-08 27 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不用重定向(我認爲這更好)。 –