【问题标题】:Google OAuth 2.0 get Error 400: redirect_uri_mismatch, ASP.net core 3.1Google OAuth 2.0 得到错误 400:redirect_uri_mismatch,ASP.net core 3.1
【发布时间】:2021-11-13 10:40:05
【问题描述】:

我正在尝试将 google drive api 与我在 http://127.0.0.1:4000 上运行的 asp.net core 3.1 Web 服务器连接。但在授权期间我得到不同的 redirect_uri。并且 credential.json 都是有效的。

我得到的错误:

我的代码:

public class GDriveService : BaseService
    {
        private string credentialFileName = ConfigurationManager.AppSettings["CredentialFile"].ToString();
        private string appName = ConfigurationManager.AppSettings["AppName"].ToString();
        private string[] scopes;
        private UserCredential credential;
        private DriveService service;
        public GDriveService(IWebHostEnvironment env):base(env)
        {
            scopes = new string[] { DriveService.Scope.Drive,
                               DriveService.Scope.DriveFile,};


            using (var stream = new FileStream(Path.Combine(env.WebRootPath, credentialFileName), FileMode.Open, FileAccess.Read))
            {
                String FilePath = Path.Combine(env.WebRootPath, "DriveServiceCredentials");
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(FilePath, true)).Result;
            }
}

我的凭据配置:

【问题讨论】:

  • 您是否已提交 OAuth2 屏幕配置以供验证?
  • 我也遇到了这个问题。 @ziganotschka 我在谷歌控制台中的应用程序仍处于测试阶段,我已经提交了 OAuth2 同意屏幕。不过,它似乎不起作用。帮忙?
  • 我在 GCP 中使用 http://localhost/authorize/ 作为重定向 URL,从 Visual Studio 运行 Web 应用程序。然后我尝试连接到 Google Drive API 并失败了几次。然后我刷新我的 Web 应用程序(仍然连接调试器)并等待几分钟,然后再试一次。令我惊讶的是,它起作用了,但只是暂时的。奇怪..

标签: asp.net .net-core oauth-2.0 google-drive-api google-api-dotnet-client


【解决方案1】:

您发送的重定向 uri 必须与您注册的完全匹配。在您的情况下,您是从中发送的。 (注意这是由客户端库控制的)

您只在谷歌云控制台注册了以下内容

解决方案是添加错误消息告诉您的重定向 uri,完全包括结尾 /。

此视频将向您展示如何修复它。 Google OAuth2: How the fix redirect_uri_mismatch error. Part 2 server sided web applications.

请记住确保 Visual Studio 使用的是静态端口。如果您的端口不断更改,您的重定向 uri 也必须更改。

【讨论】:

    【解决方案2】:

    尝试将http://127.0.0.1/http://127.0.0.1/authorize/ 添加到授权重定向URI 部分。

    【讨论】:

      猜你喜欢
      • 2021-11-05
      • 2014-02-09
      • 2015-04-03
      • 2021-06-28
      • 2020-11-08
      • 2021-10-14
      • 2022-11-05
      • 2016-02-21
      • 1970-01-01
      相关资源
      最近更新 更多