简体   繁体   English

在进行Google + API调用时接收HTTP 401未经授权

[英]Receiving HTTP 401 Unauthorized when making a Google plus API call

I'm trying to get the list of all my friends from the Google plus via API. 我正试图通过API从Google plus获取所有朋友的列表。 The user on whose behalf I'm doing this operation previously authorized my request and I got the auth token. 我正在执行此操作的用户先前已授权我的请求,并且我获得了身份验证令牌。 I've tried the following code in php: 我在php中尝试了以下代码:

function CallAPI() {
  $opts = array(
    "http" => array(
      "method" => "GET"
    )
  );

  $url = 'https://www.googleapis.com/plus/v1/people/me/people/visible?key=XXXX';    
  $context = stream_context_create($opts);
  $response = file_get_contents($url, false, $context);
  var_dump($response);
}

but I keep receiving HTTP request failed! 但我一直收到HTTP请求失败! HTTP/1.0 401 Unauthorized. HTTP / 1.0 401未经授权。 How can I prove that the user authorized my operations or what am I doing wrong? 如何证明用户授权我的操作或我做错了什么? Any help is much appreciated. 任何帮助深表感谢。

You need to authenticate the user to use the special keyword "me" so using your simple API key will not work (assuming the key passed is your simple key). 您需要对用户进行身份验证才能使用特殊关键字“me”,因此使用简单的API密钥将无效(假设传递的密钥是您的简单密钥)。 Instead, you need to get an access token and pass that. 相反,您需要获取访问令牌并传递它。

For a great example of how to do this in PHP using the PHP client library, try the quickstart: 有关如何使用PHP客户端库在PHP中执行此操作的一个很好的示例,请尝试快速入门:

https://developers.google.com/+/quickstart/php https://developers.google.com/+/quickstart/php


If you are already getting an access token, you can call tokeninfo passing access token to get more information about who the user is associated with it: 如果您已获得访问令牌,则可以调用tokeninfo传递访问令牌以获取有关用户与之关联的更多信息:

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=ya29.xxxxxxxx ... https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=ya29.xxxxxxxx ...

At this point, you could call: 'https://www.googleapis.com/plus/v1/people/[useridfromaccesstoken]/people/visible?key=XXXX'; 此时,你可以打电话:'https://www.googleapis.com/plus/v1/people/[useridfromaccesstoken]/people/visible?key=XXXX';

To verify that your API key is correct but I would recommend using the client library as demonstrated in the quickstart sample. 要验证您的API密钥是否正确,但我建议使用快速入门示例中演示的客户端库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 401 Unauthorized 尝试调用谷歌云函数时 - 401 Unauthorized When trying to call a google cloud functions (401)尝试使用Google+ API插入信息时未经授权 - (401) Unauthorized when trying to insert a moment using the Google+ API “ Google MC Funnels API”(未经授权的401) - “Google MC Funnels API” (401 Unauthorized) 401 未经 Adyen API 授权 - 401 Unauthorized on Adyen API 为什么我在 Microsoft Graph API 调用中收到“401 Unauthorized”? - Why am I Getting "401 Unauthorized" for Microsoft Graph API Call? 从 Google Api 请求用户 email 返回未经授权的 401 错误 - Requesting user email from Google Api returns unauthorized 401 error 使用PHP在Google+ API moments.insert上未经授权的401 - Unauthorized 401 on Google+ API moments.insert with PHP Google Events API 返回 401 错误未授权_client - Google Events API returning 401 error unauthorized_client 当我尝试使用PHP访问JIRA REST API时未经授权(401) - Unauthorized (401) when I try to access JIRA REST API with PHP 访问 prestashop api 网络服务时出现“401 - 未经授权” - Getting "401 - unauthorized" when accessing the prestashop api webservice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM