【问题标题】:Google "GoogleWebAuthorizationBroker" showing error redirect uri mismatch谷歌“GoogleWebAuthorizationBroker”显示错误重定向 uri 不匹配
【发布时间】:2019-08-03 21:23:07
【问题描述】:

我在 MVC (C#) 中执行此操作。我想访问用户谷歌日历,所以我指定了一个带有“访问日历”的按钮。当用户单击下面的按钮时,将调用以下代码来获取令牌(并保存)以访问日历数据。

 UserCredential credential;
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    new ClientSecrets
                    {
                        ClientId = "xxxxxx.apps.googleusercontent.com",
                        ClientSecret = "FxxxxxxxxxZ"
                    },
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath)).Result;

当这个方法被执行时,我们应该被重定向到同意屏幕,相反,我得到了的错误

但它显示的重定向 URI 我从未在控制台中指定。这些是我在谷歌项目控制台中指定的重定向 uri。 我做错了什么吗?如何正确重定向到权限屏幕?

【问题讨论】:

    标签: c# oauth-2.0 google-calendar-api google-oauth google-developers-console


    【解决方案1】:

    重定向 uri 问题

    您请求中的重定向 uri 是 http://127.0.1:59693/authorize 您尚未在 Authorized redirect Uris 下添加它。

    您不能只添加任何重定向 uri。客户端库自己构建这个 url。它总是

    host:port/authorize
    

    应用类型

    您可以创建多种类型的客户端,这些客户端旨在与不同类型的应用程序一起使用。用于连接这些客户端的代码也不同。

    • 已安装的应用程序 - 安装在用户计算机上的应用程序
    • Web 应用程序 - 托管在通过 Web 浏览器连接到用户的 Web 服务器中的应用程序。

    已安装的应用程序

    您正在使用GoogleWebAuthorizationBroker.AuthorizeAsync,它是为与已安装的应用程序一起使用而设计的。浏览器窗口将在机器本身上打开。如果您尝试将其托管在网络服务器上,浏览器将尝试在服务器上打开并且不会向用户显示。

    网络应用程序

    您应该关注Web applications 并使用GoogleAuthorizationCodeFlow

    using System;
    using System.Web.Mvc;
    
    using Google.Apis.Auth.OAuth2;
    using Google.Apis.Auth.OAuth2.Flows;
    using Google.Apis.Auth.OAuth2.Mvc;
    using Google.Apis.Drive.v2;
    using Google.Apis.Util.Store;
    
    namespace Google.Apis.Sample.MVC4
    {
        public class AppFlowMetadata : FlowMetadata
        {
            private static readonly IAuthorizationCodeFlow flow =
                new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                    {
                        ClientSecrets = new ClientSecrets
                        {
                            ClientId = "PUT_CLIENT_ID_HERE",
                            ClientSecret = "PUT_CLIENT_SECRET_HERE"
                        },
                        Scopes = new[] { DriveService.Scope.Drive },
                        DataStore = new FileDataStore("Drive.Api.Auth.Store")
                    });
    
            public override string GetUserId(Controller controller)
            {
                // In this sample we use the session to store the user identifiers.
                // That's not the best practice, because you should have a logic to identify
                // a user. You might want to use "OpenID Connect".
                // You can read more about the protocol in the following link:
                // https://developers.google.com/accounts/docs/OAuth2Login.
                var user = controller.Session["user"];
                if (user == null)
                {
                    user = Guid.NewGuid();
                    controller.Session["user"] = user;
                }
                return user.ToString();
    
            }
    
            public override IAuthorizationCodeFlow Flow
            {
                get { return flow; }
            }
        }
    }
    

    【讨论】:

    • 以下 FlowMetadata 有效。这个链接也对我如何构建代码帮助很大...sparkhound.com/blog/…
    • @DalmTo 我们如何使用上述方法获取刷新令牌(在首次授权后停止重定向用户)。目前我只获得访问令牌,这意味着用户将在过期后再次被重定向。
    • 只有在第一次需要保存时才会获得 refres 令牌。让用户撤销您在其 Google 帐户中的访问权限。或者我认为你可以在你的代码中撤销访问权限。 accounts.google.com/o/oauth2/revoke?token={token} 我将不得不在图书馆里四处寻找撤销命令。
    • 等待 credential.RevokeTokenAsync(CancellationToken.None);
    • 在我撤销令牌之后。我再次被重定向到同意屏幕,并且能够获取刷新令牌。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 2020-10-15
    • 1970-01-01
    • 2021-12-10
    • 2015-08-09
    • 2020-12-21
    • 1970-01-01
    相关资源
    最近更新 更多