2017-08-28 72 views
0

我的應用程序有以下問題: 我想上傳簡單的文件到谷歌驅動器,沒有錯誤,但似乎沒有工作,因爲沒有文件beeing上傳。 我已經啓用在谷歌的API,增加如何在不打開瀏覽器窗口的情況下上傳Google Drive v3中的文件?

using Google.Apis.Drive.v3.Data; 
using Google.Apis.Upload; 

到我的項目,但我不能出去,爲什麼它不會工作。 我試過不同的解決方案,由谷歌提供,但他們都沒有工作。 獲得所需要的信息,我用

using Google.Apis.Auth.OAuth2; 
using Google.Apis.Drive.v3; 
using Google.Apis.Services; 
using Google.Apis.Util.Store; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Threading; 
using System.Net; 
// If modifying these scopes, delete your previously saved credentials 
    // at ~/.credentials/drive-dotnet-quickstart.json 
    static string[] Scopes = { DriveService.Scope.DriveReadonly }; 
    static string ApplicationName = "Drive API .NET Quickstart"; 

    static void Main(string[] args) 
    { 
     UserCredential credential; 
     WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials; 

     using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read)) 
     { 
      string credPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
      credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json"); 

      credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
       GoogleClientSecrets.Load(stream).Secrets, 
       Scopes, 
       "user", 
       CancellationToken.None, 
       new FileDataStore(credPath, true), new LocalServerCodeReceiver()).Result; 
      Console.WriteLine("Credential file saved to: " + credPath); 
     } 

     // Create Drive API service. 
     // new Google.Apis.Drive.v2.DriveService() 
     var service = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer() 
     { 
      HttpClientInitializer = credential, 
      ApplicationName = ApplicationName, 
     }); 
     // Define parameters of request. 
     Google.Apis.Drive.v3.FilesResource.ListRequest listRequest = service.Files.List(); 
     // listRequest.PageSize = 10; 
     listRequest.Fields = "nextPageToken, files(id, name)"; 

     //Upload File 
     gUpload(service, @"C:\Users\xxx\test.txt"); //THIS DOES NOT WORK 

     // List files. 
     IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute().Files; 
     Console.WriteLine("Files:"); 
     if (files != null && files.Count > 0) 
     { 
      foreach (Google.Apis.Drive.v3.Data.File file in files) 
      { //THIS WORKS FINE 
       Console.WriteLine("{0} ({1})", file.OriginalFilename, file.Id); 
      } 
     } 
     else 
     { 
      Console.WriteLine("No files found."); 
     } 

     Console.ReadLine(); 
    } 

這會給我包含在我的GDrive的文件夾以及它的垃圾文件,所有文件的正確憑證和清單。

的另一種方法,添加文件,而不是列出它們不反正工作:

public static void TestUpload(string filePath, DriveService driveService) 
    { 
     if (System.IO.File.Exists(filePath.ToString())) 
     { 
      File body = new File(); 
      body.Name = System.IO.Path.GetFileName(filePath.ToString()); 
      body.Description = "Test Description"; 
      body.MimeType = "text/plain"; 
      byte[] byteArray = System.IO.File.ReadAllBytes(filePath); 

      System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray); 
      FilesResource.CreateMediaUpload request = driveService.Files.Create(body, stream, "text/plain"); 
      request.Upload(); 
      File file = request.ResponseBody; //returns null value 
     } 

    } 

這種運行沒有任何錯誤,但請求爲空,沒有什麼上傳,我的其他改掉的一個運行作爲以及沒有錯誤,但沒有上傳任何東西。

public static bool gUpload(Google.Apis.Drive.v3.DriveService service, string filePath) 
    { 

     string fileID = "0B-W1aRTTOB1QM3hPd0dOUFVObHM"; //random 
     System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open); 
     File fBody = new File { Name = "test.txt" }; 

     var r = service.Files.Update(fBody, fileID, stream, "application/octet-stream"); 
     r.ChunkSize = ResumableUpload.MinimumChunkSize; 
     r.Resume(); 
     // UploadStatus.Completed 
     return true; 
    } 

我希望有人在那裏給我一個關於如何更改代碼以獲得上傳工作的工作答案。

問候 傑克

回答

1

看起來你正在使用錯誤的範圍,應該是DriveService.Scope.Drive(但也可能是多餘的)。當您更新範圍列表時,也不要忘記從中刪除緩存的憑據。您可以檢查示波器here

相關問題