【发布时间】:2018-06-14 20:13:31
【问题描述】:
我正在尝试签署对 Google 0Auth2 的请求(在 PHP 中) 这是我的代码:
$jwtHeader = $this->base64url_encode(json_encode(array(
"alg" => "RS256",
"typ" => "JWT"
)));
$now = time();
$jwtClaim = $this->base64url_encode(json_encode(array(
"iss" => "xxxxxxxxxxxxxxx.xxx.gserviceaccount.com",
"scope" => "https://www.googleapis.com/auth/prediction",
"aud" => "https://www.googleapis.com/oauth2/v4/token",
"iat" => $now,
"exp" => $now + 3600,
)));
$sign = hash_hmac('SHA256', $jwtHeader.".".$jwtClaim, $secret);
$jwtAssertion = $jwtHeader.".".$jwtClaim.".".$sign;
$client = new \GuzzleHttp\Client();
$res = $client->post('https://www.googleapis.com/oauth2/v4/token', ['headers' => ['Content-Type: application/x-www-form-urlencoded'], 'form_params' => ['grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion' => $jwtAssertion]]);
if ($res->getStatusCode() == 200) {
return $res->getBody()->getContents();
}
//BASE64 METHOD
public function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
我得到的回应是
{
"error": "invalid_grant",
"error_description": "Invalid JWT Signature."
}
现在已经有几个小时了,仍然遇到同样的错误。有人遇到过这个吗? 如有任何建议,我将不胜感激。
【问题讨论】:
-
很难说RS256是不对称的,你有公钥设置吗?你确定
urn:ietf:params:oauth:grant-type:jwt-bearer是预期的吗?你能不能也显示 base64url_encode 方法,除了代码看起来不错。 -
宾果游戏,你不是对 hmac 签名进行 base64 化
-
@LawrenceCherone 感谢您的回复。设置服务帐户后,我从下载的 json 文件中获取了私钥,这里是我的
$secret。urn:ietf:params:oauth:grant-type:jwt-bearer是的,这就是文档所说的。 base64url_encode 方法只是一个没有填充的 base64_encode。我将更新问题以包含该方法 -
我也确实将签名编码为 base64。仍然有同样的错误
标签: php laravel oauth google-api jwt