我有一些用于创建Google日历事件的代码,但是它不起作用。我尝试通过C#在Google日历中创建活动。
源代码来自Google网站(https://developers.google.com/calendar/v3/reference/events/insert),但部分代码来自其他链接(https://developers.google.com/calendar/quickstart/dotnet)。
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;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CalendarQuickstart
{
class Program
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-dotnet-quickstart.json
static string[] Scopes = { CalendarService.Scope.Calendar };
static string ApplicationName = "Google Calendar API .NET Quickstart";
static void Main(string[] args)
{
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,
});
Event newEvent = new Event()
{
Summary = "Google I/O 2015",
Location = "800 Howard St., San Francisco, CA 94103",
Description = "A chance to hear more about Google's developer products.",
Start = new EventDateTime()
{
DateTime = DateTime.Parse("2019-06-28T09:00:00-07:00"),
TimeZone = "America/Los_Angeles",
},
End = new EventDateTime()
{
DateTime = DateTime.Parse("2019-06-28T17:00:00-07:30"),
TimeZone = "America/Los_Angeles",
},
Recurrence = new String[] { "RRULE:FREQ=DAILY;COUNT=2" },
Attendees = new EventAttendee[] {
new EventAttendee() { Email = "lpage@example.com" },
new EventAttendee() { Email = "sbrin@example.com" },
},
Reminders = new Event.RemindersData()
{
UseDefault = false,
Overrides = new EventReminder[] {
new EventReminder() { Method = "email", Minutes = 24 * 60 },
new EventReminder() { Method = "sms", Minutes = 10 },
}
}
};
String calendarId = "primary";
EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
Event createdEvent = request.Execute();
Console.WriteLine("Event created: {0}", createdEvent.HtmlLink);
}
}
}
这是一个错误,我不明白这是什么意思。
答案 0 :(得分:0)
错误消息“请求的身份验证范围不足”表明您在令牌中使用的范围存在问题。我可以在您的代码中看到您使用的是正确的作用域来执行这些操作,因此最有可能的问题是token.json尚未使用新的作用域进行更新,并且仍然使用先前的作用域,不允许使用这些动作。
解决方案:
删除文件token.json并再次运行脚本,它会提示您授予访问权限的链接,一旦被接受,它将使用更新的范围再次创建token.json。每次更改范围时都必须这样做。
如果上述方法不能解决问题,则另一重要的事情是在Cloud Platform控制台中启用要使用的服务,在本例中为Calendar API(快速入门[1]的第一步)。 / p>
请记住,如果您在Cloud Platform控制台中为项目启用了其他API,则必须创建一个新的凭据.json,它将更新已启用的服务,并将该凭据.json替换为上一个。
[1] https://developers.google.com/calendar/quickstart/dotnet