2 回答
TA贡献1796条经验 获得超10个赞
首先,您必须获得一个访问令牌以进行身份验证。为此,您需要 json 身份验证密钥。
此页面对我有很大帮助: https ://www.it-swarm.dev/de/curl/wie-kann-man-mit-curl-eine-verbindung-zum-google-drive-api-herstellen/806069468 /
也许这个 PHP 代码可以帮助你一点:
function get_Google_accesstoken($scope,$credfile,$proxy,$timetoexpiration){
#Developers Info at developers.google.com/identity/protocols/oauth2/service-account
$GoogleApiKeyInfo=GoogleApiKeyInfo($credfile);
$Header=array();
$Header["alg"]="RS256";
$Header["typ"]="JWT";
$ClaimSet["iss"]=$GoogleApiKeyInfo["client_email"];
$ClaimSet["scope"]=$scope;
$ClaimSet["aud"]=$GoogleApiKeyInfo["token_uri"];
$ClaimSet["iat"]=time();
$ClaimSet["exp"]=$ClaimSet["iat"]+($timetoexpiration);
$Jws=base64_encode(json_encode($Header)).".".base64_encode(json_encode($ClaimSet));
$OpenSslRslts=openssl_sign($Jws,$Signature,$GoogleApiKeyInfo["private_key"],OPENSSL_ALGO_SHA256);
$Jwt=$Jws.".".base64_encode($Signature);
$SendVars=array();
$SendVars["grant_type"]=("urn:ietf:params:oauth:grant-type:jwt-bearer");
$SendVars["assertion"]=$Jwt;
$SendVars=http_build_query($SendVars);
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $GoogleApiKeyInfo["token_uri"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $SendVars);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)){
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$response=json_decode($response);
return $response;
}
您可以在developers.google.com/identity/protocols/oauth2/scopes找到 $scope 的提示 $proxy 仅在您需要代理且 $timetoexpiration 没有影响时使用,因为您的 accesstoken 始终有效 60 分钟
- 2 回答
- 0 关注
- 130 浏览
添加回答
举报