使用Google API将变量从AngularJS服务传递到控制器?

使用Google API将变量从AngularJS服务传递到控制器?,angularjs,google-api,google-oauth,google-api-java-client,Angularjs,Google Api,Google Oauth,Google Api Java Client,我正在尝试使用谷歌API Javascript客户端在我的应用程序上使用谷歌登录,然后访问用户的电子邮件地址和联系人。我将此与AngularJS相结合,并且我已经读到最好将其作为自己的服务 以下是迄今为止该服务的代码: .service('googleLogin', ['$http', '$rootScope', function ($http, $rootScope) { var clientId = '{MY CLIENT KEY}',

我正在尝试使用谷歌API Javascript客户端在我的应用程序上使用谷歌登录,然后访问用户的电子邮件地址和联系人。我将此与AngularJS相结合,并且我已经读到最好将其作为自己的服务

以下是迄今为止该服务的代码:

.service('googleLogin', ['$http', '$rootScope', function ($http, $rootScope) {
            var clientId = '{MY CLIENT KEY}',
                apiKey = '{MY API KEY}',
                scopes = 'https://www.googleapis.com/auth/userinfo.email https://www.google.com/m8/feeds',
                domain = '{MY COMPANY DOMAIN}';

            this.handleClientLoad = function () {
                // Step 2: Reference the API key
                gapi.client.setApiKey(apiKey);
                gapi.auth.init(function () { });
                window.setTimeout(checkAuth, 1);
            };

            this.checkAuth = function() {
                gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: true, hd: domain }, this.handleAuthResult );
            };

            this.handleAuthResult = function(authResult) {
                if (authResult && !authResult.error) {
                    gapi.client.load('oauth2', 'v2', function () {
                        var request = gapi.client.oauth2.userinfo.get();
                        request.execute(function (resp) {
                            console.log(userEmail);
                        });
                    });
                }
            };

            this.handleAuthClick = function (event) {
                // Step 3: get authorization to use private data
                gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: false, hd: domain }, this.handleAuthResult );
                return false;
            };

        }]);
然后,我通过以下命令从控制器调用此命令:

        $scope.login = function () {
            googleLogin.handleAuthClick();
        };
它工作正常,并且API被正确调用。但是现在,我不知道如何通过服务从API获取数据。我需要获得用户的电子邮件地址,以及他们的联系人列表。我不能让单独的
get
函数返回这些值,因为似乎API客户端调用必须在链中进行(例如,
handleAuthResult
handleAuthClick
中作为参数调用)

我还尝试将它们设置为
$rootScope
值,或者仅仅是普通变量,但一旦控制器调用它们,它们总是作为
未定义的
出现


我的做法正确吗?如何从googleapi、服务和控制器获取这些变量?谢谢。

我最终使用了angularJS提供的promise/defer实现,有文档记录

这是最后的服务:

.service('googleLogin', ['$http', '$rootScope', '$q', function ($http, $rootScope, $q) {
            var clientId = '{MY CLIENT ID}',
                apiKey = '{MY API KEY}',
                scopes = 'https://www.googleapis.com/auth/userinfo.email https://www.google.com/m8/feeds',
                domain = '{MY COMPANY DOMAIN}',
                userEmail,
                deferred = $q.defer();

            this.login = function () {
                gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: false, hd: domain }, this.handleAuthResult);

                return deferred.promise;
            }

            this.handleClientLoad = function () {
                gapi.client.setApiKey(apiKey);
                gapi.auth.init(function () { });
                window.setTimeout(checkAuth, 1);
            };

            this.checkAuth = function() {
                gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: true, hd: domain }, this.handleAuthResult );
            };

            this.handleAuthResult = function(authResult) {
                if (authResult && !authResult.error) {
                    var data = {};
                    gapi.client.load('oauth2', 'v2', function () {
                        var request = gapi.client.oauth2.userinfo.get();
                        request.execute(function (resp) {
                            $rootScope.$apply(function () {
                                data.email = resp.email;
                            });
                        });
                    });
                    deferred.resolve(data);
                } else {
                    deferred.reject('error');
                }
            };

            this.handleAuthClick = function (event) {
                gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: false, hd: domain }, this.handleAuthResult );
                return false;
            };

        }]);
以及它当前在控制器中的调用方式:

var promise = googleLogin.login();
                promise.then(function (data) {
                    console.log(data.email);
                }, function (reason) {
                    console.log('Failed: ' + reason);
                });

handleClientLoad
方法的作用是什么?看起来这就是初始化例程。当单例服务加载时会自动调用吗?@JimG。我很久以前就写过这段代码,它看起来确实像是一个初始化例程,但它肯定不会在任何地方被调用。可能是多余的/不必要的?