Chrome identity API spotify oauth2 login

Tags: , , ,



I’m trying to implement spotify login in my chrome extension but I get always this error and I’m able to see the auth window only when the browser is loaded for the first time.

Unchecked runtime.lastError: Authorization page could not be loaded.

This is the code I’m using

const client_id = '<client_id>'
const redirectUri = chrome.identity.getRedirectURL('spotify');

console.log(redirectUri);

chrome.runtime.onMessage.addListener( (message, sender, sendResponse) => {
  console.log(sender)
  if( message.action === 'run_auth_flow' ){
    chrome.identity.launchWebAuthFlow({
      "url": `https://accounts.spotify.com/authorize?client_id=${client_id}&redirect_uri=${redirectUri}&response_type=token`, 
      'interactive': true,  
    }, (redirect_url) => { 
      console.log(redirect_url);
    });
  }
  //sendResponse({ status: 'ok' })
  return true;
});

chrome.identity.onSignInChanged.addListener( (account, signedIn) => {
  console.log(account, signedIn)
});

Is there any fix? I just want to access the spotify API to play users playlist in my chrome ext

Answer

The problem with the auth flow was in this line of code:

const redirectUri = chrome.identity.getRedirectURL('spotify');

To make things working I’ve just used getRedirectURL() without passing any path.

const redirectUri = chrome.identity.getRedirectURL();

This will give back a virtual url address like this https://<app-id>.chromiumapp.org/ in the callback with the auth token appended to it.



Source: stackoverflow