使用.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凭据。
关于可能出现什么问题的任何想法?我记得在更新我的谷歌套餐后遇到了类似的问题,但是在这个例子中我并没有这样做。我也尝试更新软件包,但仍然遇到了同样的问题。
答案 0 :(得分:0)
问题是我没有正确的授权范围。原来我有:
Scopes = new string[] { ScriptService.Scope.Forms, ScriptService.Scope.Spreadsheets,
ScriptService.Scope.Drive, YouTubeService.Scope.YoutubeUpload, YouTubeService.Scope.Youtube };
然而,在检查&#34;范围&#34;我的应用脚本项目属性中的选项卡,我错过了&#34; userinfo.email&#34;范围。所以,我按以下方式更新了我的代码:
Scopes = new string[] { ScriptService.Scope.Forms, ScriptService.Scope.Spreadsheets,
ScriptService.Scope.Drive, ScriptService.Scope.UserinfoEmail, YouTubeService.Scope.YoutubeUpload, YouTubeService.Scope.Youtube };
我的猜测是,自从几个月前我最初编写脚本以来,API已经更新,因为我当时能够使用我拥有的三个原始授权成功运行它。