很长时间以来,我一直试图解决这个问题。 我需要使用服务帐户生成令牌访问权才能使用Google日历应用程序。 首先,我根据基于Pem证书的firebase / php-jwt库执行JWT,在其中存储Google服务帐户提供的私钥 在Google中进行卷曲以获取上述令牌后,但抛出以下错误““ invalid_grant / Invalid JWT Signature”
Workbooks("Nutrition Update.xlsm").Worksheets("Breakfast").Range("B7:P100").Value = _
Workbooks("Nutrition.xlsm").Worksheets("Breakfast").Range("B7:P100").Value
Workbooks("Nutrition Update.xlsm").Worksheets("Lunch").Range("B7:P100").Value = _
Workbooks("Nutrition.xlsm").Worksheets("Lunch").Range("B7:P100").Value
Workbooks("Nutrition Update.xlsm").Worksheets("Dinner").Range("B7:P100").Value = _
Workbooks("Nutrition.xlsm").Worksheets("Dinner").Range("B7:P100").Value
我向Google卷曲以获得访问令牌的示例
$time = time();
$key = file_get_contents("./certs/certificado.pem");
$token = array(
"iss" => "*****@*******.iam.gserviceaccount.com",
"aud" => "https://www.googleapis.com/oauth2/v3/token",
"iat" => $time,
"exp" => $time+60,
"scope" => "https://www.googleapis.com/auth/calendar.readonly"
);
$jwt = JWT::encode($token, $key);
$decoded = JWT::decode($jwt, $key, array('HS256'));
echo $jwt;
print_r($decoded);
$decoded_array = (array) $decoded;
$grant_type = "urn:ietf:params:oauth:grant-type:jwt-bearer";
JWT::$leeway = 600;
$decoded = JWT::decode($jwt, $key, array('HS256'));
Google出现以下错误
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.googleapis.com/oauth2/v3/token");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"grant_type=".urlencode($grant_type)."&assertion=".urlencode($jwt));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close ($ch);
echo($result);