2013-12-16 42 views
1

我正在構建一個小的Angular應用程序,我正在使用Google Sign-In。我只是使用Google給出的教程中的示例代碼。谷歌登錄不識別登錄Angular的回調函數

   <span class="g-signin" 
       data-scope="https://www.googleapis.com/auth/plus.login" 
       data-clientid="blah" 
       data-redirecturi="postmessage" 
       data-accesstype="offline" 
       data-cookiepolicy="single_host_origin" 
       data-callback="signInCallback"> 
       </span> 

這使得按鈕登錄的元素,並運行此代碼開始

(function() { 
    var po = document.createElement('script'); 
    po.type = 'text/javascript'; 
    po.async = true; 
    po.src = 'https://plus.google.com/js/client:plusone.js?onload=start'; 
    var s = document.getElementsByTagName('script')[0]; 
    s.parentNode.insertBefore(po, s); 
})(); 

,所有這一切都是由谷歌複製。然後,我有這個方法在我的控制器,

.controller('splashController', ['$scope', '$window', function($scope, $window){ 
     $scope.googleLogin() = function(){ 
       $scope.googleAuthResult = $window.googleAuthResult; 
       console.log($scope.googleAuthResult); 
     }; 


     window.signInCallback = function(authResult){ 
       console.log("happy"); 
     }; 
}]) 

但問題是運行谷歌+腳本之後,它正在圍繞角世界之外這signInCallback功能,這是我要保持角度,因爲我想通過角度發送令牌,而不是像jQuery一樣。有什麼建議麼?

+0

我認爲這是因爲沒有注入正確的服務或讓Angular知道$ window或其他東西,但我無法弄清楚。 –

+0

你可以移動(函數()到控制器,或成.RUN()模塊?這可能會幫助。 –

+0

我試圖把匿名函數到運行模式,但它不會改變,谷歌+腳本要事實在Angular中看到一個函數...雖然我更喜歡run(),因爲它更乾淨。 –

回答

1

嘗試使用gapi.auth.authorize方法。它調用與按鈕相同的功能,但通過Javascript顯式調用,並且它還允許您在控制器/服務/範圍內傳遞迴調方法。這是我的登錄功能:

var authParams =     // Define a params object for gapi.auth.authorize 
{ 
    client_id  : '****-****.apps.googleusercontent.com', 
    scope   : 'https://www.googleapis.com/auth/userinfo.email', 
    immediate  : false, 
    cookiepolicy : 'single_host_origin' 
}; 

/** 
* Authenticates the user and authorizes the local browser's JS GAPI Client, returns an object 
* with the GAPI Client's login token or false if it's not yet available, and the deferred 
* object whose promise will be resolved when the JS GAPI Client authenticates. 
* 
* @param hard  If {{ hard }} is truthy, force the GAPI Client to reauthenticate the user 
*     and reauthorize the client. Ensures the client is logged in and the session 
*     stays active even if it is disrupted external to the application. 
*/ 
this.login = function(hard) 
{ 
    // @TODO: Consolidate into (!hard && (token || pending) if token can be guaranteed falsey 
    //   while pending otherwise ret.logged is unreliable. 
    //   Where's a logical XOR when you need one? 
    ////////////////////////// 
    // If a login is pending and we're not forcing a hard reauth, return the deferred object 
    // promising the authorization response and 'logged':false 
    if (pending && !hard) return { logged : false, deferred : deferred }; 

    // If the token is already set and we're not forcing a hard reauth, resolve the deferred 
    // promise immediately and return it along with 'logged':token 
    if (token && !hard) 
    { 
     deferred.resolve(token); // Resolve the promise immediately, no login response to wait for 
     return { logged : token, deferred : deferred }; 
    } 
    ///////////////////////// 

    deferred = $q.defer(); // We need to wait for gapi.auth.authorize to respond, reinit the promise 
    pending = true;   // Don't restart the process on future calls if an authorization is pending 
    gapi.auth.authorize(authParams,this.handleAuthResult); // Authorize the client, then run the callback 

    // Not logged in, return the deferred object 
    return { logged : false, deferred : deferred}; 
} 

所有包含在一項服務中。

+0

欲瞭解更多最新信息,請點擊這裏 - https://developers.google.com/api -client庫/ JavaScript的/開始/啓動JS 而對於JS客戶端庫的API參考在這裏 - https://developers.google.com/api-client-library/javascript/reference/referencedocs –