0

I am using this code in PHP to get the Youtube server URL

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.youtube.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, '{
      "videoId": "'.$yt_video_id.'",
      "context": {
        "client": {
          "clientName": "ANDROID",
          "clientVersion": "16.20.35"
        },
        "device": {
          "platform": "ANDROID",
          "platformVersion": "11"
        }
      },
      "playbackMode": "EMBEDDED",
      "playerParams": {
        "autoplay": 1
      }
    }');
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');

    $headers = array();
    $headers[] = 'Content-Type: application/json';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);
    echo '<pre>';print_r($result); 

It is working fine and returns the video info along with the formats and adaptive formats. The returned server URL also has the expire field in the URL itself which means the returned media URL will expire after that time.

I added the logic to save expire in the database and reran the above code to fetch the new media URL when the current time is greater than the previous expire time.

The problem is that sometime the above curl request returns the following error, but when I refresh, it works:

"reason": {
  "runs": [{
    "text": "The following content is not available on this app."
  }]
}

I am afraid that this can happen with my live videos and the user will not be able to watch video.

What am I doing wrong, and what is the best way to handle the expiration? What catch I am missing here? I have seen that many other video players are working fine without errors.

UPDATE - If I try it with "WEB" as client then it works every time. I am following @Mahad answer for web from this link get_video_info YouTube endpoint suddenly returning 404 not found.

Bhuvnesh Gupta
  • 197
  • 2
  • 12
  • Does this happen with any video_id? - please provide some video_ids examples. – Marco Aurelio Fernandez Reyes Apr 18 '23 at 14:05
  • Hi @MarcoAurelioFernandezReyes yes this is happening with any video_id. E.g you can try with this rxgF5Ng2Exw . I just tried this 4 times. And out of 3 times it worked once and first two times it gave the above error. – Bhuvnesh Gupta Apr 19 '23 at 08:29
  • Did you trimmed the error response or this is the full response? - I can't tell, but, it *might* be that the IP is being blacklisted or something related to it. Maybe try by updating the value of `clientVersion` attribute and add more attributes to the payload - i.e. the values you're setting to `CURLOPT_POSTFIELDS`. – Marco Aurelio Fernandez Reyes Apr 19 '23 at 13:17
  • Hi @MarcoAurelioFernandezReyes this is the error part of the response, rest response is just basic like params etc. And I dont think its IP blocked because as I mentioned that when I try with WEB as client, it works every time. I think the latest version for Android is 16.20.35 I am currently trying with it only. I am updating my question also with this version. I have also tried adding all the parameters of sample request of WEB client which I found here https://stackoverflow.com/questions/67615278/get-video-info-youtube-endpoint-suddenly-returning-404-not-found in Mahad answer but no luck – Bhuvnesh Gupta Apr 19 '23 at 13:38
  • Hi, you have your key exposed – pierpy Apr 20 '23 at 18:31
  • Hi @pierpy this is universal key – Bhuvnesh Gupta Apr 24 '23 at 06:33
  • @BhuvneshGupta I was not aware of a universal key. Are using that in your project? – pierpy Apr 24 '23 at 06:36
  • Hi @pierpy I am doing some research on this for my project. Check this https://stackoverflow.com/questions/67615278/get-video-info-youtube-endpoint-suddenly-returning-404-not-found – Bhuvnesh Gupta Apr 24 '23 at 10:11

1 Answers1

0

Not 100% sure, but in your code you are using a universal apikey; so, chances to get randomly blocked are really high. Try to apply for a regular key, and request an increase of quota if you are hitting the 10000 limit too frequently.
With the reliability of a universal key, none of us would ever use our own personal data, in fact invalidating their control routines.

pierpy
  • 897
  • 3
  • 14
  • 18
  • Hi @pierpy, only universal key is used for this. And if IP is blocking then it should also block when I use "WEB" as client name but its not and returning response every time. I have tried 100 times in a row and it worked for all 100 but in case of Android it does not work for 10 times in a row. – Bhuvnesh Gupta Apr 24 '23 at 10:10