请求中的重定向URI:http://localhost:12349/authorize/与已注册的重定向URI不匹配。

7
当我尝试运行我的C#控制台应用程序时,我遇到了这个错误... 我正在尝试调用Google日历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();
    }
}

这是我在Google开发者控制台项目中的重定向URI。

Redirect URIs: http://localhost:7744/authorize/

以下是浏览器显示的错误信息。

enter image description here

我无法找到任何解决此问题的方法。希望能得到帮助。我也在stackoverflow中搜索了所有相关的帖子,但我找不到解决方案。


端口号不匹配,而端口号是HTTP uri身份的重要组成部分。 - Damien_The_Unbeliever
@Damien_The_Unbeliever 我知道。但是我没有在我的控制台应用程序中定义任何重定向URI。如何定义它。 - Hannan Hossain
@Damien_The_Unbeliever,我该如何在控制台应用程序中定义重定向URI以匹配凭据??? - Hannan Hossain
1
在控制台应用程序中进行身份验证时,您不需要定义重定向URI。请查看我的答案,我认为这将解决您的问题。 - SKD
2个回答

25

我认为您在Google开发者控制台中的“创建客户端ID”时存在错误。请确保在应用程序类型中选择了“已安装的应用程序”,以从控制台应用程序访问您的项目。

请查看附加的图像。根据请求类型,您必须在Google开发者控制台中的注册应用程序中创建客户端ID和凭据。

在进行身份验证时,您不需要在控制台应用程序中定义重定向URI。

输入图像描述


如果您正在使用控制台应用程序,则选择“其他”以创建客户端ID。 - Brijesh Bhagavath

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接