Build OAuth authorization URLs and generate token exchange code for popular providers.
https://accounts.google.com/o/oauth2/v2/auth?client_id=YOUR_CLIENT_ID&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code&scope=&state=q2uja9obg8n
Enter a Client ID to open the authorization URL.
// Install: npm install axios
const axios = require('axios');
async function exchangeCodeForToken(authorizationCode) {
try {
const response = await axios.post('https://oauth2.googleapis.com/token', {
grant_type: 'authorization_code',
code: authorizationCode,
redirect_uri: 'http://localhost:3000/callback',
client_id: 'YOUR_CLIENT_ID',
client_secret: 'YOUR_CLIENT_SECRET',
}, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
});
console.log('Access Token:', response.data.access_token);
console.log('Token Type:', response.data.token_type);
if (response.data.refresh_token) {
console.log('Refresh Token:', response.data.refresh_token);
}
return response.data;
} catch (error) {
console.error('Token exchange failed:', error.response?.data || error.message);
}
}
// After user is redirected back with ?code=AUTH_CODE
exchangeCodeForToken('PASTE_AUTH_CODE_HERE');Replace PASTE_AUTH_CODE_HERE with the code from the callback URL and YOUR_CLIENT_SECRET with your actual secret. Never expose client secrets in client-side code.