0

My app dynamically generates this URL:

https://www.youtube.com/watch_videos?video_ids=1EPwH8cXB-M,QohH89Eu5iM,OMKiiSMrL5w,QohH89Eu5iM,RG3ik4VacAA,QohH89Eu5iM,e5lPjg0GhFM,QohH89Eu5iM,QsL7H856rGs,QohH89Eu5iM,lO5qDs2iEpE,QohH89Eu5iM,ZYaWAHJZXm0.

When the user accesses the URL, YouTube automatically converts it to this URL:

https://www.youtube.com/watch?v=1EPwH8cXB-M&list=TLGGIgrODYWxuBUyMTExMjAyMA.

It means that YouTube has condensed the video lists into a list ID. This is great since I could now just share this shorter URL.

How can I dynamically generate this second URL based on the list of videos I intent to share? In other words, how can I generate list id?

Thank you!


Update

[text added by stvar based on OP's comment below]

This is my poorly attempt of google app script which returns the error "Text result of getYoutubeShortURL is longer than the limit of 50000 characters."

function getYoutubeShortURL(longURL){
  var longURLResponse = UrlFetchApp.fetch(longURL);
  return longURLResponse.getContentText()
}

I have a Google sheet with cell A1 that returns the first URL I shared above, then have another column B with the formula =getYoutubeShortURL(A1); that is supposed to return:

https://www.youtube.com/watch?v=1EPwH8cXB-M&list=TLGGIgrODYWxuBUyNzExMjAyMA.

stvar
  • 4,752
  • 2
  • 8
  • 19
Yaw
  • 1
  • 1

1 Answers1

1

That is quite simple. However, you did not specified your programming environment; I will therefore exemplify the solution to your issue using curl at a GNU/Linux bash prompt (using curl at a Windows command prompt window works very much the same way).

First, you have to remark that invoking a HTTP GET method on the URL:

https://www.youtube.com/watch_videos?video_ids=1EPwH8cXB-M,QohH89Eu5iM,OMKiiSMrL5w,QohH89Eu5iM,RG3ik4VacAA,QohH89Eu5iM,e5lPjg0GhFM,QohH89Eu5iM,QsL7H856rGs,QohH89Eu5iM,lO5qDs2iEpE,QohH89Eu5iM,ZYaWAHJZXm0

will make the YouTube's HTTP server respond with the code 303 See Other:

$ curl \
--silent \
--show-error \
--dump-header - \
--output /dev/null \
'https://www.youtube.com/watch_videos?video_ids=1EPwH8cXB-M,QohH89Eu5iM,OMKiiSMrL5w,QohH89Eu5iM,RG3ik4VacAA,QohH89Eu5iM,e5lPjg0GhFM,QohH89Eu5iM,QsL7H856rGs,QohH89Eu5iM,lO5qDs2iEpE,QohH89Eu5iM,ZYaWAHJZXm0'
HTTP/2 303 
expires: ...
content-type: text/html; charset=utf-8
cache-control: no-cache
strict-transport-security: max-age=31536000
location: https://www.youtube.com/watch?v=1EPwH8cXB-M&list=TLGGIgrODYWxuBUyMTExMjAyMA
x-content-type-options: nosniff
content-length: 0
x-frame-options: SAMEORIGIN
p3p: CP="This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl=ro for more info."
date: ...
server: YouTube Frontend Proxy
x-xss-protection: 0
set-cookie: ...
set-cookie: ...
set-cookie: ...
set-cookie: ...
alt-svc: ...

This means that the server indicates that the actual response to the HTTP request made can be found under a different URL and should be retrieved using a GET method on the resource given by the returned location field. That is the URL you're looking for:

https://www.youtube.com/watch?v=1EPwH8cXB-M&list=TLGGIgrODYWxuBUyMTExMjAyMA.

Now, to get only this URL and nothing more out of curl, you'll have to issue it as shown:

$ curl \
--silent \
--show-error \
--output /dev/null \
--write-out '%{redirect_url}\n' \
'https://www.youtube.com/watch_videos?video_ids=1EPwH8cXB-M,QohH89Eu5iM,OMKiiSMrL5w,QohH89Eu5iM,RG3ik4VacAA,QohH89Eu5iM,e5lPjg0GhFM,QohH89Eu5iM,QsL7H856rGs,QohH89Eu5iM,lO5qDs2iEpE,QohH89Eu5iM,ZYaWAHJZXm0'
https://www.youtube.com/watch?v=1EPwH8cXB-M&list=TLGGIgrODYWxuBUyMTExMjAyMA

(For to run the above curl commands on a Windows machine, replace /dev/null with NUL. Also the backslash character at the end of each line above should be replaced with the caret character, ^. The percent character % should be doubled, i.e. should be replaced with %%, and the single quote characters ' should be replaced with double-quote characters ".)

Things work similarly within any other programming environment that allows issuing HTTP requests. If you'll indicate your environment, I may adapt the solution above such that to match your needs more closely.


Here is a function that uses Google Apps Script's UrlFetchApp for to implement the curl's behavior seen above:

function getYoutubeShortURL(longURL)
{
    var params = {
        followRedirects: false
    };
    var response = UrlFetchApp.fetch(
        longURL, params);
    if (response.getResponseCode() != 303)
        return null;
    var headers = response.getHeaders();
    if ('location' in headers)
        return headers['location'];
    if ('Location' in headers)
        return headers['Location'];
    else
        return null;
}

Note that this function returns null in case the YouTube's HTTP server is responding with a status code different than the expected 303 See Other or, otherwise, when the HTTP response doesn't contain either of the headers location or Location.

stvar
  • 4,752
  • 2
  • 8
  • 19
  • Thank you! I am using a low-code based mobile builder called Appsheet (a Google company). I'm generating the long url via their scripting language which is then store on a google sheet column. I have another column I would I like to use to store the response to the HTTP request. Apparently it is not possible to create a google app script to do so. @Bellave_Jayaram from the Appsheet community helped me by creating a google cloud function which worked at the beginning. I am still interested in finding a solution. https://community.appsheet.com/t/webhook-youtube-api/34888/12 – Yaw Nov 27 '20 at 17:40
  • Then you may post that function; then I may try help you further. – stvar Nov 27 '20 at 17:42
  • This is my poorly attempt of google app script which returns the error "Text result of getYoutubeShortURL is longer than the limit of 50000 characters." function getYoutubeShortURL(longURL){ var longURLResponse = UrlFetchApp.fetch(longURL); return longURLResponse.getContentText() } I have a google sheet with column A that returns the first url I shared with you, then have another column B with the formula =getYoutubeShortURL(A1) that is supposed to return https://www.youtube.com/watch?v=1EPwH8cXB-M&list=TLGGIgrODYWxuBUyNzExMjAyMA – Yaw Nov 27 '20 at 18:29
  • Just in case: https://developers.google.com/apps-script/reference/url-fetch – Yaw Nov 27 '20 at 18:36
  • Thanks a lot. I have copied the code in my app script and pasted the error belong the function. – Yaw Nov 27 '20 at 21:30
  • https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429 I just launched it once... – Yaw Nov 27 '20 at 21:34
  • @Yaw: That is the answer of YouTube's HTTP server. I don't know, but it may well be that the cell/cells that contains/contain `=getYoutubeShortURL(...)` are evaluated multiple times. In my opinion, this is likely subject of a separate SO question, under the tag [tag:google-apps-script] only. – stvar Nov 27 '20 at 21:37
  • Was it working on your end? Will create a new question. Thank you so much! – Yaw Nov 27 '20 at 21:45
  • I looked it up: better have your new question under the tags [tag:google-sheets] and [tag:google-sheets-api], because the respective issue pertains specifically to interactions within a Google sheet. – stvar Nov 27 '20 at 21:52
  • I tested `getYoutubeShortURL` on a trivial sheet having on the `A1` cell your long URL above and `=getYoutubeShortURL(A1)` on the `A3` cell. It worked OK. Please also note that I added the parameter `followRedirects: false` to `UrlFetchApp.fetch`. – stvar Nov 27 '20 at 22:12
  • It works :) Thank you so much – Yaw Nov 27 '20 at 23:09