[英]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.