尝试连接到Google API的Google Oauth时无效的JWT

安迪

我试图通过JWT通过OAuth连接到Google API,但我不断收到此错误:

{“ error”:“ invalid_grant”,“ error_description”:“无效的JWT:令牌必须是短期令牌并且在合理的时间范围内”“}

在我的JWT calim中,我将iat设置为当前时间减去1970-01-01(以秒为单位),并将exp设置为iat + 3600,所以我不知道为什么我仍然遇到此错误。如果有人知道答案,请告诉meeeeee!

约瑟夫·施

不知道您是否曾经使用过它,但是使用PHP函数openssl_sign()的以下简单步骤对我有用

//helper function
function base64url_encode($data) { 
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); 
}

//Google's Documentation of Creating a JWT: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#authorizingrequests

//{Base64url encoded JSON header}
$jwtHeader = base64url_encode(json_encode(array(
    "alg" => "RS256",
    "typ" => "JWT"
)));
//{Base64url encoded JSON claim set}
$now = time();
$jwtClaim = base64url_encode(json_encode(array(
    "iss" => "761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@developer.gserviceaccount.com",
    "scope" => "https://www.googleapis.com/auth/prediction",
    "aud" => "https://www.googleapis.com/oauth2/v4/token",
    "exp" => $now + 3600,
    "iat" => $now
)));
//The base string for the signature: {Base64url encoded JSON header}.{Base64url encoded JSON claim set}
openssl_sign(
    $jwtHeader.".".$jwtClaim,
    $jwtSig,
    $your_private_key_from_google_api_console,
    "sha256WithRSAEncryption"
);
$jwtSign = base64url_encode($jwtSig);

//{Base64url encoded JSON header}.{Base64url encoded JSON claim set}.{Base64url encoded signature}
$jwtAssertion = $jwtHeader.".".$jwtClaim.".".$jwtSig;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android Wear Watch Face-尝试连接到Google Services API时为INVALID_ACCOUNT

即时通讯尝试连接到Google Sheets API时出现奇怪的PHP错误

连接到 Google Api 客户端时连接失败

无效签名 - Google JWT API

Google Compute Engine:尝试连接到实例时出现SSH错误

使用服务帐户和OAuth连接到Google API

尝试使用Google Cloud Text-to-Speech API时无效的JWT

连接到Google Analytics API时Java中的SSL例外

连接到 Google Drive API 时遇到问题

Google OAuth v2.0 生成无效的 JWT

使用Oauth连接到Google Compute Engine

尝试使用 Pgadmin 连接到 Google Cloud Platform 上的 postgres 容器时出现内部服务器错误

尝试连接到Google Compute Engine Ubuntu VM上托管的SQL Server时如何解决此错误

连接到Google Cloud Datastore时出错

尝试连接到API时,Mocha测试套件出错

尝试连接到 Azure 中的 API 但允许来源时出错

尝试连接到Lighthouse API时出现401错误

在GraalVM本机图像中使用Google Api服务时,“无效的JWT:观众检查失败”

通过OAuth 2连接到Google“ redirect_uri的参数值无效:缺少权限:”

尝试从本地Google Cloud函数连接到Google Cloud MySQL数据库时,为什么会出现sqlalchemy.exc.OperationalError?

使用 GOOGLE_APPLICATION_CREDENTIALS 时 JWT 无效

尝试将 Google Vision AI 连接到 Django

单击“允许”后 Google Analytics API OAuth 无法连接到本地主机

尝试连接到Networkshare时句柄无效

尝试连接到 vTiger 时凭据无效

使用JWT和dart连接到Google的数据存储

连接到 Google API 客户端时 Android Activity Lifecycle 的奇怪行为

将 Tsheets api 连接到 Google 应用程序脚本时出错

使用适用于Python的Google API客户端库连接到Google Analytics(分析)时,发生SSLHandshakeError