-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexchange-code.js
More file actions
executable file
·81 lines (68 loc) · 3.26 KB
/
exchange-code.js
File metadata and controls
executable file
·81 lines (68 loc) · 3.26 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
#!/usr/bin/env node
/**
* Exchange authorization code for refresh token
* Usage: node exchange-code.js <authorization_code>
*/
import { google } from 'googleapis';
const CLIENT_ID = '681671342232-rge5qmra7th86l20fkgj142d0ddvtq71.apps.googleusercontent.com';
const CLIENT_SECRET = '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';
const REDIRECT_URI = 'https://bk.lyarinet.com/oauth_callback';
const code = process.argv[2];
if (!code) {
console.error('❌ Please provide the authorization code');
console.error('Usage: node exchange-code.js <authorization_code>');
process.exit(1);
}
async function main() {
try {
console.log('\n⏳ Exchanging code for tokens...\n');
const oauth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URI
);
const { tokens } = await oauth2Client.getToken(code.trim());
if (!tokens.refresh_token) {
console.error('\n❌ ERROR: No refresh token received!');
console.error('\nThis usually happens if:');
console.error(' 1. You already authorized this app before');
console.error(' 2. Google didn\'t return a refresh token');
console.error('\nSolution:');
console.error(' 1. Go to: https://myaccount.google.com/permissions');
console.error(' 2. Find and REVOKE access for this app');
console.error(' 3. Run the script again with a new code');
console.error('\nAccess Token:', tokens.access_token ? '✅ Received' : '❌ Missing');
process.exit(1);
}
console.log('✅ SUCCESS! Refresh token obtained!\n');
console.log('='.repeat(50));
console.log('\n📋 COPY THIS REFRESH TOKEN:\n');
console.log(tokens.refresh_token);
console.log('\n' + '='.repeat(50));
console.log('\n📝 Next steps:');
console.log(' 1. Copy the refresh token above');
console.log(' 2. Go to your backup app: https://bk.lyarinet.com/#/settings');
console.log(' 3. Open Cloud Storage → Edit Configuration');
console.log(' 4. Paste the refresh token in the "Refresh Token" field');
console.log(' 5. Click "Update Provider"');
console.log(' 6. Click "Test Connection" to verify\n');
} catch (error) {
console.error('\n❌ ERROR:', error.message);
if (error.message.includes('redirect_uri_mismatch')) {
console.error('\n⚠️ Redirect URI mismatch!');
console.error('Make sure this EXACT URL is in Google Cloud Console:');
console.error(` ${REDIRECT_URI}`);
console.error('\nGo to: https://console.cloud.google.com/apis/credentials');
console.error('Click your OAuth 2.0 Client ID');
console.error('Add the redirect URI in "Authorized redirect URIs"');
} else if (error.message.includes('invalid_grant')) {
console.error('\n⚠️ Invalid grant! The authorization code may have expired.');
console.error('Authorization codes expire quickly. Please run get-refresh-token.js again to get a new code.');
}
process.exit(1);
}
}
main().catch(error => {
console.error('\n❌ Fatal error:', error);
process.exit(1);
});