Getting uri mismatch error in chrome extension chrome.identity.launchWebAuthFlow?

47 views Asked by At

I am using chrome.identity.launchWebAuthFlow() to direct a user through OAuth2 of a google auth API. I've configured the OAuth client ID like the Authorized JavaScript origin URL : https://<appid>.chromiumapp.org, and Authorized redirect URIs: https://<appid>.chromiumapp.org/.

In my background.js I've written

const redirectURL = 'https://<appid>.chromiumapp.org';
const { oauth2 } = chrome.runtime.getManifest();
const clientId = oauth2.client_id;
const authParams = new URLSearchParams({
  client_id: clientId,
  response_type: 'token',
  redirect_uri: redirectURL,
  scope: ['email'].join(' '),
});
const authURL = `https://accounts.google.com/o/oauth2/auth?${authParams.toString()}`;
chrome.identity.launchWebAuthFlow({ url: authURL, interactive: true }).then((responseUrl) => {
  console.warn({ responseUrl, authURL });
  const url = new URL(responseUrl);
  const urlParams = new URLSearchParams(url.hash.slice(1));
  const params = Object.fromEntries(urlParams.entries()); // access_token, expires_in
  fetch(`https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=${params.access_token}`, {
    method: 'GET',
    headers: { 'Content-Type': 'application/json' },
  }).then(response => response.json()).then((data) => {
    alert(JSON.stringify(data));
  });
}).catch((error) => {
  console.warn(error.message, authURL);
});

When I run this it gives the error of uri mismatch enter image description here

I've also used values for redirectURL like const redirectURL = 'http://<appid>.chromiumapp.org'; or const redirectURL = chrome.identity.getRedirectURL() but nothing works getting the same error. I'm still in development mode my extension is not published yet.

0

There are 0 answers