Php 从Google Api请求用户电子邮件返回未经授权的401错误
我尝试连接到google API,它返回:Php 从Google Api请求用户电子邮件返回未经授权的401错误,php,http-headers,file-get-contents,oauth-2.0,Php,Http Headers,File Get Contents,Oauth 2.0,我尝试连接到google API,它返回: 无法打开流:HTTP请求失败!HTTP/1.0 401未经授权 我请求访问令牌,它返回我xxx,然后我通过以下PHP代码连接到Google API: $opts = array('http' => array( 'method' => 'GET', 'header' => "Authorization: OAuth $access_token\r\nContent-Type: appl
无法打开流:HTTP请求失败!HTTP/1.0 401未经授权
我请求访问令牌,它返回我xxx,然后我通过以下PHP代码连接到Google API:
$opts = array('http' =>
array(
'method' => 'GET',
'header' => "Authorization: OAuth $access_token\r\nContent-Type: application/json\r\n",
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://www.googleapis.com/userinfo/email?alt=json',false,$context);
我在哪里写错了标题或其他什么?
更新:$access\u token是我在json中请求得到的值。- GET请求不需要内容类型头
- 您需要查询此URL以获取用户信息:
- 确保
值包含正确授权的访问令牌,因为您需要用户通过OAUth 2.0流$access\u token
所有这些都记录在这里:检查如何在php中完成,但是在将客户端登录到Google时,必须设置RequestPermission参数,以便从用户的配置文件中获取信息 请求权限:'https://www.googleapis.com/auth/userinfo.profile“,