urllib.error.HTTPError: HTTP Error 403: Forbidden Error

Question:

I was writing code for a project using Python. I was trying to make a project that tells you when your favorite youtuber uploads his/her video when I encountered an error.

Here is my code:

import time
import urllib.request, json

def look_for_new_video():
    api_key = "AIzaSy..."
    channel_id = "UCd4QrSA9GDlViP_0dwbdSBA"

    base_video_url = "https://www.youtube.com/watch?v="
    base_search_url = "https://www.googleapis.com/youtube/v3/search?"

    url = base_search_url + 'key={}&channelId={}&part=snippet,id&order=date&maxResults=1'.format(api_key, channel_id)
    inp = urllib.request.urlopen(url)
    resp = json.load(inp)

    vidID = resp['items'][0]['id']['videoId']
    video_exists = False
    with open('videoid.json', 'r') as json_file:
        data = json.load(json_file)
        if data['videoId'] != vidID:
            data = {'videoId':vidID, 'channelId':channel_id}
            json.dump(data, json_file)

try:
    while True:
        look_for_new_video()
        time.sleep(10)

except KeyboardInterrupt:
    print('stopping')

And here is the error I encountered:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

PS C:UsersgarviOneDriveDesktopcode-learning> & C:/Users/garvi/AppData/Local/Programs/Python/Python310/python.exe c:/Users/garvi/On
eDrive/Desktop/code-learning/py1/main.py
Traceback (most recent call last):
  File "c:UsersgarviOneDriveDesktopcode-learningpy1main.py", line 25, in <module>
    look_for_new_video()
  File "c:UsersgarviOneDriveDesktopcode-learningpy1main.py", line 12, in look_for_new_video
    inp = urllib.request.urlopen(url)
  File "C:UsersgarviAppDataLocalProgramsPythonPython310liburllibrequest.py", line 216, in urlopen
    return opener.open(url, data, timeout)
  File "C:UsersgarviAppDataLocalProgramsPythonPython310liburllibrequest.py", line 525, in open
    response = meth(req, response)
  File "C:UsersgarviAppDataLocalProgramsPythonPython310liburllibrequest.py", line 634, in http_response
    response = self.parent.error(
  File "C:UsersgarviAppDataLocalProgramsPythonPython310liburllibrequest.py", line 563, in error
    return self._call_chain(*args)
  File "C:UsersgarviAppDataLocalProgramsPythonPython310liburllibrequest.py", line 496, in _call_chain
    result = func(*args)
  File "C:UsersgarviAppDataLocalProgramsPythonPython310liburllibrequest.py", line 643, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

Please help me with this issue as it will mean the world to me. Thanks.

Asked By: Garvit Nagpal

||

Answers:

If you check full response, you will see:

{
  "error": {
    "code": 403,
    "message": "YouTube Data API v3 has not been used in project 169818930380 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=169818930380 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "errors": [
      {
        "message": "YouTube Data API v3 has not been used in project 169818930380 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=169818930380 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
        "domain": "usageLimits",
        "reason": "accessNotConfigured",
        "extendedHelp": "https://console.developers.google.com"
      }
    ],
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developers console API activation",
            "url": "https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=169818930380"
          }
        ]
      },
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "SERVICE_DISABLED",
        "domain": "googleapis.com",
        "metadata": {
          "consumer": "projects/169818930380",
          "service": "youtube.googleapis.com"
        }
      }
    ]
  }
}

So you have to follow this instructions: "YouTube Data API v3 has not been used in project 169818930380 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=169818930380 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry."

Answered By: Oleksii Tambovtsev