Skip to content

Commit

Permalink
Handle YouTube V3 API not activated (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored Jul 23, 2023
1 parent ddd444e commit 68529d1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "youtubeaio"
version = "1.1.3"
version = "1.1.4"
description = "Asynchronous Python client for YouTube V3 API."
authors = ["Joost Lekkerkerker <joostlek@outlook.com>"]
maintainers = ["Joost Lekkerkerker <joostlek@outlook.com>"]
Expand Down
5 changes: 5 additions & 0 deletions src/youtubeaio/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)
from youtubeaio.types import (
AuthScope,
ForbiddenError,
MissingScopeError,
UnauthorizedError,
YouTubeAPIError,
Expand Down Expand Up @@ -86,6 +87,10 @@ async def _check_request_return(self, response: ClientResponse) -> ClientRespons
raise YouTubeResourceNotFoundError
if response.status == 401:
raise UnauthorizedError
if response.status == 403:
response_json = await response.json()
error_message = response_json["error"]["errors"][0]["message"]
raise ForbiddenError(error_message)
if 400 <= response.status < 500:
try:
response.raise_for_status()
Expand Down
35 changes: 35 additions & 0 deletions tests/fixtures/youtube_not_activated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"error": {
"code": 403,
"message": "YouTube Data API v3 has not been used in project 69696969 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=69696969 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 69696969 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=69696969 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=69696969"
}
]
},
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "SERVICE_DISABLED",
"domain": "googleapis.com",
"metadata": {
"service": "youtube.googleapis.com",
"consumer": "projects/69696969"
}
}
]
}
}
23 changes: 23 additions & 0 deletions tests/test_youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from aresponses import Response, ResponsesMockServer

from youtubeaio.types import (
ForbiddenError,
UnauthorizedError,
YouTubeAPIError,
YouTubeBackendError,
Expand Down Expand Up @@ -186,3 +187,25 @@ async def test_unauthorized(
with pytest.raises(UnauthorizedError):
await youtube.get_video(video_id="Ks-_Mh1QhMc")
await youtube.close()


async def test_not_activated(
aresponses: ResponsesMockServer,
) -> None:
"""Test handling YouTube api not activated."""
aresponses.add(
YOUTUBE_URL,
"/youtube/v3/videos",
"GET",
aresponses.Response(
status=403,
headers={"Content-Type": "application/json"},
text=load_fixture("youtube_not_activated.json"),
),
)

async with aiohttp.ClientSession() as session:
youtube = YouTube(session=session)
with pytest.raises(ForbiddenError):
await youtube.get_video(video_id="Ks-_Mh1QhMc")
await youtube.close()

0 comments on commit 68529d1

Please sign in to comment.