gpt4 book ai didi

rest - 谷歌分析 : Invalid JWT Signature (invalid_grant) when trying to make authorized API call

转载 作者:行者123 更新时间:2023-12-03 15:56:56 26 4
gpt4 key购买 nike

我试图对 Google Analytics 中的服务帐户进行授权 API 调用(HTTP/REST)。
使用此文档:https://developers.google.com/identity/protocols/OAuth2ServiceAccount

我只是使用 HTTP/REST 请求来测试。

所以我有服务帐户的私钥文件:

{
"type": "service_account",
"project_id": "test-x",
"private_key_id": "some_private_key_id",
"private_key": "-----BEGIN PRIVATE KEY----- some_private_key -----END PRIVATE KEY-----",
"client_email": "test-01@test-x.iam.gserviceaccount.com",
"client_id": "some_client_id",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test-01%40test-x.iam.gserviceaccount.com"
}

我基于创建 JWT
客户电子邮件:
test-01@test-x.iam.gserviceaccount.com

标题:
{"alg":"RS256","typ":"JWT"}

声明集:
{
"iss": "test-01@test-x.iam.gserviceaccount.com",
"scope": "https://www.googleapis.com/auth/analytics.readonly",
"aud": "https://www.googleapis.com/oauth2/v4/token",
"exp": 1488820112,
"iat": 1488816522
}

iat - 我只是设置电流

exp - 当前 + 1 小时,

我使用此服务创建签名: https://jwt.io/#debugger

它生成我尝试用于访问 token 请求的编码值

当我尝试使用“编码”字段生成的结果时:
curl -d 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=JWT_that_has_been_signed' https://www.googleapis.com/oauth2/v4/token

结果:
{
"error": "invalid_grant",
"error_description": "Invalid JWT Signature."
}

但我不使用我的私钥。
根据用于计算签名的文档,我必须使用我的私钥。
我不完全理解如何以正确的方式使用 key 来正确计算签名。

jwt.io 已经生成了 PUBLIC 和 PRIVATE key ...

可能我错误地使用了 jwt.io..

请告诉我创建 JWT 的正确方法,或者可能是创建它的另一个服务。

谢谢!

最佳答案

对于探索这个问题的任何人,我有一组 PHP 代码来获取带有 JSON 服务帐户文件的 token 。此 PHP 代码段将为您提供一个 token ,用于获取 GA 数据。

// assume $config is your json service account file converted to array
$config = "YOUR JSON FILE CONVERTED TO ARRAY";
$jwt_header = '{"alg":"RS256","typ":"JWT"}';
$jwt_header_cryph = urlencode(base64_encode($jwt_header));

$jwt_claim = '{
"iss": "'.$config['client_email'].'",
"scope":"https://www.googleapis.com/auth/analytics.readonly",
"aud":"https://www.googleapis.com/oauth2/v4/token",
"exp":'.(time()+3600).',
"iat":'.time().'
}';

$jwt_claim_cryph = urlencode(base64_encode($jwt_claim));

$data = $jwt_header_cryph.".".$jwt_claim_cryph;

$binary_signature = "";

$algo = "SHA256";
openssl_sign($data, $binary_signature, $config['private_key'], $algo);
$jws_signature_cryph = urlencode(base64_encode($binary_signature));

//concatenating the jwt request to complete it
$complete_request = $jwt_header_cryph.".".$jwt_claim_cryph.".".$jws_signature_cryph;

//build CURL request with post method
$url = 'https://www.googleapis.com/oauth2/v4/token';

//set POST variables
$fields = array(
'grant_type' => urlencode('urn:ietf:params:oauth:grant-type:jwt-bearer'),
'assertion' => $complete_request
);

$fields_string = "";
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true );

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

$token = json_decode($result, true);

变量 $token会有以下内容
[
"access_token" => "your-access-token-string"
"expires_in" => 3599
"token_type" => "Bearer"
]

使用 access_token用于 ping GA 以获取分析数据的字符串。

希望这对某人有所帮助,因为这在任何地方都没有得到很好的解释,并且需要进行一些挖掘才能正确地找出加密。

关于rest - 谷歌分析 : Invalid JWT Signature (invalid_grant) when trying to make authorized API call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42651549/

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