【问题标题】:Google Oauth 2.0 Web API redirect_uri_mismatch - Google redirecting the wrong URLGoogle Oauth 2.0 Web API redirect_uri_mismatch - Google 重定向错误的 URL
【发布时间】:2019-05-20 22:54:46
【问题描述】:

我正在尝试在 asp.net c# 项目中为 Google 表格使用 Google API 登录,但收到 redirect_uri_mismatch 错误 400。我什至没有设置 Google 重定向到的重定向 URL。我设置了重定向网址http://127.0.0.1:63092/login.aspx,但谷歌重定向到http://127.0.0.1:13557/authorize/错误的端口和错误的网页。我的Credential.json 也没有像/authorize/ 这样的东西,如下所示。

{
  "web": {
    "client_id": "client_idxxxxxxx.apps.googleusercontent.com",
    "project_id": "app-xxxxx",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "secret_xxxxxxxx",
    "redirect_uris": [ "http://127.0.0.1:63092/login.aspx" ],
    "javascript_origins": [ "http://127.0.0.1:63092" ]
  }
}

每次尝试时,它都会更改127.0.0.1 上的端口并重定向到/authorize/ 这可能是什么问题?我做错了什么?

我的代码

static string[] Scopes = { SheetsService.Scope.Spreadsheets };
static string ApplicationName = "My App";
static string thisDir = System.Web.Hosting.HostingEnvironment.MapPath("~/");
protected void Button1_Click(object sender, EventArgs e)
{
    UserCredential credential;
    using (var stream =
    new FileStream(thisDir + "credential.json", FileMode.Open, FileAccess.Read))
    {
        string credPath = thisDir + "token.json";
        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
            Scopes,
            "user",
            CancellationToken.None,
            new FileDataStore(credPath, true)).Result;
    }

    var service = new SheetsService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = ApplicationName,
    });

    String spreadsheetId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    String range = "Sheet1";
    SpreadsheetsResource.ValuesResource.GetRequest request =
            service.Spreadsheets.Values.Get(spreadsheetId, range);
    ValueRange response = request.Execute();
    IList<IList<Object>> values = response.Values;
    if (values != null && values.Count > 0)
    {
        Console.WriteLine("Name, Major");
        foreach (var row in values)
        {
            Console.WriteLine("{0}, {1}", row[0], row[4]);
        }
    }
    else
    {
        Console.WriteLine("No data found.");
    }
}

【问题讨论】:

  • 您需要出示您的代码。显示您为启动 OAuth 流程而调用的完整 URL。仔细检查该 URL 中的 redirect_uri
  • @JohnHanley 我已经用我的代码更新了这个问题。我正在使用 Google API 的在线 .Net 示例
  • 您使用了错误类型的 Google 凭据。在 Google 凭据控制台中,为应用程序类型选择“其他”。下载新凭据并重试。
  • 您正在使用的示例的链接是什么?我可能想编写一个测试代码。有时文档/示例会过时或需要更新。
  • 您研究过这些差异吗?一种是用于应用程序。另一个用于 Web 服务器。区别在于redirect_uri 的处理方式。

标签: asp.net-mvc oauth-2.0 google-api google-oauth google-sheets-api


【解决方案1】:

Google 为不同的应用程序使用提供了多种凭据类型。在您的情况下,您使用的是为 Web 服务器设计的凭据,而不是为已安装的应用程序设计的凭据(在某人的桌面上本地运行)。

在 Google 凭据控制台中,为应用程序类型选择“其他”。下载新凭据并重试。

【讨论】:

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