Skip to content

Commit 68529d1

Browse files
authored
Handle YouTube V3 API not activated (#61)
1 parent ddd444e commit 68529d1

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "youtubeaio"
3-
version = "1.1.3"
3+
version = "1.1.4"
44
description = "Asynchronous Python client for YouTube V3 API."
55
authors = ["Joost Lekkerkerker <[email protected]>"]
66
maintainers = ["Joost Lekkerkerker <[email protected]>"]

src/youtubeaio/youtube.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
)
2121
from youtubeaio.types import (
2222
AuthScope,
23+
ForbiddenError,
2324
MissingScopeError,
2425
UnauthorizedError,
2526
YouTubeAPIError,
@@ -86,6 +87,10 @@ async def _check_request_return(self, response: ClientResponse) -> ClientRespons
8687
raise YouTubeResourceNotFoundError
8788
if response.status == 401:
8889
raise UnauthorizedError
90+
if response.status == 403:
91+
response_json = await response.json()
92+
error_message = response_json["error"]["errors"][0]["message"]
93+
raise ForbiddenError(error_message)
8994
if 400 <= response.status < 500:
9095
try:
9196
response.raise_for_status()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"error": {
3+
"code": 403,
4+
"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.",
5+
"errors": [
6+
{
7+
"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.",
8+
"domain": "usageLimits",
9+
"reason": "accessNotConfigured",
10+
"extendedHelp": "https://console.developers.google.com"
11+
}
12+
],
13+
"status": "PERMISSION_DENIED",
14+
"details": [
15+
{
16+
"@type": "type.googleapis.com/google.rpc.Help",
17+
"links": [
18+
{
19+
"description": "Google developers console API activation",
20+
"url": "https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=69696969"
21+
}
22+
]
23+
},
24+
{
25+
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
26+
"reason": "SERVICE_DISABLED",
27+
"domain": "googleapis.com",
28+
"metadata": {
29+
"service": "youtube.googleapis.com",
30+
"consumer": "projects/69696969"
31+
}
32+
}
33+
]
34+
}
35+
}

tests/test_youtube.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from aresponses import Response, ResponsesMockServer
88

99
from youtubeaio.types import (
10+
ForbiddenError,
1011
UnauthorizedError,
1112
YouTubeAPIError,
1213
YouTubeBackendError,
@@ -186,3 +187,25 @@ async def test_unauthorized(
186187
with pytest.raises(UnauthorizedError):
187188
await youtube.get_video(video_id="Ks-_Mh1QhMc")
188189
await youtube.close()
190+
191+
192+
async def test_not_activated(
193+
aresponses: ResponsesMockServer,
194+
) -> None:
195+
"""Test handling YouTube api not activated."""
196+
aresponses.add(
197+
YOUTUBE_URL,
198+
"/youtube/v3/videos",
199+
"GET",
200+
aresponses.Response(
201+
status=403,
202+
headers={"Content-Type": "application/json"},
203+
text=load_fixture("youtube_not_activated.json"),
204+
),
205+
)
206+
207+
async with aiohttp.ClientSession() as session:
208+
youtube = YouTube(session=session)
209+
with pytest.raises(ForbiddenError):
210+
await youtube.get_video(video_id="Ks-_Mh1QhMc")
211+
await youtube.close()

0 commit comments

Comments
 (0)