Bad Request when trying to get an access token

298 views Asked by At

I'm trying to use the Nest Device API, however I am unable to get an Access Token as it throws

{
  "error": "invalid_grant",
  "error_description": "Bad Request"
}

I've downloaded my credentials.json from GCP and I open a new tab with the AUTH_URL below:

    const credentials = require('../../../credentials.json');
    
    const PROJECT_ID = <NEST DEVICE API PROJECT ID>;
    const REDIRECT_URL = 'http://localhost:3000/connect/callback';
    const AUTH_URL =
      `https://nestservices.google.com/partnerconnections/${PROJECT_ID}/auth?` +
      `redirect_uri=${REDIRECT_URL}&access_type=offline&prompt=consent&client_id=${credentials.web.client_id}&` +
      `response_type=code&scope=https://www.googleapis.com/auth/sdm.service`;

From that, I have my callback page that gets the authcode.

const credentials = require('../../../credentials.json');
const { code } = router.query; // Auth Code

try {
  const url =
    `https://www.googleapis.com/oauth2/v4/token?client_id=${credentials.web.client_id}` +
    `&client_secret=${credentials.web.client_secret}&code=${code}&grant_type=authorization_code&` +
    `redirect_uri=${REDIRECT_URL}`;

    const response = await fetch(url, {
      method: 'POST', // *GET, POST, PUT, DELETE, etc.
    });
    console.log(response);
  } catch (e) {
 console.error(e);
}

This is where the API returns the above error. I have also tried taking this URL and doing curl -L -X POST <url> however I get exactly the same results.

Any ideas?

0

There are 0 answers