2012-11-06 17 views
1

我需要檢索Google硬盤登錄的用戶憑證。假設我有一個在谷歌驅動器的帳戶,我想檢索用戶信息 - 我怎樣才能在C#中工作?檢索Google硬盤登錄的用戶憑證

當我運行下面的代碼,我得到的錯誤:

using System; 
using System.Diagnostics; 
using DotNetOpenAuth.OAuth2; 
using Google.Apis.Authentication.OAuth2; 
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth; 
using Google.Apis.Drive.v2; 
using Google.Apis.Drive.v2.Data; 
using Google.Apis.Util; 
using Google.Apis.Requests; 
using System.Collections.Generic; 
using System.Net; 
using Google.GData.Client; 
using Google.GData.Documents; 
using Google.GData.Extensions; 
using Google.Apis.Oauth2.v2; 
using Google.Apis.Oauth2.v2.Data; 
using Google.Apis.Authentication; 
namespace google_drive_iterate_folders 
{ 
    class Program 
    { 

    static DriveService BuildService(IAuthenticator credentials) 
    { 
     return new DriveService(credentials); 
    } 


    public static IAuthenticator GetCredentials(String authorizationCode, String state) 
    { 
     String emailAddress = ""; 
     try 
     { 
      IAuthorizationState credentials = ExchangeCode(authorizationCode); 
      Userinfo userInfo = GetUserInfo(credentials); 
      String userId = userInfo.Id; 
      emailAddress = userInfo.Email; 
      if (!String.IsNullOrEmpty(credentials.RefreshToken)) 
      { 
       StoreCredentials(userId, credentials); 
       return GetAuthenticatorFromState(credentials); 
      } 
      else 
      { 
       credentials = GetStoredCredentials(userId); 
       if (credentials != null && 
        !String.IsNullOrEmpty(credentials.RefreshToken)) 
       { 
        return GetAuthenticatorFromState(credentials); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("An error occurred during code exchange."); 
      // Drive apps should try to retrieve the user and credentials for 
      // the current session. 
      // If none is available, redirect the user to the authorization URL. 
      e.AuthorizationUrl = GetAuthorizationUrl(emailAddress, state); 
      throw e; 
     } 
     //catch() 
     //{ 
     // Console.WriteLine("No user ID could be retrieved."); 
     //} 
     // No refresh token has been retrieved. 
     String authorizationUrl = GetAuthorizationUrl(emailAddress, state); 
     throw new NoRefreshTokenException(authorizationUrl); 
    } 

    /// <summary> 
    /// Exchange an authorization code for OAuth 2.0 credentials. 
    /// </summary> 
    /// <param name="authorizationCode">Authorization code to exchange 
    //// for OAuth 2.0 credentials.</param> 
    /// <returns>OAuth 2.0 credentials.</returns> 
    /// <exception cref="CodeExchangeException">An error occurred.</exception> 
    static IAuthorizationState ExchangeCode(String authorizationCode) 
    { 
     var provider = new NativeApplicationClient(
      GoogleAuthenticationServer.Description, 
      ClientCredentials.CLIENT_ID, 
      ClientCredentials.CLIENT_SECRET); 
     IAuthorizationState state = new AuthorizationState(); 
     state.Callback = new Uri(ClientCredentials.REDIRECT_URI); 
     try 
     { 
      state = provider.ProcessUserAuthorization(authorizationCode, state); 
      return state; 
     } 
     catch (ProtocolException) 
     { 
      throw new CodeExchangeException(null); 
     } 
    } 

    /// <summary> 
    /// Send a request to the User Info API to retrieve the user's information. 
    /// </summary> 
    /// <param name="credentials">OAuth 2.0 credentials to authorize 
    //// the request.</param> 
    /// <returns>User's information.</returns> 
    /// <exception cref="NoUserIdException">An error occurred.</exception> 
    static Userinfo GetUserInfo(IAuthenticator credentials) 
    { 
     Oauth2Service userInfoService = new Oauth2Service(credentials); 
     Userinfo userInfo = null; 
     try 
     { 
      userInfo = userInfoService.Userinfo.Get().Fetch(); 
     } 
     catch (GoogleApiRequestException e) 
     { 
      Console.WriteLine("An error occurred: " + e.Message); 
     } 

     if (userInfo != null && !String.IsNullOrEmpty(userInfo.Id)) 
     { 
      return userInfo; 
     } 
     else 
     { 
      throw new NoUserIdException(); 
     } 
    } 

    /// <summary> 
    /// Retrieve an IAuthenticator instance using the provided state. 
    /// </summary> 
    /// <param name="credentials">OAuth 2.0 credentials to use.</param> 
    /// <returns>Authenticator using the provided OAuth 2.0 
    /// credentials</returns> 
    public static IAuthenticator GetAuthenticatorFromState(
     IAuthorizationState credentials) 
    { 
     var provider = new StoredStateClient(
      GoogleAuthenticationServer.Description, ClientCredentials.CLIENT_ID, 
      ClientCredentials.CLIENT_SECRET, credentials); 
     var auth = new OAuth2Authenticator<StoredStateClient>(
      provider, StoredStateClient.GetState.. 

回答

1

谷歌驅動器SDK文檔包含了完整的.NET示例,演示如何在數據庫中存儲的憑據,並恢復他們在需要的時候:

檢查了這一點: - https://developers.google.com/drive/examples/dotnet

+0

請看看上面的代碼,讓我知道我在哪裏做錯了 – Sujit

0

OK,有一個快看......你缺少另一個用指令(雖然我很驚訝你在運行時錯誤而不是編譯的時候,想必你的IDE可以告訴你這一點。 ..)。

我不能確定userinfo屬於哪個命名空間,但是看着this link,我只能看到你的谷歌命名空間缺失。儘管如此,我並不真的期望它那裏...

無論如何,谷歌是你的朋友。