This video is temporarily saved in My Local Recordings( a temp folder in your browser) and prone to be lost due to an unexpected system or browser error. Upload this video to awesomescreenshot.com or download it to local to avoid any possible data loss.
Save to Awesome Screenshot
Upload for features :
Shareable link
Download in MP4
Quick video editing
Back up
DownloadSize: 1.42 MB
In WebM format
Save to cloud
YouTube
Google Drive
Recording length is limited to 5 minutes. Upgrade to unlock the unlimited recordings.
// Define the scopes you need. // For example, read-only access to YouTube or Google Drive metadata. const SCOPES = [ 'https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/drive.readonly' ];
// Path where the generated access/refresh tokens will be saved const TOKEN_PATH = path.join(__process.cwd(), 'token.json'); const CREDENTIALS_PATH = path.join(__process.cwd(), 'credentials.json');
/** * Reads previously authorized credentials from the local file. */ async function loadSavedCredentialsIfExist() { try { const content = await fs.readFile(TOKEN_PATH); const credentials = JSON.parse(content); return google.auth.fromJSON(credentials); } catch (err) { return null; } }
/** * Main execution function */ async function run() { // 1. Try to load existing tokens to avoid re-authenticating every time let auth = await loadSavedCredentialsIfExist();
if (!auth) { console.log('No saved tokens found. Launching browser for authentication...'); // 2. This spins up a temporary local server, opens your browser, // and automatically captures the authorization code response. auth = await authenticate({ scopes: SCOPES, keyfilePath: CREDENTIALS_PATH, });
// 3. Save the tokens for future executions if (auth.credentials) { await saveCredentials(auth); console.log('Tokens successfully saved to:', TOKEN_PATH); } } else { console.log('Loaded existing credentials from environment.'); }
// --- Example Usage --- // Now that 'auth' is authenticated, you can use it to call Google APIs. console.log('\n--- OAuth Exchange Complete ---'); console.log('Access Token:', auth.credentials.access_token); console.log('Refresh Token:', auth.credentials.refresh_token);
If you must use a pure bash curl approach and already have the authorization code manually, you can use this clean script wrapper. Bash
#!/usr/bin/env bash
# Exit immediately if a command exits with a non-zero status set -e
CLIENT_ID="YOUR_CLIENT_ID" CLIENT_SECRET="YOUR_CLIENT_SECRET" AUTHORIZATION_CODE="F8t5QuvjavJgAMEzK6MVg" REDIRECT_URI="urn:ietf:wg:oauth:2.0:oob" # Note: Ensure your GCP console client matches this redirect URI
echo "Exchanging authorization code for tokens..."
https://docs.google.com/document/d/e/2PACX-1vRZjKPFWhbxDrzjPMkXFP8vxL0jzsTmpwVNYQeswSyHclNMSof3AaPB7oofNoR0ZRZfinjE2uFoW1an/pub?urp=gmail_link https://docs.google.com/document/d/e/2PACX-1vRZjKPFWhbxDrzjPMkXFP8vxL0jzsTmpwVNYQeswSyHclNMSof3AaPB7oofNoR0ZRZfinjE2uFoW1an/pub?urp=gmail_ [ [https://skippy-one-timer.blogspot.com] New comment on https://docs.google.com/document/d/e/2PACX-1vRZjKPFWhbxDrzjPMkXFP8vxL0jzsTmpwVNYQeswSyHclNMSof3AaPB7oofNoR0ZRZfinjE2uFoW1an/pub?urp=gmail_link. { "debug_videoId": "1L8CFo-vyWf5GoCYxtxtWOS7krrniedhv", "debug_playbackQuality": "unknown", "debug_date": "Tue Apr 21 2026 02:47:40 GMT-0400 (Eastern Daylight Time)", "origin": "https://youtube.googleapis.com", "timestamp": 1776754060515 } Video Inbox https://mastodon.social/@Skippy-One-Time Unsubscribe Sun, Apr 12, 2:08 PM (9 days ago) to me https://mastodon.social/@Skippy-One-Time has left a new comment on th...
const fs = require('fs').promises;
ReplyDeleteconst path = require('path');
const { authenticate } = require('@google-cloud/local-auth');
const { google } = require('googleapis');
// Define the scopes you need.
// For example, read-only access to YouTube or Google Drive metadata.
const SCOPES = [
'https://www.googleapis.com/auth/youtube.readonly',
'https://www.googleapis.com/auth/drive.readonly'
];
// Path where the generated access/refresh tokens will be saved
const TOKEN_PATH = path.join(__process.cwd(), 'token.json');
const CREDENTIALS_PATH = path.join(__process.cwd(), 'credentials.json');
/**
* Reads previously authorized credentials from the local file.
*/
async function loadSavedCredentialsIfExist() {
try {
const content = await fs.readFile(TOKEN_PATH);
const credentials = JSON.parse(content);
return google.auth.fromJSON(credentials);
} catch (err) {
return null;
}
}
/**
* Serializes credentials to a file compatible with GoogleAUth.fromJSON.
*/
async function saveCredentials(client) {
const content = await fs.readFile(CREDENTIALS_PATH);
const keys = JSON.parse(content);
const key = keys.installed || keys.web;
const payload = JSON.stringify({
type: 'authorized_user',
client_id: key.client_id,
client_secret: key.client_secret,
refresh_token: client.credentials.refresh_token,
});
await fs.writeFile(TOKEN_PATH, payload);
}
/**
* Main execution function
*/
async function run() {
// 1. Try to load existing tokens to avoid re-authenticating every time
let auth = await loadSavedCredentialsIfExist();
if (!auth) {
console.log('No saved tokens found. Launching browser for authentication...');
// 2. This spins up a temporary local server, opens your browser,
// and automatically captures the authorization code response.
auth = await authenticate({
scopes: SCOPES,
keyfilePath: CREDENTIALS_PATH,
});
// 3. Save the tokens for future executions
if (auth.credentials) {
await saveCredentials(auth);
console.log('Tokens successfully saved to:', TOKEN_PATH);
}
} else {
console.log('Loaded existing credentials from environment.');
}
// --- Example Usage ---
// Now that 'auth' is authenticated, you can use it to call Google APIs.
console.log('\n--- OAuth Exchange Complete ---');
console.log('Access Token:', auth.credentials.access_token);
console.log('Refresh Token:', auth.credentials.refresh_token);
// Example: Initializing the Drive client
// const drive = google.drive({ version: 'v3', auth });
}
run().catch(console.error);
Alternative: Bash Curl Script (Legacy / Manual Code Entry)
If you must use a pure bash curl approach and already have the authorization code manually, you can use this clean script wrapper.
Bash
#!/usr/bin/env bash
# Exit immediately if a command exits with a non-zero status
set -e
CLIENT_ID="YOUR_CLIENT_ID"
CLIENT_SECRET="YOUR_CLIENT_SECRET"
AUTHORIZATION_CODE="F8t5QuvjavJgAMEzK6MVg"
REDIRECT_URI="urn:ietf:wg:oauth:2.0:oob" # Note: Ensure your GCP console client matches this redirect URI
echo "Exchanging authorization code for tokens..."
RESPONSE=$(curl -s -X POST \
-d "client_id=${CLIENT_ID}" \
-d "client_secret=${CLIENT_SECRET}" \
-d "redirect_uri=${REDIRECT_URI}" \
-d "grant_type=authorization_code" \
-d "code=${AUTHORIZATION_CODE}" \
https://oauth2.googleapis.com/token)
# Pretty print the JSON payload result
echo "Response payload received:"
echo "$RESPONSE" | json_pp 2>/dev/null |