76 lines
2.8 KiB
JavaScript
76 lines
2.8 KiB
JavaScript
const { BrowserWindow } = require('@electron/remote');
|
|
const { v4: uuidv4 } = require('uuid'); // Add this for GUID generation
|
|
const AuthProvider = require("./AuthProvider");
|
|
|
|
const { protectedResources, msalConfig } = require("./authConfig");
|
|
|
|
let authProvider;
|
|
class GraphApiClient {
|
|
constructor() {
|
|
this.baseUrl = 'https://graph.microsoft.com/v1.0';
|
|
this.clientId = "073204aa-c1e0-4e66-a200-e5815a0aa93d";
|
|
this.scopes = "OneDrive.ReadWrite offline_access openid profile";
|
|
this.redirectUrl = "https://photos.onedrive.com/auth/login";
|
|
|
|
}
|
|
|
|
cleanPath(path) {
|
|
return path
|
|
.replace(/^\/+|\/+$/g, '') // Remove leading/trailing slashes
|
|
.split('/')
|
|
.map(segment => encodeURIComponent(segment))
|
|
.join('/');
|
|
}
|
|
|
|
async listFolderContents(folderPath) {
|
|
try {
|
|
console.log('graphApiClient listFolderContents:', folderPath);
|
|
const tokenRequest = {
|
|
scopes: protectedResources.graphMe.scopes
|
|
};
|
|
console.log('graphApiClient tokenRequest:', tokenRequest);
|
|
if (!authProvider) {
|
|
authProvider = new AuthProvider(msalConfig);
|
|
console.log('graphApiClient AuthProvider created', msalConfig);
|
|
}
|
|
|
|
await authProvider.logout();
|
|
const tokenResponse = await authProvider.getToken(tokenRequest);
|
|
const account = authProvider.account;
|
|
const accessToken = tokenResponse.accessToken;
|
|
console.log('graphApiClient Auth received: ', accessToken);
|
|
console.log('graphApiClient Auth received: ', account);
|
|
|
|
const cleanPath = this.cleanPath(folderPath);
|
|
console.log('graphApiClient Clean path:', cleanPath);
|
|
const url = `${this.baseUrl}/me/drive/root:/${cleanPath}:/children`;
|
|
|
|
console.log('Querying OneDrive items from:', url);
|
|
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
'Accept': 'application/json',
|
|
'Cookie': auth.cookies,
|
|
'Origin': 'https://onedrive.live.com',
|
|
'Referer': 'https://onedrive.live.com/'
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
console.error('Graph API error details:', errorData);
|
|
throw new Error(`Graph API error: ${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
return data.value;
|
|
|
|
} catch (error) {
|
|
console.error('Graph API error:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = new GraphApiClient();
|