我正在从旧的 Google 登录库迁移到新的 Google Identity Services (GIS) 库。这是强制性的,因为从 2023 年 3 月起旧的将不再使用。
之前,我做了(为了清晰起见,进行了简化):
<script src="https://apis.google.com/js/api:client.js"></script>
gapi.load();
var auth2 = gapi.auth2.init();
auth2.attachClickHandler();
onGoogleSignIn(googleUser); // attachClickHandler's callback
var profile = googleUser.getBasicProfile(); // profile info accessible
var authResponse = googleUser.getAuthResponse(); // auth response accessible
var accessToken = authResponse.id_token; // get actual access token
现在,我正在尝试(为了清楚起见,进行了简化):
<script src="https://accounts.google.com/gsi/client"></script>
var gisClient = google.accounts.oauth2.initTokenClient();
gisClient.requestAccessToken();
callback(); // initTokenClient's callback
var accessToken = response.access_token; // get access token in callback
使用旧的谷歌登录库,我验证了访问令牌服务器端,如下所示:
Payload payload = await GoogleJsonWebSignature.ValidateAsync(accessToken);
这还会在有效负载中返回用户的电子邮件和姓名。
我从 GIS 获取的访问令牌比从 GAPI 获取的旧访问令牌短得多。
An 在线令牌调试器 https://jwt.io/告诉我这不是有效的 JWT 令牌。
ValidateAsync 方法抛出异常:
JWT must consist of Header, Payload, and Signature
考虑到它不是有效的 JWT 令牌,这并不奇怪。
我还尝试了以下调用:
Payload payload = await JsonWebSignature.VerifySignedTokenAsync(AccessToken, options);
相同的结果。
The 官方文档 https://developers.google.com/identity/gsi/web/guides/verify-google-id-token没有说明如何为 C# / .NET 验证此令牌服务器端。
我在文档中的任何地方都找不到这方面的帮助。
我该怎么做才能与 Google Identity Services 一起进行服务器端访问令牌验证(以及电子邮件+个人资料的检索)?