NotionX / react-notion-x

Fast and accurate React renderer for Notion. TS batteries included. ⚡️
https://react-notion-x-demo.transitivebullsh.it
MIT License
4.68k stars 543 forks source link

Getting HTTPError: Response code 401 (Unauthorized) with notion-client #554

Open muke5hy opened 1 month ago

muke5hy commented 1 month ago

Description

I am constatnly getting following error, token and database ID works with @notionhq/client but it is not working with notion-client What am I doing wrong?

⨯ Internal error: HTTPError: Response code 401 (Unauthorized) at Request.eval (./node_modules/.pnpm/got@11.8.6/node_modules/got/dist/source/as-promise/index.js:118:42) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) digest: "1102196494"

Notion Test Page ID

NOTION_AUTH_TOKEN=secret_thisisasecreat NOTION_DATABASE_ID=8a42e373328c49a9a495ddd05fce7fa3

ywj3493 commented 1 month ago

I got exactly same issue.

in my case,

"dependencies" : { "react-notion-x": "^6.16.0" }

and actual version is 6.16.0.

ywj3493 commented 1 month ago

Description

I am constatnly getting following error, token and database ID works with @notionhq/client but it is not working with notion-client What am I doing wrong?

⨯ Internal error: HTTPError: Response code 401 (Unauthorized) at Request.eval (./node_modules/.pnpm/got@11.8.6/node_modules/got/dist/source/as-promise/index.js:118:42) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) digest: "1102196494"

Notion Test Page ID

NOTION_AUTH_TOKEN=secret_thisisasecreat NOTION_DATABASE_ID=8a42e373328c49a9a495ddd05fce7fa3

I have resolved my issue. In case you encounter a similar situation, please refer to this.

The NotionAPI class has a problem with the authToken option, but if you just need to retrieve some data, you don’t need to authorize.

import { Client } from "@notionhq/client";
import { DatabaseObjectResponse } from "@notionhq/client/build/src/api-endpoints";
import { NotionAPI } from "notion-client";

const notionToken = process.env.NOTION_KEY;
const notionDatabaseId = process.env.NOTION_DATABASE_ID;

const notion = new Client({
  auth: notionToken,
});

const notionApi = new NotionAPI();

export async function getPosts() {
  const response = await notion.databases.query({
    database_id: notionDatabaseId as string,
    filter: {
      property: "상태",
      status: {
        equals: "공개",
      },
    },
  });

  return response.results as DatabaseObjectResponse[];
}

export async function getPage(id: string) {
  const response = await notionApi.getPage(id);

  return response;
}
ywj3493 commented 1 month ago

image

authToken for NotionAPI is not secret key. you need to check token_v2 for chrome developer tool.

muke5hy commented 1 month ago

image

authToken for NotionAPI is not secret key. you need to check token_v2 for chrome developer tool.

Will this token_v2 ever changes, say when user logs out of Notion?

takasay commented 1 month ago

I am hesitant to rely on this cookie token thing personally. I found another way via the Notion official API if you want to retrieve a private page.

https://github.com/NotionX/react-notion-x/tree/master/packages/notion-compat

import { Client } from '@notionhq/client'
import { NotionCompatAPI } from 'notion-compat'

const notion = new NotionCompatAPI(
  new Client({ auth: process.env.NOTION_TOKEN })
)

const recordMap = await notion.getPage(pageId)

Note that there are some compatibility limitations if you use notion-compat.