0

I have a web application with React and Node.js and I am trying to connect to the Google Search Console API.

It requires authorisation so on the front end I am connecting to the correct account and scope and I am getting the OAuth2 authorizationCode it look something like this:

4/0AX4XfWg2MKbTfn41rBjuv1wn4u5VF4G1m5p8q6-_Px-FL991ccJsn-qn6xdUbJqfM73TEw

I am then sending it to the back end but the OAuth2 returns 400

Here is what I got:

const { google } = require('googleapis');

export default async () => {

  const oauth2Client = new google.auth.OAuth2(
    YOUR_CLIENT_ID,
    YOUR_CLIENT_SECRET,
    YOUR_REDIRECT_URL
  );

  google.options({
    auth: oauth2Client
  });

  // It throws 400 on this line
  const { tokens } = await oauth2Client.getToken(authorizationCode);
  oauth2Client.setCredentials(tokens);

  const { data } = await google.webmasters.sites.list({});
  console.log(data);
}

But I get 400 Bad Request

Álvaro
  • 2,255
  • 1
  • 22
  • 48

1 Answers1

0

I found the solution, there was two problems the first one was really silly and I forgot to add the object properly to the new google.auth.OAuth2 instead of

const oauth2Client = new google.auth.OAuth2({
  clientId: CLIENT_ID,
  clientSecret: CLIENT_SECRET,
  redirectUri: REDIRECT_URI
})

I had written

const oauth2Client = new google.auth.OAuth2(
  CLIENT_ID,
  CLIENT_SECRET,
  REDIRECT_URL
);

And the second thing was this answer from another person with similar problems here

Where I had to change the redirectUri from https://localhost:3001 to postmessage on both the back and fron end... go figure, no where to be found on the googleapis docs

Álvaro
  • 2,255
  • 1
  • 22
  • 48