How to provide Youtube authentication token in PHP cURL

Following this guide, I am trying to get a Youtube subscription. I can get the user authentication token, but I don’t know HOW to send it with my cURL request. The docs just say this:

To request a feed of inbound subscribers, send a GET request to the following URL. Note. For this request, you must provide an authorization token that allows YouTube to verify that the user has allowed access to the resource.

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

When I send a request without a token, I get a 401 user authentication error. I can not find information on how to send a token. Here is my current code:

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

Thanks in advance for your help.

+6
source share
1 answer

Take a look at this guide:

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

You need to add a header containing: Authorization: Media ACCESS_TOKEN

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

You can also pass the token as part of the Get request:

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

Source: https://habr.com/ru/post/944740/


All Articles