How to make OpenId Connect Client Credentials API call

Hi, I’m new to building any sort of plugin for Obsidian but I have a working MVP in plain Typescript and NodeJS however, when moving my code over to the plugin, I can’t seem to get the API call to work.
Is there something specific I need to do or maybe Obsidian itself needs some tweaks?

async callExternalApi(endpoint: string, method = 'GET', body?: any): Promise<any> {
		const apiBaseUrl = 'https://localhost:44356';
		const token = await this.getBearerToken();
	
		const response = await fetch(`${apiBaseUrl}${endpoint}`, {
			method,
			headers: {
				'Authorization': `Bearer ${token}`,
				'Content-Type': 'application/json',
			},
			body: body ? JSON.stringify(body) : undefined,
		});
	
		if (!response.ok) {
			const errorData = await response.json();
			console.error('Error calling External API:', errorData);
			throw new Error(`Failed to call Umbraco API: ${response.statusText}`);
		}
	
		return response.json();
	}
async getBearerToken(): Promise<string> {
		const clientId = 'user-back-office-obsidian';
		const clientSecret = 'my-client-secret';
		const tokenEndpoint =  'https://localhost:44356/api/v1/security/back-office/token';
	
		if (!clientId || !clientSecret || !tokenEndpoint) {
			throw new Error('Missing CLIENT_ID, CLIENT_SECRET, or TOKEN_ENDPOINT in environment variables.');
		}
	
		const body = new URLSearchParams({
			grant_type: 'client_credentials',
			client_id: clientId,
			client_secret: clientSecret,
		});
	
		const response = await fetch(tokenEndpoint, {
			method: 'POST',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded',
			},
			body: body.toString(),
		});
	
		if (!response.ok) {
			const errorData = await response.json();
			console.error('Error fetching bearer token:', errorData);
			throw new Error(`Failed to fetch bearer token: ${response.statusText}`);
		}
	
		const data = (await response.json()) as { access_token: string };
		return data.access_token; // Assuming the token is in the "access_token" field
	}

This is how I have been calling my external api endpoint without Obsidian.
I’d apprecatiate any help, I’m pretty new to all this as I come from a .net c# background.

Thank.

fetch doesn’t really work well in Obsidian because of CORS.

I would try:

Thanks, I’ll take a look. I also found the issue was to do with the version of node-fetch I was using. V3, which is the latest, works different to v2, which is what I was using.