2014-10-01 159 views
0

我有一個顯示Google日曆數據的應用程序,但它需要初始登錄。我知道可以使用OAuth 2.0存儲令牌,但我不知道如何去做。以下是我的代碼如下。我希望網頁在沒有登錄的情況下使用來自Google日曆的JSON數據顯示日曆。以角度存儲OAuth 2.0的令牌

控制器

angular.module('demo', ["googleApi"]) 
.config(function(googleLoginProvider) { 
    googleLoginProvider.configure({ 
     clientId: '239511214798.apps.googleusercontent.com', 
     scopes: ["https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/calendar", "https://www.googleapis.com/auth/plus.login"] 
    }); 
}) 
.controller('DemoCtrl', ['$scope', 'googleLogin', 'googleCalendar', 'googlePlus', function ($scope, googleLogin, googleCalendar, googlePlus) { 

    $scope.login = function() { 
     googleLogin.login(); 
    }; 

    $scope.$on("googlePlus:loaded", function() { 
     googlePlus.getCurrentUser().then(function(user) { 
     $scope.currentUser = user; 
     }); 
    }) 
    $scope.currentUser = googleLogin.currentUser; 

    $scope.loadEvents = function() { 
     this.calendarItems = googleCalendar.listEvents({calendarId: this.selectedCalendar.id}); 
    } 

    $scope.loadCalendars = function() { 
     $scope.calendars = googleCalendar.listCalendars(); 
    } 

}]); 

googleAPi

angular.module('googleApi', []) 
.value('version', '0.1') 

.service("googleApiBuilder", function($q) { 
    this.loadClientCallbacks = []; 

    this.build = function(requestBuilder, responseTransformer) { 
     return function(args) { 
      var deferred = $q.defer(); 
      var response; 
      request = requestBuilder(args); 
      request.execute(function(resp, raw) { 
       if(resp.error) { 
        deferred.reject(resp.error); 
       } else { 
        response = responseTransformer ? responseTransformer(resp) : resp; 
        deferred.resolve(response); 
       } 

      }); 
      return deferred.promise; 

     } 
    }; 

    this.afterClientLoaded = function(callback) { 
     this.loadClientCallbacks.push(callback); 
    }; 

    this.runClientLoadedCallbacks = function() { 
     for(var i=0; i < this.loadClientCallbacks.length; i++) { 
      this.loadClientCallbacks[i](); 
     } 
    }; 
}) 

.provider('googleLogin', function() { 

    this.configure = function(conf) { 
     this.config = conf; 
    }; 

    this.$get = function ($q, googleApiBuilder, $rootScope) { 
     var config = this.config; 
     var deferred = $q.defer(); 
     return { 
      login: function() { 
       gapi.auth.authorize({ client_id: config.clientId, scope: config.scopes, immediate: false}, this.handleAuthResult); 

       return deferred.promise; 
      }, 

      handleClientLoad: function() { 
       gapi.auth.init(function() { }); 
       window.setTimeout(checkAuth, 1); 
      }, 

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

      handleAuthResult: function(authResult) { 
       if (authResult && !authResult.error) { 
        var data = {}; 
        $rootScope.$broadcast("google:authenticated", authResult); 
        googleApiBuilder.runClientLoadedCallbacks(); 
        deferred.resolve(data); 
       } else { 
        deferred.reject(authResult.error); 
       } 
      }, 
     } 
    }; 


}) 

.service("googleCalendar", function(googleApiBuilder, $rootScope) { 

    var self = this; 
    var itemExtractor = function(resp) { return resp.items; }; 

    googleApiBuilder.afterClientLoaded(function() { 
     gapi.client.load('calendar', 'v3', function() { 

      self.listEvents = googleApiBuilder.build(gapi.client.calendar.events.list, itemExtractor); 
      self.listCalendars = googleApiBuilder.build(gapi.client.calendar.calendarList.list, itemExtractor); 
      self.createEvent = googleApiBuilder.build(gapi.client.calendar.events.insert); 

      $rootScope.$broadcast("googleCalendar:loaded") 
     }); 

    }); 

}) 

    .service("googlePlus", function(googleApiBuilder, $rootScope) { 

      var self = this; 
      var itemExtractor = function(resp) { return resp.items; }; 

      googleApiBuilder.afterClientLoaded(function() { 
        gapi.client.load('plus', 'v1', function() { 
         self.getPeople = googleApiBuilder.build(gapi.client.plus.people.get); 
         self.getCurrentUser = function() { 
          return self.getPeople({userId: "me"}); 
         } 
         $rootScope.$broadcast("googlePlus:loaded") 
        }); 

      }); 

    }) 
+0

你問如何使用硬編碼的身份驗證令牌?或者你想不顯示用戶登錄日曆?如果是後者,我不會看到有什麼辦法可以做到這一點不幸。 – 2014-10-02 00:08:37

+0

從我所做的研究來看,硬編碼不適用於OAuth系統。我的理解是有一種方法可以在不使用硬編碼的情況下存儲令牌。 – byrdr 2014-10-02 00:10:51

+0

這取決於你的意思。存儲令牌絕對適用於OAuth 2.0,但OAuth 1.0具有必須在每個請求上生成的令牌。你是這個意思嗎? – 2014-10-02 00:14:10

回答

0

什麼,你會想要做的就是結果回來後,你將要關閉其保存到localStorage的或一個cookie,然後使用在未來如果存在的話。

從本質上講,你將需要更新您的handleAuthResult的結果從谷歌API存儲:

 handleAuthResult: function (authResult) { 
      if (authResult && !authResult.error) { 
       var data = {}; 
       $rootScope.$broadcast("google:authenticated", authResult); 
       googleApiBuilder.runClientLoadedCallbacks(); 

       // here you will store the auth_token 
       window.localStorage.setItem('auth_token', authResult.token /*I don't know what this response looks like, but it should be similar to this*/); 

       deferred.resolve(data); 
      } else { 
       deferred.reject(authResult.error); 
      } 
     }, 

Live Demo

+0

謝謝。也許我希望完成的事情是不可能的。我希望完全避免登錄,以便我可以訪問私人Google日曆中的數據,並使用發回的JSON數據在網站上構建日曆。 – byrdr 2014-10-02 02:06:19

+0

啊好吧是的,我無法想象這是可能的。否則,爲什麼他們需要登錄? :) – 2014-10-02 16:01:43