我在尝试运行我的c#控制台应用程序时遇到此错误...我正在尝试调用google calender api v3来获取日历并将事件添加到日历。根据google-api-dotnet-client的示例代码,我这样做。(https://code.google.com/p/google-api-dotnet-client/source/browse/Calendar.VB.ConsoleApp/Program.vb?repo=samples)这是vb.net代码。我在将其转换为c#代码后使用此示例。
这是我的代码:
class Program
{
static void Main(string[] args)
{
try
{
new Program().Run().Wait();
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine("ERROR: " + e.Message);
}
}
}
private async Task Run()
{
UserCredential credential;
IList<string> scopes = new List<string>();
CalendarService service;
scopes.Add(CalendarService.Scope.Calendar);
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
// problem occuring during executing this statement.
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
scopes,
"user", CancellationToken.None, new FileDataStore("Calender.SampleApp") );
}
BaseClientService.Initializer initializer = new BaseClientService.Initializer();
initializer.HttpClientInitializer = credential;
initializer.ApplicationName = "C# Calendar Sample";
service = new CalendarService(initializer);
Event newEvent = new Event();
newEvent.Summary = "Appointment";
newEvent.Description = "Need to meet my Uncle";
IList<EventReminder> reminders = new List<EventReminder>();
reminders.Add(new EventReminder { Method = "sms", Minutes = 10 });
newEvent.Reminders = new Event.RemindersData { UseDefault = false, Overrides = reminders };
newEvent.Recurrence = new String[] { "DTSTART;TZID=Bangladesh Standard Time:20140124T163000;RRULE:FREQ=DAILY" };
IList<EventAttendee> attendees = new List<EventAttendee>();
attendees.Add(new EventAttendee { Email = "hannan.cse.m@gmail.com", Organizer = true, DisplayName = "Hannan" });
newEvent.Attendees = attendees;
newEvent.GuestsCanInviteOthers = false;
newEvent.GuestsCanModify = false;
newEvent.GuestsCanSeeOtherGuests = false;
newEvent.Location = "Dhaka, Bangladesh";
newEvent.Start = new EventDateTime { DateTime = DateTime.Now, TimeZone = "Bangladesh Standard Time" };
Event recurringEvent = service.Events.Insert(newEvent, "primary").Execute();
var list = await service.CalendarList.List().ExecuteAsync();
}
}
这是我的GoogleDevelopers控制台项目中的重定向URI。
Redirect URIs: http://localhost:7744/authorize/
这是浏览器中显示的错误消息。
我找不到任何解决此问题的方法。一些帮助将是值得的。我还在stackoverflow中搜索所有已发布的帖子。但我找不到它的解决方案。
答案 0 :(得分:21)
我认为您在GoogleDevelopers Console中“创建客户端ID”时做错了什么。确保您已在应用程序类型中选择“已安装的应用程序”以从控制台应用程序访问您的项目。
查看附图。根据请求类型,您必须创建clientid 和您在Google Developers Console中注册的应用程序中的信誉。
在进行身份验证时,您无需在控制台应用程序中定义重定向uri。
答案 1 :(得分:0)
我在一个简单的测试程序(https://ctrlq.org/google.apps.script/docs/guides/rest/quickstart/dotnet.html)上遇到了这个错误,它碰巧是client_secret.json是错误的,再次下载它就有用了。