概念 API,尝试生成授权令牌时获取 Invalid_Grant

Notion API, Getting Invalid_Grant when trying to generate authorization token

我在 Notion.so

上创建了一个集成

我使用以下方法获得了临时 OAuth 代码 URL Add to Notion

以上URL,在从概念UI授权后,给我以下代码 XXXXXXX-XXXXXXX 现在使用上面步骤中的代码获取授权码

POST https://api.notion.com/v1/oauth/token HTTP/1.1
Authorization: Basic XXXXXXXOnNlY3JldF9DeXp0d1A0TVNLZkZIY0XXXXXXXXX
Content-Type: application/json
Content-Length: 164

{
    "grant_type": "authorization_code",
    "code": "XXXXXX-XXXXX",
    "redirect_uri": "http://localhost:8080/api/notion/auth/callback"
}

此回复在

{
  "error": "invalid_grant"
}

我错过了什么?

提前致谢!

尝试从两个调用中删除重定向 URL 以获取访问代码和授权令牌。当我尝试这个时,这从服务器返回了成功响应。

app.get(auth_path, (req, res) => {
  res.redirect(
    //Documentation expects "${api_url}/v1/oauth/authorize?client_id=${process.env.NOTION_ID}&response_type=code&redirect_uri=${redirect_uri}"
    encodeURI(`${api_url}/v1/oauth/authorize?client_id=${process.env.NOTION_ID}&response_type=code`),
  );
});

`enter code here`app.get(access_token_path, ({query: {code}}, res) => {
  //Exchange authorization code for access token
  axios({
    method:"post",
    url:"https://api.notion.com/v1/oauth/token", 
    data: {
    "code":code,
    "grant_type": "authorization_code",
    
    }, 
    headers: {
    "Authorization": `Basic ${auth_token}`,
    "Content-Type": "application/json"
    }
  }).then((response) => {
    res.sendStatus(200)
  }).catch((err => {
    console.log(err)
    res.sendStatus(500)
  }))
});

如果您尝试将集成添加到已有此集成的工作区,也可能会附加此错误。

转到您的工作区设置,删除集成并重试整个身份验证过程。它应该可以解决问题。

就我而言,我在两个请求中都需要 redirect_uri。但是仔细检查你有两次完全相同的 redirect_uri 。我漏掉了结尾的斜杠