我有一个网站,我可以在其中为成员创建活动,并且我正在尝试连接到谷歌日历 API,所以当我创建新活动时,我可以将其同步到日历。
我已遵循本指南:https ://developers.google.com/calendar/quickstart/dotnet 但是当我到达运行示例代码的部分时,必须通过 OAuth 进行身份验证。我得到以下信息:
我的代码:
public class InitializeGoogleCalendarApiHelper : IInjected
{
public InitializeGoogleCalendarApiHelper(ILogger logger)
{
this.logger = logger;
}
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-dotnet-quickstart.json
static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
static string ApplicationName = "Nordmanni Google Calendar API";
private readonly ILogger logger;
public CalendarService Initialize()
{
UserCredential credential;
using (var stream =
new FileStream("C:/Projects/Nordmanni/Nordmanni.App/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 = "C:/Projects/Nordmanni/Nordmanni.App/GoogleApiToken.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
logger.Info<InitializeGoogleCalendarApiHelper>($"Credential file saved to: {credPath}");
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
return service;
}
}
我现在在本地运行网站,在视觉工作室。我所做的每个请求的端口号都会更改,因此设置端口号似乎不起作用。
这些是我在谷歌设置的设置。
我花了最后一天半的时间寻找解决方案,但到目前为止一直找不到任何东西。
我已经下载了 credentials.json 并添加到了解决方案中。欢迎任何资源或链接,或者我可以查看的示例代码。我不确定我是否正确设置了域,或者是否可以在本地运行时进行设置。