The aim is to create a Watch Later button using the YouTube API. When a user clicks the button, the video is saved into the user’s Watch Later playlist. Similar to how it works when you implement a Facebook like button on your own site.
So far, we have two official entries in the API documentations:
- Retrieving and updating a users playlist.
- Adding video to playlist: describes a method of forming an XML text format that automatically adds a video to the Watch Later playlist.
And we have a number of PHP code samples on the API docs.
How can this be achieved using either PHP or/and javascript?
3
Answers
I don’t know if this will help you much since I never worked with Youtube’s API. But have a look at this answer on how to get the Watch Later playlist using Youtube’s API v3.
Then you can take a look this, from Youtube docs, for inserting an element into a given playlist.
ANYWAY I’m not sure this is possible anymore (you can give it a try) since September 15, 2016 revision:
Referring to point 2.2 in the list:
The Watch Later playlist is the playlist with id
WL
. You can add a video to this playlist the same way as the other Youtube playlists.You will first need to go to your Google developer console :
Oauth Client ID
Then you can use the code below which will authenticate, retrieve an access token with
https://www.googleapis.com/auth/youtube
scope and then add a video to your watch later playlist.For the following Javascript & PHP samples, when a button is pressed, it logs-in the user if not already authenticated and add the video to the watch later playlist of the authenticated user.
Javascript
This is based on api-samples provided by Google here.
Here is a live demo with the source code (as below)
Here is a fiddle. Replace your client id and add as Authorized JavaScript origins in developer console : https://fiddle.jshell.net
index.html :
Replace
OAUTH2_CLIENT_ID
with your own client IDIn the API response, I check the following status code :
409
: the video already on playlist404
: the video isn’t foundPHP
Based on google-api php sample :
install google-api client :
The php script
watchlater.php
:Replace
$OAUTH2_CLIENT_ID
and$OAUTH2_CLIENT_SECRET
with their respective value. Also, you have to set a redirect_uri in google console, here it will behttp://localhost/watchlater.php
In the PHP version, you can see that I store video id in
$_SESSION["video"]
to be able to add it immediately when google authentication redirect towatchlater.php
Here is a screen of google console
Oauth Client ID
for this project (valid for the Javascript & PHP version above) :Note that :
CLIENT_ID
and set Javascript OriginCLIENT_ID
,CLIENT_SECRET
, set Javascript Origin and set Redirect URINote for testing, I noticed that it can take some time to delete the video when doing it manually if you want to re-add it again
Retrieving "Watch Later" playlist:
As pointed out by others, YouTube no longer allow this
Adding a video to that same list is rather straight forward though:
You can find Javascript code snippet and more here: https://developers.google.com/youtube/v3/docs/playlistItems/insert