0

我使用谷歌自己的網站配置API。我想使用AppsService類來驗證用戶。使用異常Google.GData.Apps:遠程服務器返回錯誤:(503)服務器不可用

AppsService service = new AppsService("domain", "admin username", "admin password"); 
UserEntry user = service.RetrieveUser("username"); 

但它拋出異常:請求執行失敗:https://apps-apis.google.com/a/feeds/pmu.mygbiz.com/user/2.0/username

InnerException is: The remote server returned an error: (503) Server Unavailable.

據前6個月前的工作。

回答

0

ClientLogin(登錄名和密碼)已於2012年4月20日棄用,並於2015年5月26日關閉。此代碼不再需要切換到使用Oauth2。

我認爲你需要切換到admin目錄API以及

PM> Install-Package Google.Apis.Admin.Directory.directory_v1 

更新代碼:

我可以給你一些示例代碼,但我不能測試它100%我不沒有應用程序域。

輔助類:

class AuthenticationHelper 
    { 

     /// <summary> 
     /// Authenticate to Google Using Oauth2 
     /// Documentation https://developers.google.com/accounts/docs/OAuth2 
     /// </summary> 
     /// <param name="clientId">From Google Developer console https://console.developers.google.com</param> 
     /// <param name="clientSecret">From Google Developer console https://console.developers.google.com</param> 
     /// <param name="userName">A string used to identify a user.</param> 
     /// <returns></returns> 
     public static DirectoryService AuthenticateOauth(string clientId, string clientSecret, string userName) 
     { 

      // There are a lot of scopes check here: https://developers.google.com/admin-sdk/directory/v1/guides/authorizing 
      string[] scopes = new string[] { 
        DirectoryService.Scope.AdminDirectoryGroup , // Manage your Groups 
        DirectoryService.Scope.AdminDirectoryUser // Manage users 
        }; 

      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.AdminSDK.Auth.Store")).Result; 



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

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

      } 

     } 

     /// <summary> 
     /// Authenticating to Google using a Service account 
     /// Documentation: https://developers.google.com/accounts/docs/OAuth2#serviceaccount 
     /// </summary> 
     /// <param name="serviceAccountEmail">From Google Developer console https://console.developers.google.com</param> 
     /// <param name="keyFilePath">Location of the Service account key file downloaded from Google Developer console https://console.developers.google.com</param> 
     /// <returns></returns> 
     public static DirectoryService AuthenticateServiceAccount(string serviceAccountEmail, string keyFilePath) 
     { 

      // check the file exists 
      if (!File.Exists(keyFilePath)) 
      { 
       Console.WriteLine("An Error occurred - Key file does not exist"); 
       return null; 
      } 

      // There are a lot of scopes check here: https://developers.google.com/admin-sdk/directory/v1/guides/authorizing 
      string[] scopes = new string[] { 
        DirectoryService.Scope.AdminDirectoryGroup , // Manage your Groups 
        DirectoryService.Scope.AdminDirectoryUser // Manage users 
        }; 

      var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable); 
      try 
      { 
       ServiceAccountCredential credential = new ServiceAccountCredential(
        new ServiceAccountCredential.Initializer(serviceAccountEmail) 
        { 
         Scopes = scopes 
        }.FromCertificate(certificate)); 

       // Create the service. 
       DirectoryService service = new DirectoryService(new BaseClientService.Initializer() 
       { 
        HttpClientInitializer = credential, 
        ApplicationName = "Directory API Sample", 
       }); 
       return service; 
      } 
      catch (Exception ex) 
      { 

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

      } 
     } 
    } 

身份驗證:

var service = AuthenticationHelper.AuthenticateOauth("xxxxx-d0vpdthl4ms0soutcrpe036ckqn7rfpn.apps.googleusercontent.com", "NDmluNfTgUk6wgmy7cFo64RV", "userID"); 

提出一個要求:

try 
    { 
     var userList = service.Users.List(); 
     userList.MaxResults = 10; 
     userList.Execute(); 
    } 
    catch (Exception ex) 
    { 

     Console.WriteLine(ex.Message); 


    } 

    Console.ReadLine(); 

代碼從樣本項目Google-Dotnet-Samples/admin Directory

+0

感謝撕開。我試圖找到它的代碼(管理目錄API)在asp.net,但我沒有得到任何解決方案。你有任何鏈接? –

+0

我沒有關於這個api的教程,您可以嘗試按照我的Google Analytics(分析)教程,上面的代碼應該讓您開始,本教程將解釋它的功能。 http://www.daimto.com/googleanalytics-authentication-csharp/我無法測試它,因爲我沒有域帳戶。 – DaImTo

相關問題