我正在尝试让 Google OAuth 在 Blazor 服务器应用程序中工作。请注意,我需要的不是登录功能。相反,我想为用户调用日历 API,最终用户需要对此进行授权,据我了解,这是一个不同的用户流程。
在此用户流程中,用户被定向到如下网址:
https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/calendar&include_granted_scopes=true&response_type=token&state=12345&redirect_uri=https%3A//localhost:7115/api/gauth&client_id=test.apps.googleusercontent.com
`
这会启动 Google 授权提示,并且用户对其进行授权,然后 google 会向上述 url 中包含的redirect_uri 发送回调。
问题在于,根据文档,回调查询字符串的格式如下:
https://oauth2.example.com/callback#access_token=xyz&token_type=Bearer&expires_in=3600
注意使用主题标签,而不是问号。回调是控制器方法,而不是浏览器,因此它会忽略主题标签之后的所有内容。因此,控制器操作不会获取任何参数。
[Microsoft.AspNetCore.Mvc.Route("api")]
[ApiController]
public class GoogleScopeAuth : ControllerBase
{
[HttpGet("gauth")]
public async Task<ActionResult> Get(string? access_token = null, string? token_type = null, BigInteger? expires_in = null, string? state = null, string? error = null )
{
Console.WriteLine(access_token);
Console.WriteLine(token_type);
Console.WriteLine(expires_in);
Console.WriteLine(state);
return Ok();
}
}
如何配置它,以便主题标签转换为问号,以便得到正确处理?
我是 Google OAuth 的新手,所以如果我遗漏了什么,请告诉我。
我想做的是捕获访问令牌,以便我可以使用它来保持应用程序的日历与用户的 Google 日历同步(将事件插入 Google 日历)。
如何配置它,以便主题标签转换为问号,以便得到正确处理?
你不能。
?
是查询字符串。 #
是碎片。
明显的区别在于
#
片段不是 HTTP 请求本身的一部分。可以把它想象成一个“只有浏览器”知道的书签。无法从 HTTP 请求中读取片段。因此,您无法读取 #
之后的令牌,也无法告诉浏览器、身份提供商或 HTTP 以不同方式执行此操作。了解更多
OAuth 隐式流程已死吗?
https://developer.okta.com/blog/2019/05/01/is-the-oauth-implicit-flow-dead
Microsoft 不建议使用隐式授予。https://learn.microsoft.com/en-us/aspnet/core/blazor/security/web assembly/?view=aspnetcore-8.0#use-the-authorization-code-flow-with-pkce
开拓者
由于不活动而关闭。根据上面的信息,我猜它坏了,除非有必要的需求,否则不会修复。 使用授权代码流程来验证和访问 Google API。更多信息可以在这里找到。
https://blazorschool.com/tutorial/blazor-server/dotnet7/authentication-with-google-oauth-700535