6

按照指南,我正在尝试检索 Youtube 用户的订阅。我能够获取用户的身份验证令牌,但是我不知道如何通过我的 cURL 请求实际发送它。文档只是这样说:

要请求当前登录用户订阅的提要,请向以下 URL 发送 GET 请求。注意:对于此请求,您必须提供授权令牌,以便 YouTube 验证用户是否已授权访问资源。

https://gdata.youtube.com/feeds/api/users/default/subscriptions?v=2

当我发送没有令牌的请求时,我收到 401 user authentication required 错误。我找不到有关如何发送令牌的任何信息。这是我当前的代码:

$ch_subs = curl_init();
curl_setopt($ch_subs, CURLOPT_URL, 'https://gdata.youtube.com/feeds/api/users/default/subscriptions?v=2');
curl_setopt($ch_subs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_subs, CURLOPT_SSL_VERIFYPEER, false);
$subs_return = curl_exec($ch_subs);
curl_close($ch_subs);

提前感谢您的帮助。

4

1 回答 1

12

看看这个指南:

https://developers.google.com/youtube/2.0/developers_guide_protocol#OAuth2_Calling_a_Google_API

您需要添加一个标头,其中包含: Authorization: Bearer ACCESS_TOKEN

$headers = array('Authorization: Bearer ' . $access_token);
curl_setopt($ch_subs, CURLOPT_HTTPHEADER, $headers);

您还可以将令牌作为 Get 请求的一部分传递:

curl_setopt($ch_subs, CURLOPT_URL, 'https://gdata.youtube.com/feeds/api/users/default/subscriptions?v=2&access_token=' . $access_token);
于 2013-05-10T20:12:13.010 回答