I am trying to use public integration with the Notion API and was strictly following along the documention. Link: https://developers.notion.com/docs/authorization#authorizing-public-integrations
but with the following index.html I am getting the error, rediect uri is invalid or missing.
<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>
For handling all the further steps, here is my backend configuration.
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);
})
});
In the Integration setting, I have provided this url as the redirect_uri: https://notion-backend-intervue.herokuapp.com/
As this get request is not invoked(I confirmed this from the Heroku log). I guess I have done some formatting issues in providing the redirect URI. Please help me.
Later I found that it is not the issue related to the formatting of the redirect uri, it is correct, based upon what is expecting from the documentation. But the format provided in the documentation will not work for some unknown reasons, either the API is updated after the documentation or maybe due to some bugs. So, here is the solution:
In the anchor tag, change the href link to this:
href="https://api.notion.com/v1/oauth/authorize?client_id=c7d10044-846c-423d-bc66-d6bf7838cc53&response_type=code"
and after that, the notion server will provide you the link in the redirect uri, that you have provided in the notion-integration-setting, which in my case was like this:
https://notion-backend-intervue.herokuapp.com/auth/notion/callback
so, make a route in the backend to catch that code and send a post-request with the code to "https://api.notion.com/v1/oauth/token" and you will get the access token as the response.
By backend route looks like this:
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);
});
});
As you saw above, you also have to omit the redirect_uri from the data object of my the post request.