jesper
jesper

Reputation: 1

Find all my videos and delete them youtube api

I am using PHP Curl to upload videos and it works great. I keep getting this when i try to delete a video:

Http method DELETE is not supported by this URL - Error 405

$headers = array("Authorization: GoogleLogin auth=".$authvalue,
             "GData-Version: 2",
             "Content-Type: application/atom+xml; charset=UTF-8",
             "DELETE /feeds/api/users/".$username."/uploads/".$videoid." HTTP/1.1");

$curl = curl_init("http://gdata.youtube.com");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, 0);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_REFERER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
 $b = curl_exec($curl);    

Upvotes: 0

Views: 1218

Answers (1)

Chad Befus
Chad Befus

Reputation: 1968

First you need a list of youtube IDs for every uploaded video given your account.

  1. The first request you need to make for this is to get the id of your uploaded videos playlist:

    This is a GET request to url:

    "https://www.googleapis.com/youtube/v3/channels"
    

    with headers:

    "Content-type": "application/json",
    "Authorization": "Bearer %s" % {YOUR ACCESS TOKEN}
    

    and parameters:

    "part": "contentDetails",
    "mine": "true",
    "key": {YOUR APPLICATION KEY}
    

    From the response you want to access:

    response_body["items"][0][contentDetails][relatedPlaylists][uploads]

  2. The second request is to get all the ids of the videos you have in your uploads playlist.

    To get this start with a GET request to URL:

    "https://www.googleapis.com/youtube/v3/playlistItems"
    

    sending headers:

    "Content-type": "application/json",
    "Authorization": "Bearer %s" % {YOUR AUTH TOKEN}
    

    and parameters:

    "part": "snippet",
    "maxResults": {MAX 50 -- PAGINATION IS NEEDED},
    "playlistId": {FROM ABOVE},
    "key": {YOUR API KEY}
    

    From this you want to parse out of the response_body["items"] (i) each items and store the youtube id field:

    response_body["items][i]["snippet"]["resourceId"]["videoId"]
    

    if the response has response_body["nextPageToken"] in it you need to resend the request with parameter "pageToken": {NEXT PAGE TOKEN} to get the rest of your paginated results.

  3. To delete a video using the youtube API you need to make a single DELETE request for each video. Use the url:

    "https://www.googleapis.com/youtube/v3/videos"
    

    send 2 headers:

    "Content-type": "application/json",
    "Authorization": "Bearer %s" % {YOUR VALID ACCESS TOKEN}
    

    send 1 parameter:

    "id": {THE YOUTUBE ID FOR THE VIDEO}
    

    On success you should either get a 204 Deleted or 404 Not Found.

    If you get 500, 502, 503, 504 response then you should retry the request (after a short wait).

Upvotes: 2

Related Questions