0

自從2011年11月起,我在ASP.NET應用程序中使用舊版本的Calendar GData API (v1, v2),允許用戶在提交用戶名和密碼後檢索和/或創建日曆活動,直到2014年11月17日之前谷歌決定關閉這個版本的API作爲宣佈Calendar GData API/Google Calendar Connectors deprecation我的Google身份驗證問題

現在我堅持的Google APIS Calendar (v3)的新版本,它迫使我使用認證過程的不同場景代替傳統之一。我完全不介意使用此版本的Calendar API,因爲它支持所有必需的功能,但現在我不知道如何處理多個用戶身份驗證,以便使用其每個用戶代碼控制檯註冊的用戶客戶端ID和祕密。

所以我的問題是:有沒有辦法讓用戶使用他/她的正常憑據登錄(通過用戶名/密碼或Google+ Sign Up功能),並繞過創建API項目,啓用所需的API和通過ASP.net代碼在控制檯內創建新的用戶憑證?

任何在C#ASP.net中製作的示例代碼都非常感謝。

編輯:這是我的驗證碼我用

public static CalendarService Authenticate() 
     { 
      CalendarService service; 
      GoogleAuthorizationCodeFlow flow; 
      string json_File = System.Configuration.ConfigurationManager.AppSettings["Authentication_Path"]; 
      string store_path = System.Configuration.ConfigurationManager.AppSettings["FileStore_Path"]; 
      string url = System.Configuration.ConfigurationManager.AppSettings["Authent_URL"]; 

      using (var stream = new FileStream(json_File, FileMode.Open, FileAccess.Read)) 
      { 
       flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer 
       { 
        DataStore = new FileDataStore(store_path), 
        ClientSecretsStream = stream, 
        Scopes = new[] { CalendarService.Scope.Calendar } 
       }); 
      } 
      var uri = url; 

      var result = new AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync("TRAININGCALENDAR", CancellationToken.None).Result; 
      if (result.Credential == null) 
      { 
       GoogleCalendar_Bus.Main_Authentication(url, "", ""); 
      } 
      // The data store contains the user credential, so the user has been already authenticated. 
      service = new CalendarService(new BaseClientService.Initializer 
      { 
       ApplicationName = "Calendar API Sample", 
       HttpClientInitializer = result.Credential 
      }); 
      if (result.Credential != null) 
      { 
       service = new CalendarService(new BaseClientService.Initializer 
       { 
        ApplicationName = "Calendar API Sample", 
        HttpClientInitializer = result.Credential 
       }); 
      } 

      return service; 
     } 

回答

2

不,你需要使用Oauth2。當他們進行身份驗證時,您只需保存刷新令牌,這將允許您獲得新的訪問令牌,並且您將再次訪問。您需要自己執行Idatastore以將這些刷新令牌存儲在數據庫中。

創建Idatastore的實現,存儲到數據庫是廣泛的,張貼在這裏,但你可以在這裏看到一個基本的例子代碼:DatabaseDataStore.cs

然後你可以這樣使用它。

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        new ClientSecrets { ClientId = _client_id 
             ,ClientSecret = _client_secret } 
        ,scopes 
        ,Environment.UserName 
        ,CancellationToken.None 
        ,new DatabaseDataStore(@"LINDAPC\SQL2012", "LindaTest", "test123", "test", "test")).Result; 

更新:現在我可以看到您的代碼。

  1. 請確保您擁有lanetted .net客戶端庫。 Google.Apis.Calendar.v3 Client Library
  2. 你的代碼正在使用FileDataStore這是你需要改變的。您需要自己創建與我創建的DatabaseDatastore類似的Idatastore。

你的代碼看起來和我通常做的不一樣。

string[] scopes = new string[] { 
     CalendarService.Scope.Calendar , // Manage your calendars 
     CalendarService.Scope.CalendarReadonly // View your Calendars 
      }; 

      try 
      { 
       // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData% 
       UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret } 
                    , scopes 
                    , userName 
                    , CancellationToken.None 
                    , new FileDataStore("Daimto.GoogleCalendar.Auth.Store")).Result; 



       CalendarService service = new CalendarService(new BaseClientService.Initializer() 
       { 
        HttpClientInitializer = credential, 
        ApplicationName = "Calendar API Sample", 
       }); 
       return service; 
      } 
      catch (Exception ex) 
      { 

       Console.WriteLine(ex.InnerException); 
       return null; 

      } 

這可能是由於你沒有使用最最新的客戶端的lib的事實,你可以找到谷歌日曆here一個示例控制檯應用程序遺憾的是該機還採用FileDatastore你必須編輯使用DatabaseDataStore。認證示例項目可以在這裏找到Google-Dotnet-Samples/Authentication/它顯示瞭如何創建自己的Idatastore實現。

我仍在編寫該示例項目的教程,希望能儘快完成。

+0

感謝DalmTo提供此信息,但它確實沒有任何意義,因爲Google通過用戶名和密碼關閉了正常的身份驗證過程,迫使每個用戶(甚至是新用戶訪問我的應用程序)創建一個API項目,並且用戶憑證開始使用Calendar API。那就是我的意思 !!!! – 2014-12-08 06:32:21