如何使用angularjs实现谷歌+登录

时间:2013-12-28 00:25:44

标签: angularjs google-plus

我有一个AngularJS应用,我想实现G +登录。我已经完成了他们的样本,他们作为独立的应用程序工作。

https://developers.google.com/+/web/signin/

在我的Angular应用程序中,我可以显示G +登录按钮。但我坚持回调。我是否将回调函数放在我的控制器js文件中?

如果是这样,给予这个控制器:

app.controller('myController', function ($scope) {
    function signinCallback(authResult) {

在我的数据回调中,如何将其命名为myController中的signinCallback?

    <span id="signinButton">
      <span
        class="g-signin"
        data-callback="signinCallback"
        data-clientid="123456789.apps.googleusercontent.com"
        data-cookiepolicy="single_host_origin"
        data-requestvisibleactions="http://schemas.google.com/AddActivity"
        data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read"
      </span>
    </span>

3 个答案:

答案 0 :(得分:7)

Google+ PhotoHunt示例应用程序演示了AngularJS与Google+的集成。该示例在Ruby,Java,Python和C#/ .NET for web中可用。 值得注意的是AngularJS前端的以下代码:

用于渲染按钮的标记:

<span id="signin" ng-show="immediateFailed">
  <span id="myGsignin"></span>
</span>

将标记粘贴到代码的JavaScript:

$scope.signIn = function(authResult) {
  $scope.$apply(function() {
    $scope.processAuth(authResult);
  });
}

$scope.processAuth = function(authResult) {
  $scope.immediateFailed = true;
  if ($scope.isSignedIn) {
    return 0;
  }
  if (authResult['access_token']) {
    $scope.immediateFailed = false;
    // Successfully authorized, create session
    PhotoHuntApi.signIn(authResult).then(function(response) {
      $scope.signedIn(response.data);
    });
  } else if (authResult['error']) {
    if (authResult['error'] == 'immediate_failed') {
      $scope.immediateFailed = true;
    } else {
      console.log('Error:' + authResult['error']);
    }
  }
}

$scope.renderSignIn = function() {
  gapi.signin.render('myGsignin', {
    'callback': $scope.signIn,
    'clientid': Conf.clientId,
    'requestvisibleactions': Conf.requestvisibleactions,
    'scope': Conf.scopes,
    'apppackagename': 'your.photohunt.android.package.name',
    'theme': 'dark',
    'cookiepolicy': Conf.cookiepolicy,
    'accesstype': 'offline'
  });
}

在processAuth中,您应该看到一个访问令牌,并且可以更新您的UI以反映这一点。您还可以看到the full controller's JavaScript code on GitHub

答案 1 :(得分:2)

我不确定这是否有效,但我会这样试试:

module.factory("GPlusAuthService", function ($q, $window) {
    var signIn;
    signIn = function () {
        var defered = $q.defer();
        $window.signinCallback = function (response) {
            $window.signinCallback = undefined;
            defered.resolve(response);
        };

        gapi.auth.signIn({
            clientid: "123456789.apps.googleusercontent.com"
            cookiepolicy: "single_host_origin"
            requestvisibleactions: "http://schemas.google.com/AddActivity"
            scope: "https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read",
            callback: "signinCallback"

        }) 
        return defered.promise;
    };
    return {
        signIn: signIn;
    }


});
module.controller('myController', function ($scope, GPlusAuthService) {
    $scope.signIn = function() {
        GPlusAuthService.signIn().then(function(response) {

        });    
    }
});


<span id="signinButton" ng-controller="myController">
   <span class="g-signin" ng-click="signIn()"></span>
</span>

答案 2 :(得分:0)

在用户同意登录后将要调用的函数在data-callback中指定,此函数需要全局可访问,绑定到window对象。 从控制器访问全局对象是一种反模式,作为中间结构,您可以使用Angular提供的$window,您可以在测试中模拟