【发布时间】:2023-03-07 01:34:01
【问题描述】:
我正在开发一个非常简单的聊天机器人,它带有一个 ASP.NET webhook 来处理响应。我在发送或接收消息时没有任何问题,但我有点卡在验证 Authorization 标头中的 Bearer 令牌以验证传入请求是否来自 Google。
我已包含 Google.Apis.Auth API 版本 1.55。它具有应该进行此验证的功能。当然,文档没有给出 .NET 示例,但据我所知,它应该是这样的:
try
{
string token = "token here";
SignedTokenVerificationOptions stvo = new SignedTokenVerificationOptions()
{
TrustedAudiences = { "my project id" },
TrustedIssuers = { "chat@system.gserviceaccount.com" },
CertificatesUrl = "https://www.googleapis.com/service_accounts/v1/metadata/x509/chat@system.gserviceaccount.com"
};
JsonWebSignature.Payload r = await JsonWebSignature.VerifySignedTokenAsync(token, stvo);
return true;
}
catch (InvalidJwtException)
{
return false;
}
我的问题是我从 API 深处得到一个异常,看起来它正在处理来自 Google 的证书。我觉得我对这点影响不大!
System.ArgumentNullException: Value cannot be null.
Parameter name: source
at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable`1 source, Func`2 selector)
at Google.Apis.Auth.SignedTokenVerification.CertificateCacheBase.<GetCertificatesAsync>d__5.MoveNext() in C:\Apiary\2021-09-08.15-52-39\Src\Support\Google.Apis.Auth\SignedTokenVerification.cs:line 246
at Google.Apis.Auth.SignedTokenVerification.<GetCertificatesAsync>d__6.MoveNext() in C:\Apiary\2021-09-08.15-52-39\Src\Support\Google.Apis.Auth\SignedTokenVerification.cs:line 203
at Google.Apis.Auth.SignedTokenVerification.<VerifyRS256TokenAsync>d__4`2.MoveNext() in C:\Apiary\2021-09-08.15-52-39\Src\Support\Google.Apis.Auth\SignedTokenVerification.cs:line 110
at Google.Apis.Auth.SignedTokenVerification.<VerifySignedTokenAsync>d__3`2.MoveNext() in C:\Apiary\2021-09-08.15-52-39\Src\Support\Google.Apis.Auth\SignedTokenVerification.cs:line 102
at Google.Apis.Auth.JsonWebSignature.<VerifySignedTokenAsync>d__1`1.MoveNext() in C:\Apiary\2021-09-08.15-52-39\Src\Support\Google.Apis.Auth\JsonWebSignature.cs:line 61
at GroupHandler.<>c__DisplayClass0_0.<<ProcessRequest>b__0>d.MoveNext() in D:\IIS\Sites\Test\Google\BotVerify.ashx:line 31
这是正确的方法吗?还是我错过了一些完全明显的东西? :) 乔尔
【问题讨论】:
标签: google-api-dotnet-client hangouts-chat hangouts-api