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

相关问题