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
.