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

Jak*_*rsh 3 google-api google-api-java-client angularjs google-oauth

我正在尝试使用Google API Javascript客户端在我的应用上使用Google登录,然后访问用户的电子邮件地址和联系人.我将它与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;
            };

        }]);
Run Code Online (Sandbox Code Playgroud)

然后我用控制器从控制器中调用以下内容:

        $scope.login = function () {
            googleLogin.handleAuthClick();
        };
Run Code Online (Sandbox Code Playgroud)

哪个有效,并且API被正确调用.但现在,我不确定如何通过服务从API获取数据.我需要获取用户的电子邮件地址以及他们的联系人列表.我不能只有具有get返回这些值的单独函数,因为似乎API客户端调用必须在链中进行(例如,handleAuthResult作为参数调用handleAuthClick).

我也尝试将它们设置为$rootScope值,或者只是普通变量,但是一旦控制器调用它们,它们总是变为undefined.

我接近这个吗?如何从Google API,服务到控制器获取这些变量?谢谢.

Jak*_*rsh 8

我最终使用了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;
            };

        }]);
Run Code Online (Sandbox Code Playgroud)

以及如何在控制器中调用它:

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