我正在尝试使用概念API的公共集成,并严格遵循文档。链接:https://developers.notion.com/docs/authorization#authorizing-public-integrations
但是通过下面的index.html,我得到了这样的错误: rediect uri是无效的或者缺失的。
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>HTML</title>
</head>
<body>
<a
href="https://api.notion.com/v1/oauth/authorize?client_id=c7d10044-846c-423d-bc66-d6bf7838cc53&redirect_uri=https%3A%2F%2Fnotion-backend-intervue.herokuapp.com%2Fauth%2Fnotion%2Fcallback&response_type=code"
>Add to Notion</a
>
</body>
</html>为了处理所有进一步的步骤,下面是我的后端配置。
app.get("/auth/notion/callback/:code/:state", (req, res) => {
tempCode = req.params.code;
tempState = req.params.state;
console.log(tempCode);
res.send({
"message": "Yes this is the route you are looking for",
"code": tempCode,
"state": tempState
});
axios.post('/https://api.notion.com/v1/oauth/token', {
grant_type: "authorization_code",
code: tempCode,
redirect_uri: 'https://notion-backend-intervue.herokuapp.com/auth/notion/callback/'
}).then((response) => {
console.log(response)
}).catch((err) => {
console.log(`error-->`, err);
})
});在集成设置中,我提供了这个url作为redirect_uri:https://notion-backend-intervue.herokuapp.com/
因为这个get请求没有被调用(我从Heroku日志中确认了这一点)。我想我在提供重定向URI时遇到了一些格式化问题。请帮帮我。
发布于 2021-07-19 14:02:19
后来我发现这不是与重定向uri的格式有关的问题,根据文档的预期,它是正确的。但是由于一些未知的原因,文档中提供的格式将不起作用,要么是在文档之后更新了API,要么可能是由于一些bug。因此,以下是解决方案:
在锚标签中,将href链接更改为:
href="https://api.notion.com/v1/oauth/authorize?client_id=c7d10044-846c-423d-bc66-d6bf7838cc53&response_type=code"然后,概念服务器将在重定向uri中为您提供链接,这是您在概念集成设置中提供的,在我的示例中是这样的:
https://notion-backend-intervue.herokuapp.com/auth/notion/callback因此,在后端创建一个路由来捕获该代码,并将包含该代码的post请求发送到"https://api.notion.com/v1/oauth/token“,您将获得访问令牌作为响应。
后端路由如下所示:
app.get("/auth/notion/callback", (req, res) => {
let tempAuthCode = req.query.code;
console.log("Code --> ", tempAuthCode);
axios({
method: "post",
url: "https://api.notion.com/v1/oauth/token",
data: {
"grant_type": "authorization_code",
"code": tempAuthCode,
},
headers: {
"Authorization":`Basic ${encodedToken}`,
"Content-Type": "application/json"
}
}).then((response) => {
console.log(response);
}).catch((err) => {
console.log(err);
});
});正如您在上面看到的,您还必须从post请求的数据对象中省略redirect_uri。
https://stackoverflow.com/questions/68429633
复制相似问题