mirror of
https://github.com/mudabbir-ahmad/Media-Aggregation-and-Sorting-System.git
synced 2026-10-07 19:40:20 +00:00
feat: migrate to Google Photos Picker API and update related documentation due to normal library api being depricated (kept for added media from app)
This commit is contained in:
1 parent
f6c61ab829
commit
5c096fbab4
5 files changed
+363
-201
No files matched your search
+57
-41
@@ -1,27 +1,26 @@
|
||||
// Google Photos API Test
|
||||
// Google Photos Picker API Test
|
||||
//
|
||||
// Usage:
|
||||
// node TEST/testGooglePhotosAPI.js <ACCESS_TOKEN>
|
||||
// GOOGLE_ACCESS_TOKEN=ya29.xxx node TEST/testGooglePhotosAPI.js
|
||||
//
|
||||
// The token must have been granted the scope:
|
||||
// https://www.googleapis.com/auth/photospicker.mediaitems.readonly
|
||||
//
|
||||
// Getting a token from OAuth Playground (https://developers.google.com/oauthplayground):
|
||||
// 1. Click the gear icon → check "Use your own OAuth credentials"
|
||||
// 2. Enter your Client ID and Client Secret from Google Cloud Console
|
||||
// 3. In the left panel select ALL of these scopes:
|
||||
// https://www.googleapis.com/auth/photoslibrary
|
||||
// https://www.googleapis.com/auth/photoslibrary.appendonly
|
||||
// https://www.googleapis.com/auth/userinfo.email
|
||||
// https://www.googleapis.com/auth/userinfo.profile
|
||||
// 3. In "Input your own scopes" enter:
|
||||
// https://www.googleapis.com/auth/photospicker.mediaitems.readonly
|
||||
// 4. Click "Authorize APIs" and sign in with your test Google account
|
||||
// 5. Click "Exchange authorization code for tokens"
|
||||
// 6. Copy the Access token (starts with ya29.) and pass it here
|
||||
//
|
||||
// Note: if you only see 403 PERMISSION_DENIED, the token was issued without
|
||||
// the photoslibrary scope — go back and re-authorise with all scopes above.
|
||||
|
||||
const API_BASE = 'https://photoslibrary.googleapis.com/v1';
|
||||
const PICKER_BASE = 'https://photospicker.googleapis.com/v1';
|
||||
const USERINFO_URL = 'https://www.googleapis.com/oauth2/v2/userinfo';
|
||||
|
||||
function delay(ms) { return new Promise((r) => setTimeout(r, ms)); }
|
||||
|
||||
async function main() {
|
||||
const token = process.argv[2] || process.env.GOOGLE_ACCESS_TOKEN;
|
||||
|
||||
@@ -30,13 +29,13 @@ async function main() {
|
||||
console.error('Usage: node TEST/testGooglePhotosAPI.js <ACCESS_TOKEN>');
|
||||
console.error('Or set: GOOGLE_ACCESS_TOKEN=ya29.xxx node TEST/testGooglePhotosAPI.js');
|
||||
console.error('\nTo obtain a token go to https://developers.google.com/oauthplayground');
|
||||
console.error('and authorise the Photos Library API v1 scope.\n');
|
||||
console.error('and authorise the photospicker.mediaitems.readonly scope.\n');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const headers = { Authorization: `Bearer ${token}` };
|
||||
|
||||
console.log('\n--- Google Photos API Test ---\n');
|
||||
console.log('\n--- Google Photos Picker API Test ---\n');
|
||||
|
||||
// Test 1 — User info
|
||||
console.log('[1] Fetching user info...');
|
||||
@@ -52,48 +51,65 @@ async function main() {
|
||||
console.log(` Failed: ${err.message}\n`);
|
||||
}
|
||||
|
||||
// Test 2 — Search media items (replaces deprecated GET /mediaItems)
|
||||
console.log('[2] Searching media items (first 10)...');
|
||||
// Test 2 — Create Picker session
|
||||
console.log('[2] Creating Picker session...');
|
||||
let sessionId = null;
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/mediaItems:search`, {
|
||||
const res = await fetch(`${PICKER_BASE}/sessions`, {
|
||||
method: 'POST',
|
||||
headers: { ...headers, 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ pageSize: 10 }),
|
||||
body: JSON.stringify({}),
|
||||
});
|
||||
if (!res.ok) {
|
||||
console.log(` Failed: HTTP ${res.status}: ${await res.text()}\n`);
|
||||
} else {
|
||||
const data = await res.json();
|
||||
const items = data.mediaItems || [];
|
||||
console.log(` Found ${items.length} items`);
|
||||
items.slice(0, 5).forEach((item, i) => {
|
||||
console.log(` ${i + 1}. ${item.filename} (${item.mimeType})`);
|
||||
});
|
||||
if (items.length > 5) console.log(` ... and ${items.length - 5} more`);
|
||||
if (data.nextPageToken) console.log(' More pages available');
|
||||
console.log();
|
||||
const session = await res.json();
|
||||
sessionId = session.id;
|
||||
console.log(` Session created: ${sessionId}`);
|
||||
console.log(` mediaItemsSet: ${session.mediaItemsSet}\n`);
|
||||
|
||||
// Wait for session to be ready
|
||||
if (!session.mediaItemsSet) {
|
||||
console.log(' Waiting for session to become ready...');
|
||||
for (let i = 0; i < 10; i++) {
|
||||
await delay(1500);
|
||||
const pollRes = await fetch(`${PICKER_BASE}/sessions/${sessionId}`, { headers });
|
||||
const pollData = await pollRes.json();
|
||||
if (pollData.mediaItemsSet) {
|
||||
console.log(' Session is ready!\n');
|
||||
break;
|
||||
}
|
||||
if (i === 9) console.log(' Session timed out.\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(` Failed: ${err.message}\n`);
|
||||
}
|
||||
|
||||
// Test 3 — List albums
|
||||
console.log('[3] Listing albums...');
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/albums?pageSize=10`, { headers });
|
||||
if (!res.ok) {
|
||||
console.log(` Failed: HTTP ${res.status}: ${await res.text()}\n`);
|
||||
} else {
|
||||
const data = await res.json();
|
||||
const albums = data.albums || [];
|
||||
console.log(` Found ${albums.length} albums`);
|
||||
albums.slice(0, 5).forEach((album, i) => {
|
||||
console.log(` ${i + 1}. "${album.title}" - ${album.mediaItemsCount || 0} items`);
|
||||
});
|
||||
console.log();
|
||||
// Test 3 — List media items from session
|
||||
if (sessionId) {
|
||||
console.log('[3] Fetching media items from session...');
|
||||
try {
|
||||
const params = new URLSearchParams({ sessionId, pageSize: '10' });
|
||||
const res = await fetch(`${PICKER_BASE}/mediaItems?${params.toString()}`, { headers });
|
||||
if (!res.ok) {
|
||||
console.log(` Failed: HTTP ${res.status}: ${await res.text()}\n`);
|
||||
} else {
|
||||
const data = await res.json();
|
||||
const items = data.mediaItems || [];
|
||||
console.log(` Found ${items.length} items`);
|
||||
items.slice(0, 5).forEach((item, i) => {
|
||||
const file = item.mediaFile || {};
|
||||
console.log(` ${i + 1}. ${file.filename || '(no name)'} (${file.mimeType || '?'})`);
|
||||
});
|
||||
if (items.length > 5) console.log(` ... and ${items.length - 5} more`);
|
||||
if (data.nextPageToken) console.log(' More pages available');
|
||||
console.log();
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(` Failed: ${err.message}\n`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(` Failed: ${err.message}\n`);
|
||||
}
|
||||
|
||||
console.log('--- Tests complete ---\n');
|
||||
|
||||
Reference in new issue
Block a user