diff --git a/README.md b/README.md index 4efd7e0..7f78b1a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ > ⚠️ Notion changed their API around 12.2022 which broke the automatic login requests made by this tool to extract the > `token_v2`. > -> To solve this new limitation, you need to copy the value of the `token_v2` cookie manually (see [How do I find +> To solve this new limitation, you need to copy the value of the `token_v2` and `file_token` cookie manually (see [How do I find > all these values?](./documentation/setup.md) for more info). @@ -20,6 +20,7 @@ Create a `.env` file with the following properties ([How do I find all these val # Notion (Required) NOTION_SPACE_ID= NOTION_TOKEN_V2= + NOTION_FILE_TOKEN= # Options: markdown, html (default is markdown) NOTION_EXPORT_TYPE=markdown # Create folders for nested pages? Options: true, false (default is false) diff --git a/documentation/setup.md b/documentation/setup.md index 3d7e1e9..0231c0c 100644 --- a/documentation/setup.md +++ b/documentation/setup.md @@ -1,4 +1,4 @@ -### Find Your notion-space-id and token_v2 +### Find Your notion-space-id, token_v2 and file_token 1. Login to your [notion profile](https://www.notion.so/login) 2. Open your developer console of your browser and go to the "Network" tab @@ -13,6 +13,10 @@ web-UI. Meaning that you can use it in this tool as long as you don't log out. If you do log out (or if the token expires after one year) then you need to log in again and fetch a new `token_v2` value. +6. Visit [https://www.notion.so/f](https://www.notion.so/f) and in the **network tab** find the request named "f". Open that + request and go to **cookies tab** of that same requests and find the `file_token` cookie. Paste it in your `.env` file as the value for `NOTION_FILE_TOKEN`. This token (expires after one year and) is valid as long as you don't log out over the web-UI. Meaning that you can use it in this tool as long as you don't log out. If you do log out (or if the token + expires after one year) then you need to log in again and fetch a new `file_token` value. (Firefox displays this cookie under "Storage" tab on any notion page, however chrome seems to filter cookies based on the page's path hence we visit "/f") + ### Dropbox 1. Create a new app on developer console (https://www.dropbox.com/developers/apps/create) diff --git a/src/main/java/com/greydev/notionbackup/NotionClient.java b/src/main/java/com/greydev/notionbackup/NotionClient.java index 36ceffc..3d4c39e 100644 --- a/src/main/java/com/greydev/notionbackup/NotionClient.java +++ b/src/main/java/com/greydev/notionbackup/NotionClient.java @@ -28,12 +28,14 @@ public class NotionClient { private static final String ENQUEUE_ENDPOINT = "https://www.notion.so/api/v3/enqueueTask"; private static final String NOTIFICATION_ENDPOINT = "https://www.notion.so/api/v3/getNotificationLogV2"; private static final String TOKEN_V2 = "token_v2"; + private static final String FILE_TOKEN = "file_token"; private static final String EXPORT_FILE_NAME = "notion-export"; private static final String EXPORT_FILE_EXTENSION = ".zip"; private static final String KEY_DOWNLOADS_DIRECTORY_PATH = "DOWNLOADS_DIRECTORY_PATH"; private static final String KEY_NOTION_SPACE_ID = "NOTION_SPACE_ID"; private static final String KEY_NOTION_TOKEN_V2 = "NOTION_TOKEN_V2"; + private static final String KEY_NOTION_FILE_TOKEN = "NOTION_FILE_TOKEN"; private static final String KEY_NOTION_EXPORT_TYPE = "NOTION_EXPORT_TYPE"; private static final String KEY_NOTION_FLATTEN_EXPORT_FILETREE = "NOTION_FLATTEN_EXPORT_FILETREE"; private static final String KEY_NOTION_EXPORT_COMMENTS = "NOTION_EXPORT_COMMENTS"; @@ -44,6 +46,7 @@ public class NotionClient { private final String notionSpaceId; private final String notionTokenV2; + private final String notionFileToken; private final String exportType; private final boolean flattenExportFileTree; private final boolean exportComments; @@ -59,6 +62,7 @@ public class NotionClient { // both environment variables and variables defined in the .env file can be accessed this way notionSpaceId = dotenv.get(KEY_NOTION_SPACE_ID); notionTokenV2 = dotenv.get(KEY_NOTION_TOKEN_V2); + notionFileToken = dotenv.get(KEY_NOTION_FILE_TOKEN); downloadsDirectoryPath = dotenv.get(KEY_DOWNLOADS_DIRECTORY_PATH); if (StringUtils.isBlank(downloadsDirectoryPath)) { @@ -111,7 +115,7 @@ public Optional export() { log.info("downloadLink could not be extracted"); return Optional.empty(); } - log.info("Download link extracted"); + log.info("Download link extracted: " + downloadLink.get()); log.info("Downloading file..."); String fileName = String.format("%s-%s%s_%s%s", @@ -143,6 +147,7 @@ public Optional export() { private Optional downloadToFile(String url, Path downloadPath) { HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(url)) + .header("Cookie", FILE_TOKEN + "=" + notionFileToken) .GET() .build();