[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
muke5hy opened this issue May 21, 2024 · 5 comments
Open

Comments

@muke5hy
Copy link
muke5hy commented May 21, 2024

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
Copy link
ywj3493 commented May 21, 2024

I got exactly same issue.

in my case,

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

and actual version is 6.16.0.

@ywj3493
Copy link
ywj3493 commented May 22, 2024

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
Copy link
ywj3493 commented May 24, 2024

image

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

@muke5hy
Copy link
Author
muke5hy commented May 26, 2024

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
Copy link
takasay commented Jun 5, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants