2

所有的代碼運行沒有錯誤,但當我檢查我的谷歌雲端硬盤帳戶我找不到我上傳的文件(「document.txt」)。無法上傳文件到谷歌驅動器 - 使用c#

此外它又要求我再次進行身份驗證。

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    new ClientSecrets 
    { 
     ClientId = "Here my clientid", 
      ClientSecret = "client secret", 
    }, 
    new[] { DriveService.Scope.Drive }, 
    "user", 
    CancellationToken.None).Result; 

// Create the service. 
var service = new DriveService(new BaseClientService.Initializer() 
{ 
    HttpClientInitializer = credential, 
    ApplicationName = "Drive API Sample", 
}); 

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; 

問題: 爲什麼不能找到我上傳的文件,我怎麼能得到它記住我的身份驗證。

回答

0

我想你忘了body.Parent所以它不知道該文件放在什麼目錄下。

parents[] list Collection of parent folders which contain this file. Setting this field will put the file in all of the provided folders. On insert, if no folders are provided, the file will be placed in the default root folder.

例如:

body.Parents = new List<ParentReference>() { new ParentReference() { Id = 'root' } }; 

你正在要求再次驗證,因爲你不節能認證。

//Scopes for use with the Google Drive API 
string[] scopes = new string[] { DriveService.Scope.Drive, 
           DriveService.Scope.DriveFile}; 
// 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 = CLIENT_ID 
                  , ClientSecret = CLIENT_SECRET } 
              ,scopes 
              ,Environment.UserName 
              ,CancellationToken.None 
              ,new FileDataStore("Daimto.GoogleDrive.Auth.Store") 
             ).Result; 

FileDataStore將認證數據存儲在%appdata%目錄中。

更詳細的信息可以在教程Google Drive API with C# .net – Upload

更新對於下面的錯誤中找到:

"The API is not enabled for your project, or there is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your configuration. [403]"

轉到開發者控制檯爲您的項目here在APIs &權威性 - > API使得谷歌驅動API和sdk。還要去憑據,並確保您添加了產品名稱和電子郵件。

+0

感謝您的回覆。 我會做建議的變化 – 2014-12-03 11:51:36

+0

你可以找到示例項目沿着這裏https://github.com/LindaLawton/Google-Dotnet-Samples/tree/master/Google-Drive – DaImTo 2014-12-03 11:57:03

+0

偉大的教程。讓我檢查一下。 – 2014-12-03 11:59:43

相關問題