File size: 3,836 Bytes
ae650a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
YouTube OAuth Token Generator for Crown Commend
Bu script'i bir kez çalıştırıp token.json oluşturacaksın.

Kullanım:
1. client_secret_XXXXX.json dosyasını bu klasöre kopyala
2. Dosya adını 'client_secret.json' olarak değiştir
3. Bu script'i çalıştır: python generate_youtube_token.py
4. Tarayıcıda Google hesabınla giriş yap
5. token.json dosyası oluşacak
6. token.json içeriğini COMMEND_TOKEN_JSON environment variable'a koy
"""

import os
import json
from google_auth_oauthlib.flow import InstalledAppFlow
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request

# YouTube yorum yazma için gerekli scope
SCOPES = ['https://www.googleapis.com/auth/youtube.force-ssl']

def main():
    creds = None

    # Mevcut token varsa kontrol et
    if os.path.exists('token.json'):
        print("Mevcut token.json bulundu, kontrol ediliyor...")
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)

    # Token yoksa veya geçersizse yeni token al
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            print("Token expired, refreshing...")
            creds.refresh(Request())
        else:
            # client_secret.json dosyasını kontrol et
            if not os.path.exists('client_secret.json'):
                print("\n" + "="*60)
                print("HATA: client_secret.json dosyası bulunamadı!")
                print("="*60)
                print("\nYapman gerekenler:")
                print("1. Google Cloud Console'dan indirdiğin")
                print("   client_secret_XXXXX.json dosyasını bu klasöre kopyala")
                print("2. Dosya adını 'client_secret.json' olarak değiştir")
                print("3. Bu script'i tekrar çalıştır")
                print("="*60 + "\n")
                return

            print("\nTarayıcı açılacak, Google hesabınla giriş yap...")
            print("(Test kullanıcısı olarak eklediğin hesapla giriş yapmalısın)\n")

            flow = InstalledAppFlow.from_client_secrets_file(
                'client_secret.json',
                SCOPES
            )
            creds = flow.run_local_server(port=8080)

        # Token'ı kaydet
        with open('token.json', 'w') as token_file:
            token_file.write(creds.to_json())
        print("\n✓ token.json başarıyla oluşturuldu!")

    # Token içeriğini göster
    print("\n" + "="*60)
    print("COMMEND_TOKEN_JSON için kullanacağın değer:")
    print("="*60)

    with open('token.json', 'r') as f:
        token_data = json.load(f)

    # Tek satır JSON olarak yazdır (environment variable için)
    token_json_str = json.dumps(token_data)
    print("\n" + token_json_str)

    print("\n" + "="*60)
    print("Yukarıdaki JSON'u kopyala ve COMMEND_TOKEN_JSON")
    print("environment variable olarak ayarla.")
    print("="*60 + "\n")

    # Test: Token çalışıyor mu?
    print("Token test ediliyor...")
    try:
        from googleapiclient.discovery import build
        youtube = build('youtube', 'v3', credentials=creds)

        # Kendi kanalını çek
        request = youtube.channels().list(
            part="snippet",
            mine=True
        )
        response = request.execute()

        if response.get('items'):
            channel_name = response['items'][0]['snippet']['title']
            print(f"✓ Token çalışıyor! Bağlı kanal: {channel_name}")
        else:
            print("⚠ Token çalışıyor ama YouTube kanalı bulunamadı.")
            print("  YouTube'da bir kanal oluşturduğundan emin ol.")
    except Exception as e:
        print(f"⚠ Token test hatası: {e}")
        print("  Ama token.json oluşturuldu, deneyebilirsin.")

if __name__ == '__main__':
    main()