【问题标题】:Google Drive API v3 C# SDK Invalid CredentialsGoogle Drive API v3 C# SDK 凭据无效
【发布时间】:2016-05-23 18:51:03
【问题描述】:

我正在尝试使用 v3 API 在 Google Drive 中上传文件,但我不明白如何获得访问权限。

我已经获得了 OAuth2 获得的令牌(我请求授予用户对 Google Drive 的访问权限并获取令牌)

有我的代码,但我不明白如何放置令牌进行身份验证。

//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;
}

提前感谢大家。

【问题讨论】:

    标签: c# google-drive-api credentials


    【解决方案1】:

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

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

    我觉得它很有帮助,如果您阅读这些教程,您将更好地了解身份验证和上传文件:

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

    希望这些教程有所帮助。 :)

    【讨论】:

    • 谢谢 Teyam,你帮了我很大的忙!我不确定在哪里指定令牌,但现在我确定这一行“request.OauthToken = token;”是正确的。问题是您指出的令牌。检索刷新的令牌的方法中存在错误。我发布的代码运行良好。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 2014-09-20
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多