尝试获取访问令牌时请求错误

时间:2020-10-09 09:58:45

标签: javascript node.js nest-device-access

我正在尝试使用Nest Device API,但是由于抛出的访问令牌而无法获取

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

我已经从GCP下载了我的certificate.json,并使用下面的AUTH_URL打开了一个新标签:

    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`;

从那开始,我有获取身份验证码的回调页面。

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);
}

这是API返回上述错误的地方。我也尝试使用此URL并执行curl -L -X POST <url>,但是得到的结果完全相同。

有什么想法吗?

0 个答案:

没有答案