php - 如何在 PHP cURL 中提供 Youtube 身份验证 token

标签 php api curl youtube token

正在关注 this指南,我正在尝试检索 Youtube 用户的订阅。我能够获得用户的身份验证 token ,但我不知道如何使用我的 cURL 请求实际发送它。文档只是这样说:

To request a feed of the currently logged-in user's subscriptions, send a GET request to the following URL. Note: For this request, you must provide an authorization token, which enables YouTube to verify that the user authorized access to the resource.

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

当我在没有 token 的情况下发送请求时,我收到 401 需要用户身份验证错误。我找不到有关如何发送 token 的任何信息。这是我当前的代码:

$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);

在此先感谢您的帮助。

最佳答案

查看本指南:

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

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

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

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

curl_setopt($ch_subs, CURLOPT_URL, 'https://gdata.youtube.com/feeds/api/users/default/subscriptions?v=2&access_token=' . $access_token);

关于php - 如何在 PHP cURL 中提供 Youtube 身份验证 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16489922/

相关文章:

java - 是否有 Java API 可以获取 VM 参数?

php SOAP 响应为空,但__last_response不是

php - 查询在 MYSql QUERY php 中不起作用

javascript - 使用 History API 时,我应该如何处理返回到加载的初始页面

api - 在没有 Redux 的情况下难以在 REACTjs 中执行 GET 请求

rest - 学习 Clojure : unable to fix

php - 在 Codeigniter 中连接两个数据库

php - 我的登录无法正常工作,我不明白为什么? [请帮忙]

php - simplexml_load_string() 不会读取标签中带有 "soap:"的 soap 响应

curl - Jive REST API 返回 {"code":401 ,"message" :"Bad credentials"} using Basic Auth