尝试连接到 Google Oauth for google API 时无效的 JWT
Invalid JWT when trying to connect to Google Oauth for google API
我试图通过 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,所以我不知道为什么我仍然收到此错误。如果有人知道答案请告诉我!
如果您按照此处 HTTP/Rest 下的步骤进行操作:
https://developers.google.com/identity/protocols/OAuth2ServiceAccount#formingheader,您会看到您的服务帐户支持 RSA SHA-256,并且您似乎在使用 HMAC。
不确定你是否让它工作,但使用 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;
如果您看到此错误,则必须为令牌设置正确的到期时间。示例:
var currentTime = new Date().getTime() / 1000; //must be in seconds
var exp = currentTime + 60;
var auth_claimset = {
iss : "...",
scope : "...",
aud : "...",
exp : exp,
iat : currentTime
};
我有同样的问题,我通过同步我的虚拟机时间来解决它,使正确的时间与 public ntpserver:
ntpdate ntp.ubuntu.com
WSL 也会发生这种情况,如果您在 Windows Docker 上使用 WSL2 后端的本地开发机器上进行测试,也会导致此问题。
您需要安装ntpdate
sudo apt install ntpdate
然后你需要设置时间。
sudo ntpdate time.windows.com
这里有一篇关于它的好文章 - https://tomssl.com/fixing-clock-drift-in-wsl2-using-windows-terminal/
我试图通过 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,所以我不知道为什么我仍然收到此错误。如果有人知道答案请告诉我!
如果您按照此处 HTTP/Rest 下的步骤进行操作: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#formingheader,您会看到您的服务帐户支持 RSA SHA-256,并且您似乎在使用 HMAC。
不确定你是否让它工作,但使用 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;
如果您看到此错误,则必须为令牌设置正确的到期时间。示例:
var currentTime = new Date().getTime() / 1000; //must be in seconds
var exp = currentTime + 60;
var auth_claimset = {
iss : "...",
scope : "...",
aud : "...",
exp : exp,
iat : currentTime
};
我有同样的问题,我通过同步我的虚拟机时间来解决它,使正确的时间与 public ntpserver:
ntpdate ntp.ubuntu.com
WSL 也会发生这种情况,如果您在 Windows Docker 上使用 WSL2 后端的本地开发机器上进行测试,也会导致此问题。
您需要安装ntpdate
sudo apt install ntpdate
然后你需要设置时间。
sudo ntpdate time.windows.com
这里有一篇关于它的好文章 - https://tomssl.com/fixing-clock-drift-in-wsl2-using-windows-terminal/