저장소 API에 대한 API 요청 (GET 버킷)을 만들고 있는데 필요한 매개 변수 중 하나가 '인증'헤더입니다.서비스 계정을 사용하여 Google Cloud Storage REST API에 대한 OAuth2 액세스 토큰을 요청할 때 invalid_grant 오류가 발생했습니다.
서비스 계정을 사용하여 API에 액세스하고 있습니다.
https://developers.google.com/accounts/docs/OAuth2ServiceAccount 문서를 따라 "Authorization"헤더에 대한 액세스 토큰을 얻었으므로 승인 된 요청을 REST API에 보낼 수있었습니다. 문제는 항상 "invalid_grant"오류가 발생하는 것입니다.
사용이 코드를 확인하려면 : "invalid_grant"오류가 발생합니다이 코드에서 뭔가 잘못
<?php
error_reporting(E_ERROR);
const CLIENT_ID = 'XXXXXXXXXXXX.apps.googleusercontent.com';
const SERVICE_ACCOUNT = '[email protected]';
const KEY_FILE = 'XXX.p12';
function get_oauth_access_token()
{
$header[alg] = 'RS256';
$header[typ] = 'JWT';
$header = urlencode(base64_encode(utf8_encode(json_encode($header))));
$assertion_time = time();
$claim[iss] = CLIENT_ID; //also tried SERVICE_ACCOUNT here, no improvement
$claim[scope] = 'https://www.googleapis.com/auth/devstorage.read_only';
$claim[aud] = 'https://accounts.google.com/o/oauth2/token';
$claim[exp] = $assertion_time + 3600;
$claim[iat] = $assertion_time;
$claim = urlencode(base64_encode(utf8_encode(json_encode($claim))));
$data = $header . '.' . $claim;
$p12 = file_get_contents(KEY_FILE);
$cert = array();
openssl_pkcs12_read($p12, $cert, 'notasecret');
$priv_key_id = openssl_get_privatekey($cert[pkey]);
openssl_sign($data, $signature, $priv_key_id, 'sha256');
$signature = urlencode(base64_encode($signature));
$assertion = $data . '.' . $signature;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('grant_type'=>'assertion',
'assertion_type'=>'http://oauth.net/grant_type/jwt/1.0/bearer',
'assertion'=>$assertion));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
var_dump($result);
var_dump($error);
}
get_oauth_access_token();
있습니까?
아니, 그것은 내가 여기에 언급 한대로 작동하지 않습니다 당신이 제시 한 코드는 입니다 https://groups.google.com/forum/?fromgroups#!topic/gs-discussion/3y_2XVE2q7U 아마 그냥 해결 방법과 액세스 토큰을 얻기 위해 SDK가 필요 bloat 추가합니다. –