Search code examples
python-3.xyoutube-data-apiplaylist

Python - youtube.playlistItems().delete() doesn't work with [Deleted video] (YouTube API v3)


I want to clean the remnant [Deleted video] of several playlist of my YouTube channel. I'm using this code but it doesn't work.

YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
CLIENT_SECRETS_FILE = "client_secrets.json"
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"



def get_authenticated_service(args):
  flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
    scope=YOUTUBE_READ_WRITE_SCOPE,
    message=MISSING_CLIENT_SECRETS_MESSAGE)

  storage = Storage("%s-oauth2.json" % sys.argv[0])
  credentials = storage.get()

  if credentials is None or credentials.invalid:
    credentials = run_flow(flow, storage, args)

  return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    http=credentials.authorize(httplib2.Http()))





 if __name__ == "__main__":

 try:

  args = argparser.parse_args() 
  youtube = get_authenticated_service(args)
  youtube.playlistItems().delete(id="xxxxxxxxx").execute() 

except HttpError as e:
 print ("\nAn HTTP error %d occurred:\n%s" % (e.resp.status, e.content))

I get this error massage (403)(Forbidden)

The request is not properly authorized to delete the specified playlist item

{
 "error": {
  "errors": [
   {
    "domain": "youtube.playlistItem",
    "reason": "playlistItemsNotAccessible",
    "message": "Forbidden",
    "locationType": "parameter",
    "location": "id"
   }
  ],
  "code": 403,
  "message": "Forbidden"
 }
}

Even using this (Try this API) from here:

https://developers.google.com/youtube/v3/docs/playlistItems/delete?hl=en-419

or here

https://developers.google.com/youtube/v3/docs/playlistItems/delete?hl=es-419

My credentials, my developer Key and my client_secrets.json file are good, becouse i used it before and its works.

Someone knows what is happend? Or someone knows other way to remove "Deleted video" from playlist using Python + Youtube API v3?


Solution

  • The problem was solved:

    If you execute PlaylistItems().list(), you get this response.

    "items": [
      {
       "kind": "youtube#playlistItem",
       "etag": "\"DuHzAJ-eQIiCIp7p4ldoVcVAOeY/Ktqi5NIapmys1w2V0FiorhFR-Uk\"",
       "id": "UExES3pRck8tTUFDZndHV3Z0eXVaVHZXNENxNTNGYV9wNC4wMTcyMDhGQUE4NTIzM0Y5",
       "snippet": {
        "publishedAt": "2018-06-06T13:43:17.000Z",
        "channelId": "xxxxxxxxxxxxxxxxxx",
        "title": "Deleted video",
        "description": "This video is unavailable.",
        "channelTitle": "xxxxxxxxxxxxxxxxxx",
        "playlistId": "xxxxxxxxxxxxxxxxxxxxxxx",
        "position": 0,
        "resourceId": {
         "kind": "youtube#video",
         "videoId": "D6NOeUfxCnM"
        }
    

    for delete items from playlist you must to use this

       "id": "UExES3pRck8tTUFDZndHV3Z0eXVaVHZXNENxNTNGYV9wNC4wMTcyMDhGQUE4NTIzM0Y5",
    

    if you use this "videoId": "D6NOeUfxCnM" you get the error massage (403)(Forbidden)