Rthur2003 commited on
Commit
ae650a9
·
1 Parent(s): 0406dc2

feat: add YouTube OAuth token generator script

Browse files
Files changed (1) hide show
  1. generate_youtube_token.py +105 -0
generate_youtube_token.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ YouTube OAuth Token Generator for Crown Commend
3
+ Bu script'i bir kez çalıştırıp token.json oluşturacaksın.
4
+
5
+ Kullanım:
6
+ 1. client_secret_XXXXX.json dosyasını bu klasöre kopyala
7
+ 2. Dosya adını 'client_secret.json' olarak değiştir
8
+ 3. Bu script'i çalıştır: python generate_youtube_token.py
9
+ 4. Tarayıcıda Google hesabınla giriş yap
10
+ 5. token.json dosyası oluşacak
11
+ 6. token.json içeriğini COMMEND_TOKEN_JSON environment variable'a koy
12
+ """
13
+
14
+ import os
15
+ import json
16
+ from google_auth_oauthlib.flow import InstalledAppFlow
17
+ from google.oauth2.credentials import Credentials
18
+ from google.auth.transport.requests import Request
19
+
20
+ # YouTube yorum yazma için gerekli scope
21
+ SCOPES = ['https://www.googleapis.com/auth/youtube.force-ssl']
22
+
23
+ def main():
24
+ creds = None
25
+
26
+ # Mevcut token varsa kontrol et
27
+ if os.path.exists('token.json'):
28
+ print("Mevcut token.json bulundu, kontrol ediliyor...")
29
+ creds = Credentials.from_authorized_user_file('token.json', SCOPES)
30
+
31
+ # Token yoksa veya geçersizse yeni token al
32
+ if not creds or not creds.valid:
33
+ if creds and creds.expired and creds.refresh_token:
34
+ print("Token expired, refreshing...")
35
+ creds.refresh(Request())
36
+ else:
37
+ # client_secret.json dosyasını kontrol et
38
+ if not os.path.exists('client_secret.json'):
39
+ print("\n" + "="*60)
40
+ print("HATA: client_secret.json dosyası bulunamadı!")
41
+ print("="*60)
42
+ print("\nYapman gerekenler:")
43
+ print("1. Google Cloud Console'dan indirdiğin")
44
+ print(" client_secret_XXXXX.json dosyasını bu klasöre kopyala")
45
+ print("2. Dosya adını 'client_secret.json' olarak değiştir")
46
+ print("3. Bu script'i tekrar çalıştır")
47
+ print("="*60 + "\n")
48
+ return
49
+
50
+ print("\nTarayıcı açılacak, Google hesabınla giriş yap...")
51
+ print("(Test kullanıcısı olarak eklediğin hesapla giriş yapmalısın)\n")
52
+
53
+ flow = InstalledAppFlow.from_client_secrets_file(
54
+ 'client_secret.json',
55
+ SCOPES
56
+ )
57
+ creds = flow.run_local_server(port=8080)
58
+
59
+ # Token'ı kaydet
60
+ with open('token.json', 'w') as token_file:
61
+ token_file.write(creds.to_json())
62
+ print("\n✓ token.json başarıyla oluşturuldu!")
63
+
64
+ # Token içeriğini göster
65
+ print("\n" + "="*60)
66
+ print("COMMEND_TOKEN_JSON için kullanacağın değer:")
67
+ print("="*60)
68
+
69
+ with open('token.json', 'r') as f:
70
+ token_data = json.load(f)
71
+
72
+ # Tek satır JSON olarak yazdır (environment variable için)
73
+ token_json_str = json.dumps(token_data)
74
+ print("\n" + token_json_str)
75
+
76
+ print("\n" + "="*60)
77
+ print("Yukarıdaki JSON'u kopyala ve COMMEND_TOKEN_JSON")
78
+ print("environment variable olarak ayarla.")
79
+ print("="*60 + "\n")
80
+
81
+ # Test: Token çalışıyor mu?
82
+ print("Token test ediliyor...")
83
+ try:
84
+ from googleapiclient.discovery import build
85
+ youtube = build('youtube', 'v3', credentials=creds)
86
+
87
+ # Kendi kanalını çek
88
+ request = youtube.channels().list(
89
+ part="snippet",
90
+ mine=True
91
+ )
92
+ response = request.execute()
93
+
94
+ if response.get('items'):
95
+ channel_name = response['items'][0]['snippet']['title']
96
+ print(f"✓ Token çalışıyor! Bağlı kanal: {channel_name}")
97
+ else:
98
+ print("⚠ Token çalışıyor ama YouTube kanalı bulunamadı.")
99
+ print(" YouTube'da bir kanal oluşturduğundan emin ol.")
100
+ except Exception as e:
101
+ print(f"⚠ Token test hatası: {e}")
102
+ print(" Ama token.json oluşturuldu, deneyebilirsin.")
103
+
104
+ if __name__ == '__main__':
105
+ main()