C# 尝试创建新事件时出现身份验证作用域不足错误
我想用C#在Google日历中创建一个事件。我在编程的第一阶段,但我想尝试解决这个问题 守则:C# 尝试创建新事件时出现身份验证作用域不足错误,c#,google-api,google-oauth,google-calendar-api,google-api-dotnet-client,C#,Google Api,Google Oauth,Google Calendar Api,Google Api Dotnet Client,我想用C#在Google日历中创建一个事件。我在编程的第一阶段,但我想尝试解决这个问题 守则: private void GoogleAPI_Add_events() { UserCredential credential; using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
private void GoogleAPI_Add_events()
{
UserCredential credential;
using (var stream =
new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = DateTime.Now;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 8;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
var ev = new Event();
EventDateTime start = new EventDateTime();
start.DateTime = new DateTime(2021, 3, 11, 10, 0, 0);
EventDateTime end = new EventDateTime();
end.DateTime = new DateTime(2021, 3, 15, 10, 0, 0);
ev.Start = start;
ev.End = end;
ev.Summary = "New Event";
ev.Description = "Description...";
var calendarId = "primary";
Event recurringEvent = service.Events.Insert(ev, calendarId).Execute();
MessageBox.Show("New evento creato");
}
我得到这个错误:
Google.GoogleAppeException:'Google.API.Requests.RequestError请求的身份验证作用域不足。[403]错误[
请求的身份验证作用域不足
基本上,这意味着您当前验证应用程序的用户没有授予您执行您尝试执行的操作所需的权限
您正在尝试使用需要以下作用域之一的方法
你还没有把你要发送的内容发布为Scope
,但我猜这不是其中之一
注:
请记住,当您更改范围时,您需要更改“用户”文本或删除credPath中存储的用户凭据文件,否则它不会为您的应用程序请求新的授权
在下面的代码中,术语“用户”表示与您一起登录的用户。如果您已经与此用户一起登录,则此用户的凭据将由FileDataStore存储在credsPath目录中,您应该有一个名为token.json的目录
要么将“user”改为其他字符串,比如“user1”,要么进入该目录并删除该文件
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
如何更改用户tex?我输入了静态字符串[]Scopes={”“};检查我的更新以了解文件数据存储如何工作。您能否提供与
Scopes
相关的代码?静态字符串[]Scopes={”“};