谷歌登录无法识别登录 角度中的回调功能
Google Sign In not recognizing sign in Callback function in Angular
我正在构建一个小型的Angular应用程序,并且正在使用Google登录。我只是使用谷歌提供的教程中的示例代码。
<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");
};
}])
但问题是,在 google+ 脚本运行后,它会在 Angular World 之外寻找这个 signInCallback 函数,我想将其保留在 Angular 中,因为我想通过 angular 发送令牌,而不是像 jQuery 那样。有什么建议吗?
尝试使用 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};
}
所有这些都包含在服务中。
相关文章:
- 谷歌登录无法识别登录 角度中的回调功能
- 用于登录和注销功能的Backbone.js架构
- 是否有任何twitter功能用于登录应用程序
- 谷歌登录成功功能在我阻止第三方cookie时没有任何作用
- 我有三个地方供用户登录,在同一页面上,我如何在一个功能中让所有三个按钮工作和三个表单处理
- 剑道UI登录功能
- 登录状态指示灯功能不起作用
- 两个登录/注册按钮是相同的.如何为同一个按钮提供 2 个功能
- 可以'在Mocha功能之前,无法获取登录的代理
- 我们有一个登录的基本登录功能,但随后它突然又回到登录屏幕
- Javascript基本登录功能
- Facebook登录未定义功能
- IndexedDb登录功能
- 如果用户已登录,禁用某些功能
- 角,Loopback:用户.登录不是一个功能
- 登录功能没有'我不在firefox工作,但在chrome、IE、safari和opera上工作
- XBMC (Python)的简单登录功能问题
- Facebook登录功能没有;t在登录后执行任何操作
- 是否需要检查每个控制器页面中的登录功能
- 用c语言创建登录功能cgi-html.(没有PHP, Apache)