我要做的是使用GoogleAPI将图片上传到云端硬盘(属于一个帐户)。
所以我正在使用服务器到服务器应用程序并尝试按照以下链接中的步骤使用OAuth 2.0进行身份验证
https://developers.google.com/identity/protocols/OAuth2ServiceAccount#overview
现在根据谷歌文档,需要三个步骤
我已经准备好在第一步中生成JWT。但是,当我请求访问令牌时,我收到了空响应。
所以我尝试使用休息控制台发送令牌请求,仅检查发布请求的结果。我收到的回复是500内部错误。
以下是我的代码
$jwt_claim = '{
"iss":"myemail.gserviceaccount.com",
"scope":"https://www.googleapis.com/auth/prediction",
"aud":"https://accounts.google.com/o/oauth2/token",
"exp":1328554385,
"iat":1328550785
}';
if (!$cert_store = file_get_contents(base_url('assets').'/myprivatekey.p12')) {
echo "Error: Unable to read the cert file\n";
exit;
}
$cert_info = array();
if (!openssl_pkcs12_read($cert_store, $cert_info, "notasecret")){
echo "Error: Unable to read the cert store.\n";
exit;
}
echo "Certificate Information Generated Succesfully<br>";
//print_r($cert_info['pkey']);
$sign = JWT::encode($jwt_claim,$cert_info['pkey'],'RS256');
echo 'Signature <br>';
echo $sign;
//2. Access token request
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://www.googleapis.com/oauth2/v4/token',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'grant_type' => urlencode('urn:ietf:params:oauth:grant-type:jwt-bearer'),
'assertion' => $sign
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
echo '<br>response <br/>';
echo $resp;