2016-05-23 108 views
1

我试图使用v3 API上传Google云端硬盘中的文件,但我不明白如何获取访问权限。Google云端硬盘API v3 C#SDK无效凭证

我已经通过已位于的OAuth2获得的令牌(我为授予访问权限的用户谷歌驱动器的请求,并获得令牌)

有我的代码,但我不知道如何把令牌的认证。

//creazione servizio 
var DriveService = new Google.Apis.Drive.v3.DriveService(); 

//creazione del file 
Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File(); 
body.Name = nome; 
body.MimeType = mimeType; 

//upload del file 
Google.Apis.Drive.v3.FilesResource.CreateMediaUpload request = DriveService.Files.Create(body,file,mimeType); 
request.OauthToken = token; 
request.Fields = "id"; 
request.Upload(); 

Response.Write(request.Upload().Exception.Message); 

var returnFile = request.ResponseBody; 

if (returnFile != null) 
{ 
    esito.urlFile = returnFile.Id; 
    esito.esito = true; 
} 

在此先感谢大家。

回答

1

如果您使用的访问令牌已过期或无效(如Handling API Errors中所述),通常会遇到您在SO帖子标题中指示的凭据无效。

建议此类错误的操作是使用长期刷新令牌刷新访问令牌。如果失败,请指导用户通过OAuth流程,如Authorizing Your App with Google Drive中所述。

我发现它的帮助,你会明白有关身份验证和上传文件更好,如果你去通过这些教程:

  1. Google Drive Authentication C#
  2. Google Drive API with C# .net – Upload

希望这些教程帮助。 :)

+0

谢谢Teyam,你帮了我很大!我不确定指定令牌的位置,但现在我确定这一行是“request.OauthToken = token;”是对的。 问题在于您指出的令牌。检索刷新令牌的方法中存在一个错误。 我发布的代码运行良好。 谢谢! – user2950363

相关问题