- Implemented a bash script to test n8n API and retrieve credential schemas. - Added types for API responses, Google Calendar, and WhatsApp instances. - Configured Vitest for testing with React and added setup for testing-library.
3.0 KiB
3.0 KiB
Fluxo Google OAuth Simplificado
Resumo
O dashboard agora gera a oauth_url do Google diretamente, sem precisar de webhook n8n.
Fluxo
1. Usuário clica "Conectar Google Calendar"
↓
2. Dashboard gera oauth_url:
https://accounts.google.com/o/oauth2/v2/auth?
client_id=XXX
redirect_uri=http://localhost:3000/api/google-calendar/callback
state=user_id
scope=https://www.googleapis.com/auth/calendar
access_type=offline
prompt=consent
↓
3. Usuário autoriza no Google
↓
4. Google → http://localhost:3000/api/google-calendar/callback?code=...&state=...
↓
5. Dashboard:
- Salva status "connecting" no Supabase (portal.integrations)
- Redireciona para n8n: https://n8n.automatizase.com.br/rest/oauth2-credential/callback?code=...
↓
6. n8n:
- Troca code por access_token + refresh_token
- Salva na credencial OAuth2
- Redireciona para /dashboard?oauth_success=true
↓
7. Dashboard mostra toast "Conectado com sucesso!"
Configuração Necessária
1. Google Cloud Console
Adicionar redirect URIs:
http://localhost:3000/api/google-calendar/callback
https://n8n.automatizase.com.br/rest/oauth2-credential/callback
2. .env.local
NEXT_PUBLIC_GOOGLE_CLIENT_ID=your-client-id.apps.googleusercontent.com
NEXT_PUBLIC_N8N_CREDENTIAL_ID=your-n8n-credential-id
N8N_BASE_URL=https://n8n.automatizase.com.br
NEXT_PUBLIC_SITE_URL=http://localhost:3000
IMPORTANTE:
- Use o mesmo Client ID configurado no n8n
- Use o ID da credencial OAuth2 do n8n (ex: ccTThTS9TKsejR7W)
3. n8n Credential
Configurar uma credencial OAuth2 do Google Calendar com:
- Client ID (mesmo do .env.local)
- Client Secret
- Scope:
https://www.googleapis.com/auth/calendar
Como encontrar as credenciais do n8n?
1. Client ID (Google OAuth)
- Acesse n8n → Credentials
- Abra a credencial Google Calendar OAuth2
- Copie o Client ID
- Use no
.env.localcomoNEXT_PUBLIC_GOOGLE_CLIENT_ID
2. Credential ID (n8n)
Opção 1 - Via URL:
- Acesse n8n → Credentials
- Clique na credencial Google Calendar OAuth2
- Copie o ID da URL:
https://n8n.automatizase.com.br/credentials/[ID_AQUI] - Use no
.env.localcomoNEXT_PUBLIC_N8N_CREDENTIAL_ID
Opção 2 - Via API:
# Listar todas as credenciais
curl https://n8n.automatizase.com.br/api/v1/credentials \
-H "X-N8N-API-KEY: your-api-key"
# Procurar pelo type: "googleCalendarOAuth2Api"
# Copiar o "id" da credencial
Exemplo de Credential ID: ccTThTS9TKsejR7W (formato alfanumérico do n8n)
Por que não precisa de webhook?
Antes: Dashboard → Webhook n8n → Retorna oauth_url → Redireciona
Agora: Dashboard gera oauth_url diretamente → Redireciona
Vantagem: Menos overhead, mais rápido, código mais simples.
O que o n8n faz?
O n8n só entra no passo 6 do fluxo:
- Recebe o callback com
code - Troca por tokens no Google
- Salva na credencial
- Gerencia refresh automático
Não precisa de workflow para gerar oauth_url.