谷歌日历API-创建/插入/添加事件-C#-错误403
我正试图创建一个C#工具来操纵我的谷歌日历。 该工具已成功从API接收事件,但在尝试创建事件时出现错误:谷歌日历API-创建/插入/添加事件-C#-错误403,c#,google-calendar-api,C#,Google Calendar Api,我正试图创建一个C#工具来操纵我的谷歌日历。 该工具已成功从API接收事件,但在尝试创建事件时出现错误: Google.GoogleApiException: 'Google.Apis.Requests.RequestError Insufficient Permission: Request had insufficient authentication scopes. [403] Errors [ Message[Insufficient Permission: Request ha
Google.GoogleApiException: 'Google.Apis.Requests.RequestError
Insufficient Permission: Request had insufficient authentication scopes. [403]
Errors [
Message[Insufficient Permission: Request had insufficient authentication scopes.] Location[ - ] Reason[insufficientPermissions] Domain[global]
]
我有两个按钮:一个用于创建事件,另一个用于显示即将到来的事件。请注意,我尝试了3种不同的方法来添加一个事件,但都有相同的错误结果
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
namespace GoogleCalendarsAssistant
{
public partial class form_GoogleCalendarsAssistant : Form
{
public static class g
{
public static string[] Scopes = { CalendarService.Scope.Calendar };
public static string ApplicationName = "GoogleCalendarsAssistant";
public static UserCredential credential;
}
public string newline = "\r\n";
public string tab = " ";
public form_GoogleCalendarsAssistant()
{
InitializeComponent();
}
private void btn_addEvent_Click(object sender, EventArgs e)
{
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
string credPath = "token.json";
g.credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
g.Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
//tb_general.Text += newline + "Credential file saved to: " + credPath;
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = g.credential,
ApplicationName = g.ApplicationName,
});
// Define parameters of request.
Event addEvent_body = new Event();
addEvent_body.Summary = "test summary";
addEvent_body.Location = "test location";
addEvent_body.Description = "test description";
addEvent_body.Start = new EventDateTime()
{
DateTime = new DateTime(2019, 3, 7, 14, 0, 0),
TimeZone = "Romania/Bucharest"
};
addEvent_body.End = new EventDateTime()
{
DateTime = new DateTime(2019, 3, 7, 15, 0, 0),
TimeZone = "Romania/Bucharest"
};
addEvent_body.Attendees = new List<EventAttendee>()
{
new EventAttendee() {Email = "john@myjob.com"}
};
// Add event
//method 1
Event addEvent = service.Events.Insert(addEvent_body, "primary").Execute();
//method 2
//EventsResource.InsertRequest addEventResource = service.Events.Insert(addevent_body, "primary");
//addEventResource.Execute();
//method 3
//EventsResource.InsertRequest request = service.Events.Insert(addevent_body, "primary");
//request.Execute();
}
private void btn_upcoming_Click(object sender, EventArgs e)
{
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
string credPath = "token.json";
g.credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
g.Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
//tb_general.Text += newline + "Credential file saved to: " + credPath;
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = g.credential,
ApplicationName = g.ApplicationName,
});
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = DateTime.Now;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 30;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// List events.
Events events = request.Execute();
//Console.WriteLine("Upcoming events:");
tb_general.Text = "Upcoming events:";
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
string when = eventItem.Start.DateTime.ToString();
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
//Console.WriteLine("{0} ({1})", eventItem.Summary, when);
tb_general.Text += newline + tab + eventItem.Summary + when;
}
}
else
{
//Console.WriteLine("No upcoming events found.");
tb_general.Text = newline + "No upcoming events found.";
}
//Console.Read();
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.IO;
使用System.Linq;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用Google.api.Auth.OAuth2;
使用Google.api.Calendar.v3;
使用Google.api.Calendar.v3.Data;
使用Google.api.Services;
使用Google.api.Util.Store;
名称空间GoogleCalendarsAsistant
{
公共部分类表单\谷歌日历助理:表单
{
公共静态类别g
{
公共静态字符串[]范围={CalendarService.Scope.Calendar};
公共静态字符串ApplicationName=“GoogleCalendarsAsistant”;
公共静态用户凭证;
}
公共字符串换行符=“\r\n”;
公共字符串选项卡=”;
公共表格(GoogleCalendarsAssistant)
{
初始化组件();
}
私有无效btn\u添加事件\u单击(对象发送方,事件参数e)
{
使用(var stream=newfilestream(“credentials.json”、FileMode.Open、FileAccess.Read))
{
//文件token.json存储用户的访问和刷新令牌,并在授权流完成时自动创建
字符串credPath=“token.json”;
g、 凭证=GoogleWebAuthorizationBroker.AuthorizationAsync(
GoogleClientSecrets.Load(stream.Secrets),
g、 范围,
“用户”,
取消令牌。无,
新文件数据存储(credPath,true))。结果;
Console.WriteLine(“凭证文件保存到:”+credPath);
//tb_general.Text+=newline+“凭证文件保存到:”+credPath;
}
//创建谷歌日历API服务。
var service=new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer=g.credential,
ApplicationName=g.ApplicationName,
});
//定义请求的参数。
Event addEvent_body=新事件();
addEvent_body.Summary=“测试总结”;
addEvent_body.Location=“测试位置”;
addEvent_body.Description=“测试说明”;
addEvent_body.Start=新的EventDateTime()
{
DateTime=新的日期时间(2019,3,7,14,0,0),
时区=“罗马尼亚/布加勒斯特”
};
addEvent_body.End=新的EventDateTime()
{
DateTime=新的日期时间(2019,3,7,15,0,0),
时区=“罗马尼亚/布加勒斯特”
};
addEvent_body.Attendes=新列表()
{
新建EventAttendee(){Email=“john@myjob.com"}
};
//添加事件
//方法1
Event addEvent=service.Events.Insert(addEvent_body,“primary”).Execute();
//方法2
//EventsResource.InsertRequest addEventResource=service.Events.Insert(addevent_body,“primary”);
//addventresource.Execute();
//方法3
//EventsResource.InsertRequest请求=service.Events.Insert(addevent_body,“primary”);
//request.Execute();
}
私有无效btn\u即将到来的\u单击(对象发送者,事件参数e)
{
使用(var stream=newfilestream(“credentials.json”、FileMode.Open、FileAccess.Read))
{
//文件token.json存储用户的访问和刷新令牌,并在授权流完成时自动创建
字符串credPath=“token.json”;
g、 凭证=GoogleWebAuthorizationBroker.AuthorizationAsync(
GoogleClientSecrets.Load(stream.Secrets),
g、 范围,
“用户”,
取消令牌。无,
新文件数据存储(credPath,true))。结果;
Console.WriteLine(“凭证文件保存到:”+credPath);
//tb_general.Text+=newline+“凭证文件保存到:”+credPath;
}
//创建谷歌日历API服务。
var service=new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer=g.credential,
ApplicationName=g.ApplicationName,
});
//定义请求的参数。
EventsResource.ListRequest请求=service.Events.List(“主”);
request.TimeMin=DateTime.Now;
request.showdeled=false;
request.SingleEvents=true;
request.MaxResults=30;
request.OrderBy=EventsResource.ListRequest.OrderByEnum.StartTime;
//列出事件。
Events=request.Execute();
//Console.WriteLine(“即将到来的事件:”);
tb_general.Text=“即将举行的活动:”;
如果(events.Items!=null&&events.Items.Count>0)
{
foreach(events.Items中的var eventItem)
{
字符串when=eventItem.Start.DateTime.ToString();
if(String.IsNullOrEmpty(when))
{
当=eventItem.Start.Date时;
}
//WriteLine(“{0}({1})”,eventItem.Summary,when);
tb_general.Text+=换行符+制表符+ev