Home > database >  urllib.error.HTTPError: HTTP Error 403: Forbidden Error
urllib.error.HTTPError: HTTP Error 403: Forbidden Error

Time:01-22

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 = "AIzaSyC-t1EEyBswGE2SS0Nz8eFSKrLjbhAOO7E"
    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:\Users\garvi\OneDrive\Desktop\code-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:\Users\garvi\OneDrive\Desktop\code-learning\py1\main.py", line 25, in <module>
    look_for_new_video()
  File "c:\Users\garvi\OneDrive\Desktop\code-learning\py1\main.py", line 12, in look_for_new_video
    inp = urllib.request.urlopen(url)
  File "C:\Users\garvi\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\garvi\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 525, in open
    response = meth(req, response)
  File "C:\Users\garvi\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 634, in http_response
    response = self.parent.error(
  File "C:\Users\garvi\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 563, in error
    return self._call_chain(*args)
  File "C:\Users\garvi\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 496, in _call_chain
    result = func(*args)
  File "C:\Users\garvi\AppData\Local\Programs\Python\Python310\lib\urllib\request.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.

  •  Tags:  
  • Related