正在关注 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/