gpt4 book ai didi

oauth-2.0 - 尝试连接到 Google Oauth for google API 时 JWT 无效

转载 作者:行者123 更新时间:2023-12-01 00:42:03 34 4
gpt4 key购买 nike

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

{ "error": "invalid_grant", "error_description": "Invalid JWT: Token must be a short-lived token and in a reasonable timeframe" }



在我的 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;

关于oauth-2.0 - 尝试连接到 Google Oauth for google API 时 JWT 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36909121/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com