【问题标题】:Google api OAuth fails with redirect uri mismatchGoogle api OAuth 因重定向 uri 不匹配而失败
【发布时间】:2020-06-24 06:17:50
【问题描述】:

我有一个网站,可以在其中为成员创建活动,并且我正在尝试连接到谷歌日历 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;
        }
    }

我现在在 Visual Studio 中本地运行该网站。 我所做的每个请求的端口号都会更改,因此设置端口号似乎不起作用。

这些是我在谷歌设置的设置。

我花了最后一天半的时间,寻找解决方案,但到目前为止找不到任何东西。

我已下载 credentials.json 并添加到解决方案中。 欢迎任何资源或链接,或者我可以查看的示例代码。 我不确定我是否正确设置了域,或者是否可以在本地运行时进行设置。

【问题讨论】:

  • 您显示的错误消息 (127.0.0.1:53479/authorize) 中的 URI 不在您配置的授权 URL 列表中,因此看起来完全符合预期。端口号很重要,x.com:123x.com:456 被认为是完全不同的 OAuth2 重定向 URI,因此您需要进行更改以使其不会因每个请求(甚至每次执行 Web 应用程序)而更改。
  • @sellotape 我也试过了,但端口号似乎每次都在变化。而且地址和IIS配置的不一样。
  • 它是什么类型的网络应用程序?例如。 .NET/核心 ASP?角?对于所有这些,我的经验是端口最初设置得相当随机,但如果使用 IIS Express,则在同一台机器上执行时保持不变。在 Core/.NET ASP 中,您可以在项目的属性 > 调试选项卡中设置它,或者在 Core 中的项目 > 属性 > launchSettings.json 下进行设置。无论哪种方式,都需要与 Google 方面的配置相同。
  • .net mvc,Umbraco 项目。它有一个 localhost:1234 地址,但是当我发出请求时,它说 127.0.0.1:4321 并且端口号不断变化,即使我没有更改它。

标签: c# .net google-search-api umbraco8


【解决方案1】:

我们不久前遇到了类似的问题,据我假设您正在尝试从您的后端服务授权 google API

我们采用的解决方案

我们为此使用了 google 服务帐户。使用简单优雅

步骤 - 1:设置谷歌服务帐户

点击此链接以使用谷歌开发者控制台设置服务帐户 https://developers.google.com/android/management/service-account

第 2 步:使用服务帐户电子邮件 ID 共享您的 google 应用程序

此步骤使您的服务帐户可以访问谷歌应用程序,可能是日历、谷歌文档或谷歌表格 类似于与其他用户共享文档 https://support.google.com/calendar/answer/37082?hl=en(此链接显示如何与其他 emailId 共享您的日历)

步骤 - 3:点击这些链接将服务帐户与您的应用程序集成

https://developers.google.com/identity/protocols/oauth2/service-account https://cloud.google.com/docs/authentication/production

谷歌服务帐号的优点是您不必定期登录来更新数据。设置一次,你就可以忘记它

希望我的回答有帮助:)

【讨论】:

  • 我在第二步有点迷失了?我已经创建了一个服务帐户,并为其添加了一个密钥,下载了文件,然后保存了它。但我不知道如何与电子邮件 ID 共享日历应用程序?
  • 您好,请点击此链接,它向您展示了如何使用电子邮件 ID support.google.com/calendar/answer/37082?hl=en 共享日历
  • 还更新了答案以防其他人想查看相同的内容
  • 所以我运行了示例代码,它似乎运行良好。但是使用存储客户端如何帮助我启动和运行日历服务?
猜你喜欢
  • 2017-12-10
  • 1970-01-01
  • 2023-01-21
  • 2021-12-10
  • 2015-08-09
  • 2014-04-30
  • 1970-01-01
  • 2021-11-26
  • 2017-11-28
相关资源
最近更新 更多