2012-08-16 4 views
11

Czy jest jakiś przykładowy kod aplikacji komputerowej, jak autoryzować usługę Dysk Google i przesłać plik?C# Aplikacja na komputer. Prosty przykład przesyłania pliku na Dysk Google

Obecnie mam:

var parameters = new OAuth2Parameters 
           { 
            ClientId = ClientCredentials.ClientId, 
            ClientSecret = ClientCredentials.ClientSecret, 
            RedirectUri = ClientCredentials.RedirectUris[0], 
            Scope = ClientCredentials.GetScopes() 
           };  
string url = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters); 
    // Open url, click to allow and copy secret code 
    parameters.AccessCode = secretCodeFromBrowser; 
    OAuthUtil.GetAccessToken(parameters); 
    string accessToken = parameters.AccessToken; 
    // So, there is the access token 

Ale jakie są kolejne kroki? Jak wynika z przykładów, powinienem dostać instancję IAuthenticator i przekazać ją do konstruktora klasy DriveService ... Jak uzyskać instancję IAuthenticator? Jeśli powyższy kod jest poprawny ... Z góry dziękuję.

+0

Możliwy duplikat: http: // stackoverflow .pl/questions/10317638/insertting-file-to-google-drive-through-api –

+0

Dla każdego, kto lubi zachować prostotę, napisałem prostą paczkę, która obsługuje płynne api dla poleceń napędu. [Github Repo] (https://github.com/Berkays/Drive.Net) – Berkays

Odpowiedz

19

Powyżej znajduje się pełna próbka wiersza polecenia w języku C#, aby przesłać plik do Dysku 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; 

namespace GoogleDriveSamples 
{ 
    class DriveCommandLineSample 
    { 
     static void Main(string[] args) 
     { 
      String CLIENT_ID = "YOUR_CLIENT_ID"; 
      String CLIENT_SECRET = "YOUR_CLIENT_SECRET"; 

      // Register the authenticator and create the service 
      var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET); 
      var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization); 
      { 
       Authenticator = auth 
      }); 

      File body = new File(); 
      body.Title = "My document"; 
      body.Description = "A test document"; 
      body.MimeType = "text/plain"; 

      byte[] byteArray = System.IO.File.ReadAllBytes("document.txt"); 
      System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray); 

      FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain"); 
      request.Upload(); 

      File file = request.ResponseBody; 
      Console.WriteLine("File id: " + file.Id); 
      Console.ReadLine(); 
     } 

     private static IAuthorizationState GetAuthorization(NativeApplicationClient arg) 
     { 
      // Get the auth URL: 
      IAuthorizationState state = new AuthorizationState(new[] { DriveService.Scopes.Drive.GetStringValue() }); 
      state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl); 
      Uri authUri = arg.RequestUserAuthorization(state); 

      // Request authorization from the user (by opening a browser window): 
      Process.Start(authUri.ToString()); 
      Console.Write(" Authorization Code: "); 
      string authCode = Console.ReadLine(); 
      Console.WriteLine(); 

      // Retrieve the access token by using the authorization code: 
      return arg.ProcessUserAuthorization(authCode, state); 
     } 
    } 
} 

UPDATE: ta próbka quickstart jest teraz dostępny w https://developers.google.com/drive/quickstart

+1

Wiem, że to trochę stary post. Ale próbowałem użyć twojego kodu do celów edukacyjnych. Ale obawiam się, że dostaję błąd kompilacji w 'var service = new DriveService (auth);'. Błąd kompilacji jest wyświetlany jako "Argument 1: nie można przekonwertować z" Google.Apis.Authentication.OAuth2.OAuth2Authenticator "na" Google.Apis.Services.BaseClientService.Initializer'. " Czy możesz w tym pomóc? – Sandy

+0

Ponadto jestem trochę zdezorientowany z kilkoma dodatkowymi koncepcjami i nie mogę wskoczyć im w głowę nawet po przeczytaniu w Internecie. Widziałem twój profil SO, 0 pytań i ponad 400 odpowiedzi. Byłbym świetny, gdybym mógł wziąć 5 minut na czat SO, aby wyjaśnić kilka moich nieporozumień. Wiem, że pytam za dużo teraz :) – Sandy

+0

Naprawiłem to, korzystałem z niewłaściwej wersji biblioteki dll. Teraz muszę pracować, aby usunąć inne moje pomyłki. – Sandy

Powiązane problemy