notion-api

Notion API, Getting Invalid_Grant when trying to generate authorization token


I created an intergration on Notion.so

I got the interim OAuth code using the following URL Add to Notion

This above URL, after authorizing from Notion UI, gives me the following code XXXXXXX-XXXXXXX NOW using the code from above step to get the authorization code

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"
}

This Response in

{
  "error": "invalid_grant"
}

What am I missing?

Thanks In advance!


Solution

  • Try removing the redirect URL from both calls to get the access code and the authorization token. This returned a success response from the server when I tried this.

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