-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_gmail.py
More file actions
269 lines (217 loc) · 9.45 KB
/
debug_gmail.py
File metadata and controls
269 lines (217 loc) · 9.45 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python3
"""
Script de debug detalhado para problemas de conexão Gmail API
"""
import os
import pickle
import json
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
SCOPES = ['https://www.googleapis.com/auth/gmail.modify']
def check_files():
"""Verifica se os arquivos necessários existem"""
print("🔍 Verificando arquivos necessários...")
files_to_check = [
('credentials.json', 'Arquivo de credenciais OAuth'),
('token.pickle', 'Token de autenticação'),
]
for filename, description in files_to_check:
if os.path.exists(filename):
size = os.path.getsize(filename)
print(f"✅ {filename} - {description} ({size} bytes)")
else:
print(f"❌ {filename} - {description} (NÃO ENCONTRADO)")
print()
def check_credentials_file():
"""Verifica se o arquivo de credenciais é válido"""
print("🔍 Verificando arquivo de credenciais...")
if not os.path.exists('credentials.json'):
print("❌ Arquivo credentials.json não encontrado!")
return False
try:
with open('credentials.json', 'r') as f:
creds_data = json.load(f)
required_fields = ['installed', 'client_id', 'client_secret', 'auth_uri', 'token_uri']
if 'installed' in creds_data:
installed = creds_data['installed']
for field in required_fields:
if field in installed:
print(f"✅ {field}: {installed[field][:20]}...")
else:
print(f"❌ {field}: FALTANDO")
return False
else:
print("❌ Campo 'installed' não encontrado no JSON")
return False
print("✅ Arquivo de credenciais parece válido")
return True
except json.JSONDecodeError as e:
print(f"❌ Erro ao decodificar JSON: {e}")
return False
except Exception as e:
print(f"❌ Erro ao ler arquivo: {e}")
return False
def check_token_file():
"""Verifica se o token é válido"""
print("\n🔍 Verificando token de autenticação...")
if not os.path.exists('token.pickle'):
print("❌ Arquivo token.pickle não encontrado")
return False
try:
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
print(f"✅ Token carregado: {type(creds).__name__}")
print(f" Válido: {creds.valid}")
print(f" Expirado: {creds.expired}")
print(f" Tem refresh token: {hasattr(creds, 'refresh_token') and creds.refresh_token is not None}")
if hasattr(creds, 'scopes'):
print(f" Escopos: {creds.scopes}")
return True
except Exception as e:
print(f"❌ Erro ao carregar token: {e}")
return False
def test_authentication():
"""Testa o processo de autenticação"""
print("\n🔍 Testando autenticação...")
creds = None
try:
# Tenta carregar token existente
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
print("✅ Token carregado do arquivo")
# Verifica se precisa renovar
if creds and creds.expired and creds.refresh_token:
print("🔄 Renovando token expirado...")
creds.refresh(Request())
print("✅ Token renovado com sucesso")
# Salva o token renovado
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
print("✅ Token renovado salvo")
# Se não há credenciais válidas, tenta autenticar
if not creds or not creds.valid:
if not os.path.exists('credentials.json'):
print("❌ Arquivo credentials.json não encontrado!")
return None
print("🔄 Iniciando fluxo de autenticação OAuth...")
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
print("✅ Autenticação OAuth concluída")
# Salva as credenciais
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
print("✅ Credenciais salvas")
return creds
except Exception as e:
print(f"❌ Erro durante autenticação: {e}")
return None
def test_service_creation(creds):
"""Testa a criação do serviço Gmail"""
print("\n🔍 Testando criação do serviço Gmail...")
try:
service = build('gmail', 'v1', credentials=creds)
print("✅ Serviço Gmail criado com sucesso")
return service
except Exception as e:
print(f"❌ Erro ao criar serviço: {e}")
return None
def test_basic_api_calls(service):
"""Testa chamadas básicas da API"""
print("\n🔍 Testando chamadas básicas da API...")
tests = [
("getProfile", lambda: service.users().getProfile(userId='me').execute()),
("listMessages (sem filtro)", lambda: service.users().messages().list(userId='me', maxResults=1).execute()),
("listLabels", lambda: service.users().labels().list(userId='me').execute()),
]
for test_name, test_func in tests:
try:
print(f"🔍 Testando: {test_name}")
result = test_func()
print(f"✅ {test_name}: OK")
# Mostra algumas informações úteis
if test_name == "getProfile":
print(f" Email: {result.get('emailAddress', 'N/A')}")
print(f" Total de mensagens: {result.get('messagesTotal', 'N/A')}")
print(f" Threads não lidos: {result.get('threadsUnread', 'N/A')}")
elif test_name == "listMessages (sem filtro)":
messages = result.get('messages', [])
print(f" Mensagens encontradas: {len(messages)}")
if messages:
print(f" Primeira mensagem ID: {messages[0]['id']}")
elif test_name == "listLabels":
labels = result.get('labels', [])
print(f" Labels encontrados: {len(labels)}")
except HttpError as error:
print(f"❌ {test_name}: {error}")
print(f" Status: {error.resp.status}")
print(f" Detalhes: {error.content}")
except Exception as e:
print(f"❌ {test_name}: {e}")
def test_specific_permissions():
"""Testa permissões específicas"""
print("\n🔍 Testando permissões específicas...")
try:
service = build('gmail', 'v1', credentials=creds)
# Testa diferentes operações
operations = [
("Ler perfil", lambda: service.users().getProfile(userId='me').execute()),
("Listar mensagens", lambda: service.users().messages().list(userId='me', maxResults=1).execute()),
("Ler mensagem", lambda: service.users().messages().get(userId='me', id='test').execute()),
]
for op_name, op_func in operations:
try:
op_func()
print(f"✅ {op_name}: Permissão concedida")
except HttpError as error:
if error.resp.status == 403:
print(f"❌ {op_name}: Permissão negada (403)")
elif error.resp.status == 404:
print(f"⚠️ {op_name}: Recurso não encontrado (404) - mas permissão OK")
else:
print(f"❌ {op_name}: Erro {error.resp.status}")
except Exception as e:
print(f"❌ Erro ao testar permissões: {e}")
def main():
"""Função principal"""
print("🔧 DEBUG DETALHADO - Gmail API")
print("=" * 50)
# Verifica arquivos
check_files()
# Verifica credenciais
if not check_credentials_file():
print("\n❌ Problema com arquivo de credenciais!")
print("💡 Solução: Baixe um novo arquivo credentials.json do Google Cloud Console")
return
# Verifica token
check_token_file()
# Testa autenticação
creds = test_authentication()
if not creds:
print("\n❌ Falha na autenticação!")
print("💡 Soluções:")
print(" 1. Delete o arquivo token.pickle e tente novamente")
print(" 2. Verifique se o arquivo credentials.json está correto")
print(" 3. Confirme se a Gmail API está ativada")
return
# Testa criação do serviço
service = test_service_creation(creds)
if not service:
print("\n❌ Falha ao criar serviço!")
return
# Testa chamadas básicas
test_basic_api_calls(service)
# Testa permissões
test_specific_permissions()
print("\n" + "=" * 50)
print("📊 RESUMO DO DEBUG")
print("=" * 50)
print("✅ Se chegou até aqui, a autenticação está funcionando")
print("💡 Se ainda há problemas, verifique:")
print(" 1. Se há mensagens na sua caixa de entrada")
print(" 2. Se a Gmail API está ativada no Google Cloud Console")
print(" 3. Se as credenciais têm as permissões corretas")
if __name__ == '__main__':
main()