Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).


Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion documentation/setup.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/greydev/notionbackup/NotionClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand All @@ -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)) {
Expand Down Expand Up @@ -111,7 +115,7 @@ public Optional<File> 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",
Expand Down Expand Up @@ -143,6 +147,7 @@ public Optional<File> export() {
private Optional<File> downloadToFile(String url, Path downloadPath) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Cookie", FILE_TOKEN + "=" + notionFileToken)
.GET()
.build();

Expand Down