我以前使用过此代码,可以收到电子邮件,但现在无法正常工作....为什么?
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "https://oauth2.googleapis.com/tokeninfo?id_token=".$id,
));
$resp = curl_exec($curl);
curl_close($curl);
try{
$resp = json_decode($resp,true);
if(empty($resp['error']))
return $resp;
else return null;
}catch (\Exception $e)
{
return null;
}
$id
是access_token /
我得到名称,图片,...但是数组中没有电子邮件
答案 0 :(得分:0)
简单的答案来自文档:
仅当用户已向应用程序授予“电子邮件” OAuth范围时,才包括这些字段。
大约2个月前google shutdown google + api,如果您希望将代码添加到获取令牌的位置,也许我们可以为您提供更多帮助。
答案 1 :(得分:0)
我们有两种方法可以解决此问题:
access_token
大多数程序员都不知道access_token
和id_token
之间的区别,因此错误地使用access_token
而不是id_token
。 access_token
是比id_token
短的字符串,您应该运行以下代码来获取电子邮件:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.googleapis.com/oauth2/v3/userinfo",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Authorization: Bearer ya29.GlsJBxmiZkSmgMEUlhrtytyJEEI45uQTR8xmmrsZulz7kEOtyrtyytrycyPxCtg_TZ_hz3x5zI5fRlqgJ9ARCnQIEFZaO5o75ZbwicwFGFf_y7PZ8T",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: www.googleapis.com",
"Postman-Token: 55ft6892-d0hc-43b0-af22-5eeced9155b5,58b4bd70-355c-4293-933c-30b1c0982b67",
"User-Agent: PostmanRuntime/7.11.0",
"accept-encoding: gzip, deflate",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {echo "cURL Error #:" . $err;} else {echo $response;}
最后我得到了access_token
和id_token
的一些样本
access_token
:
ya29.GlsJB84Op0099CWZzGrazU449yFBRX1IwA7kjQvUbISnzaP1tGrqzcH7Pdo-vynDF9GZ7L9El_cvMrX2UBedIcq00Ml4Id5wb1459qpxfB3IzHv7dFwTKyV28E5Y
id_token
:
eyJhbGciOiJSUzI1NiIsImtpZCI6IjJjM2ZhYzE2YjczZmM4NDhkNDI2ZDVhMjI1YWM4MmJjMWMwMmFlZmQiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLm67b2dsZS5jb20iLCJhenAiOiI3MjI1NDU1MjAwODItdGNycTRhZzZpZjJwZmVsbXEwN2xoM2xlMm5uamljdHIuYXB67uy5nb29nbGV1c2VyY29udGVudC5jb20iLCJhdWQiOiI3MjI1NDU1MjAwODI456NycTRhZzZpZjJwZmVsbX67N2xoM2xlMm5uamljdHIuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMDg0MDMxNzUyMzc0NDg5OTI2OTQiLCJlbWFpbCI6Im5pZ2h0LmV4dGVybkBnbWFpbC5jb20iLCJlbWFpbF92ZXJpZmllZ567dHJ1ZSwiYXRfaGFzaCI6IkIyVi1GenM5WjVHRm4wQjNEa0FUMXciLCJpYXQiOjE1NTc4Mjc3MTgsImV4cCI6MTU1NzgzMTgh7H0.S6VNWWcSzOqpNdOBBAY-tDmFJQu0ZTU5RSalYnsC0P4KuW6ts8f61PQT5X2a1DXjgV6vj3iu9T1Apj8RzvYezakruLt7doB9CI9vEr1hpOwCcQ3W_o8MkZWZUtU1i4heXM7k3uW9LQy5fa3mdY4pWX0YEa6x--eAnwvUHuJzrRIfn-boV4Rl1LkkCS7AKKMYpOC-_Gej5P9eu5xc7d-xtFr_Cv8ne5pnQKk-5Un9f3IV5SW9mi9AOJI_MdmKg1n-P495TAk5wrLUME5UOJTHgleh__TmfVZwpvvc-UVKtRObJhANubqqiTA_E5O1-TSdrj9yNNhldyxsL6Xb5MfsJQ`