Google OAuth 令牌交换在 .net 中不起作用

问题描述 投票:0回答:1

好的,所以我正在尝试在我的 .net Web 应用程序 (Blazor) 中设置 Google 的 OAuth。

我已完成第一部分的设置和工作,即请求授权码。

但是,一旦我有了授权码,我就应该用它来交换令牌和刷新令牌。

为此,我使用 Google .net Auth 库。我找到了一个样本并尝试了一下,但我得到的唯一结果是:

Error:"invalid_grant", Description:"Bad Request", Uri:""

我搜索过这个并找到了this。我已经检查了所有可能的问题,但没有一个适用于我的测试。 IE,时钟没有关闭,用户(我)没有撤销访问权限,密码最近没有更改等等

在我看来,这个调用应该不难,但我花了几个小时尝试调试这个。我正在使用官方库来实现此目的,但我怀疑有些东西无法正常工作。这是代码:

GoogleAuthorizationCodeFlow authorizationCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
    DataStore = dataStore,
    ClientSecrets = new ClientSecrets()
    {
        ClientId = clientId,
        ClientSecret = clientSecret
    }
});

try
{

    var tokenResponse = await authorizationCodeFlow.ExchangeCodeForTokenAsync(
        userId, // user for tracking the userId on our backend system
         authorizationCode,
        "https://localhost:7155/gauth", // redirect_uri can not be empty. Must be one of the redirects url listed in your project in the api console
        CancellationToken.None);
 

我该怎么办?跳过.net lib并直接从我自己的代码调用api?

第一次调用获取授权码每次都很完美,不知道为什么这一步如此困难。

c# .net-core blazor google-oauth asp.net-core-8
1个回答
0
投票

GoogleAuthorizationCodeFlow 是 google api .net 客户端库的一部分,不支持 Blazor 授权。

您最好的选择是放弃该库并尝试自己编写授权代码。

© www.soinside.com 2019 - 2024. All rights reserved.