7

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!

4 Answers4

13

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)
  }))
});
adlopez15
  • 3,449
  • 2
  • 14
  • 19
  • 1
    My first call works just fine. I do get back the code. Also, just curious, how will u get the code from the first call if you do not provide redirect URL? – Prakhar Sharma Jun 01 '21 at 07:15
  • damn. first call actually worked without a redirect URL, it seems like that it picked up the redirect URL that I provided in my integration. Also, after that, second call worked just fine as well, without the redirect uri – Prakhar Sharma Jun 01 '21 at 07:25
  • The code should come back in the response. This has been reported to Notion to review. – adlopez15 Jun 01 '21 at 20:28
  • Still valid in 2022, thanks you, this is not documented anywhere – Alexy Jul 02 '22 at 12:47
  • Confirmed this is correct, it is still the case today. – Shawn Cao Mar 09 '23 at 17:27
3

This error can also append if you are trying to add an integration to a workspace that already have this integration.

Go to your workspace settings, remove the integration and retry the whole auth process. It should fix the issues.

Mathix420
  • 872
  • 11
  • 21
  • 1
    Thanks for this! I struggled with "invalid_grant" for a long time until I finally tried to remove/disconnect the integration as mentioned and that made it finally work, thank you! – NorahKSakal Sep 30 '22 at 00:22
2

In my case, I needed the redirect_uri in both requests. But check carefully that you have the exact same redirect_uri twice. I was missing the trailing slash

younesbenallal
  • 121
  • 1
  • 5
0

I'm my case, I'm able to generate an authorization code, but when I try to exchange it for an access_token, I always ended up getting invalid_grant.

I'm testing it locally, which means that I added http://localhost:3000 to my list of URLs on the Integration Page. However, this doesn't means that it should be the problem. I deployed my application to Railway, added the domain to the list of URLs on the Integration Page again, and I'm still getting invalid_grant.

Here is an example of my request to the v1/oauth/token endpoint:

await axios({
  method: "POST",
  url: "https://api.notion.com/v1/oauth/token",
  auth: {
    username: env.NOTION_CLIENT_ID,
    password: env.NOTION_CLIENT_SECRET
  },
  headers: {
    "Content-Type": "application/json"
  },
  data: {
    code,
    grant_type: "authorization_code",
    redirect_uri: env.NOTION_CALLBACK_URL
  }