react-notion-x icon indicating copy to clipboard operation
react-notion-x copied to clipboard

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

Open muke5hy opened this issue 1 year ago • 5 comments
trafficstars

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/[email protected]/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

muke5hy avatar May 21 '24 07:05 muke5hy

I got exactly same issue.

in my case,

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

and actual version is 6.16.0.

ywj3493 avatar May 21 '24 10:05 ywj3493

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/[email protected]/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 avatar May 22 '24 07:05 ywj3493

image

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

ywj3493 avatar May 24 '24 07:05 ywj3493

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?

muke5hy avatar May 26 '24 04:05 muke5hy

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.

takasay avatar Jun 05 '24 04:06 takasay

I recommend using the official Notion API if you need private access with an auth token.

This lib is mainly meant to access public notion data.

transitive-bullshit avatar Nov 01 '24 04:11 transitive-bullshit

@transitive-bullshit If it only query the public page, I think this library doesn't make much sense, beacuse users can load the page by webview directly.

liaodalin19903 avatar Feb 01 '25 09:02 liaodalin19903

@liaodalin19903 this library has been around for a long time, before notion's public pages were good and before they ever had an api. you can still do a lot of customization that you can't do with the official public pages.

transitive-bullshit avatar Feb 01 '25 09:02 transitive-bullshit