【问题标题】:Requesting user email from Google Api returns unauthorized 401 error从 Google Api 请求用户电子邮件返回未经授权的 401 错误
【发布时间】:2011-09-27 08:33:51
【问题描述】:

我尝试连接到 google API 并返回: failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized 我请求了访问令牌,它返回给我 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_token 是我通过请求在 json 中获得的值。

【问题讨论】:

标签: php http-headers file-get-contents oauth-2.0


【解决方案1】:
  • GET 请求不需要内容类型标头。
  • 您需要查询此 URL 以获取用户信息:https://www.googleapis.com/oauth2/v2/userinfo
  • 确保 $access_token 值包含正确授权的访问令牌,因为您需要您的用户通过 OAUth 2.0 流程。

所有这些都记录在那里:https://developers.google.com/accounts/docs/OAuth2Login

【讨论】:

    【解决方案2】:

    检查如何在 php 中执行此操作,但是当您将客户端登录到 Google 时,您必须设置 RequestPermission 参数,以便您可以从用户的配置文件中获取信息

    requestPermissions: 'https://www.googleapis.com/auth/userinfo.profile'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 2014-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      相关资源
      最近更新 更多