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

时间:2021-06-01 03:54:42

标签: notion-api

我在 Notion.so 上创建了一个集成

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

上面的这个 URL,在从 Notion 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"
}

我错过了什么?

提前致谢!

3 个答案:

答案 0 :(得分:2)

尝试从两个调用中删除重定向 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)
  }))
});

答案 1 :(得分:1)

就我而言,我在两个请求中都需要 redirect_uri。但请仔细检查您是否有两次完全相同的 redirect_uri。我错过了最后一个斜线

答案 2 :(得分:0)

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

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