【问题标题】:"Google.GoogleApiException: Google.Apis.Requests.RequestError Request is missing required authentication credential" when using ScriptService使用 ScriptService 时“Google.GoogleApiException:Google.Apis.Requests.RequestError 请求缺少所需的身份验证凭据”
【发布时间】:2018-03-16 11:04:01
【问题描述】:

使用 .net,我正在尝试向 Google ScriptService 发出请求,但是我不断收到此错误“请求缺少所需的身份验证凭据”,尽管我包含了凭据。事实上,不久前我使用相同的凭据成功向 YoutubeService 发出请求。

下面是我的代码,它实际上曾经可以工作,所以我不确定发生了什么变化:

Scopes = new string[] { ScriptService.Scope.Forms, ScriptService.Scope.Spreadsheets,
            ScriptService.Scope.Drive, YouTubeService.Scope.YoutubeUpload, YouTubeService.Scope.Youtube };

UserCredential credential;
using (var stream = new FileStream(@"Resources\client_secret.json", FileMode.Open, FileAccess.Read))
{
    var credPath = Path.Combine(parentDir, ".credentials/" + folderName);

    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
         GoogleClientSecrets.Load(stream).Secrets,
         Scopes,
         "user",
         CancellationToken.None,
         new FileDataStore(credPath, true)).Result;
}

// Create Google Apps Script Execution API service.
var service = new ScriptService(new BaseClientService.Initializer()
{
    HttpClientInitializer = this.credential,
    ApplicationName = Properties.Resources.ApplicationName,
 });

// Create an execution request object.
ExecutionRequest request = new ExecutionRequest();
request.Function = "createForm";
request.Parameters = new List<object>();
request.Parameters.Add(this.id);
request.Parameters.Add(name);
request.Parameters.Add(email);
request.Parameters.Add(this.link);

ScriptsResource.RunRequest runReq = service.Scripts.Run(request, Globals.Script_ID);

try
{
    // Make the API request.
    Operation op = runReq.Execute();
catch (Google.GoogleApiException e)
{
    Debug.WriteLine("Error calling API:\n{0}", e.ToString());
}

我已在开发者控制台中启用 API 并为我的平台生成 OAuth 2.0 凭据。 client_secret.json 是我从控制台下载的 OAuth 2.0 凭据。

对可能出现的问题有任何想法吗?我记得在更新我的谷歌包后遇到了类似的问题,但是在这种情况下我没有这样做。我也尝试更新软件包,但仍然遇到同样的问题。

【问题讨论】:

  • 听起来您需要更新您请求的授权范围,或者您可能需要执行授权流程。当您将 Execution API 添加到您的项目时,您是否包含任何新范围?
  • 这确实是我的问题,谢谢!我以为我添加了必要的范围,因为这以前有效,但再次检查后,我错过了 UserInfoEmail 授权范围。
  • 将解决方案发布为答案并接受它(即比较范围定义的之前/之后)

标签: c# .net google-apps-script oauth-2.0 google-oauth


【解决方案1】:

问题是我没有正确的授权范围。原来我有:

Scopes = new string[] { ScriptService.Scope.Forms, ScriptService.Scope.Spreadsheets,
        ScriptService.Scope.Drive, YouTubeService.Scope.YoutubeUpload, YouTubeService.Scope.Youtube };

但是,在我的应用程序脚本项目属性中检查“范围”选项卡时,我错过了“userinfo.email”范围。因此,我通过以下方式更新了我的代码:

Scopes = new string[] { ScriptService.Scope.Forms, ScriptService.Scope.Spreadsheets,
        ScriptService.Scope.Drive, ScriptService.Scope.UserinfoEmail, YouTubeService.Scope.YoutubeUpload, YouTubeService.Scope.Youtube };

我的猜测是,自从几个月前我最初编写脚本以来,API 已经更新,因为当时我能够使用我拥有的三个原始授权成功运行它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 2019-06-07
    • 2018-02-09
    • 1970-01-01
    • 2019-09-12
    • 2014-05-08
    • 2020-05-08
    相关资源
    最近更新 更多