-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api.py
43 lines (36 loc) · 1.26 KB
/
test_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
from dotenv import load_dotenv
import os
from googleapiclient.discovery import build
def test_api_key():
# 載入 .env 檔案中的環境變數
load_dotenv()
# 取得 API 密鑰
api_key = os.getenv('YOUTUBE_API_KEY')
if not api_key:
print("錯誤:找不到 API 密鑰,請確認 .env 檔案中已設置 YOUTUBE_API_KEY")
return False
try:
# 建立 YouTube API 服務
youtube = build('youtube', 'v3', developerKey=api_key)
# 測試 API - 取得一個熱門影片的資訊
request = youtube.videos().list(
part="snippet",
chart="mostPopular",
regionCode="TW",
maxResults=1
)
response = request.execute()
# 如果能成功取得資料,表示 API 密鑰有效
if 'items' in response:
video = response['items'][0]
print("API 測試成功!")
print(f"成功取得影片資訊:{video['snippet']['title']}")
return True
except Exception as e:
print(f"錯誤:API 測試失敗 - {str(e)}")
return False
if __name__ == "__main__":
import sys
sys.stdout.reconfigure(encoding='utf-8')
test_api_key()