fixed re-login with a refresh token (I did not yet reverse-engineer that request yet)

This commit is contained in:
Lena Brueder 2023-02-08 21:11:53 +01:00
parent c90309227d
commit 6295cfe67f
3 changed files with 36 additions and 29 deletions

View file

@ -161,34 +161,39 @@ export class PocketbookCloudLoginClient {
.then(data => data.providers.filter((item: any) => item.name.includes(this.shop_name)))
.then(data => data[0].shop_id);
let login_body: URLSearchParams;
console.log(`shop_id: ${shop_id}`);
let login_response;
if (this.refresh_token) {
login_body = new URLSearchParams({
shop_id,
username: this.username,
refresh_token: this.refresh_token,
client_id: this.client_id,
client_secret: this.client_secret,
});
login_response = await requestUrl({
url: 'https://cloud.pocketbook.digital/api/v1.0/auth/renew-token',
method: 'POST',
contentType: 'application/x-www-form-urlencoded',
headers: {
Authorization: `Bearer ${this.access_token}`,
},
body: new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: this.refresh_token,
}).toString(),
}).then(response => response.json);
} else if (this.password) {
login_body = new URLSearchParams({
shop_id,
username: this.username,
password: this.password,
client_id: this.client_id,
client_secret: this.client_secret,
});
login_response = await requestUrl({
url: 'https://cloud.pocketbook.digital/api/v1.0/auth/login/knv',
method: 'POST',
contentType: 'application/x-www-form-urlencoded',
body: new URLSearchParams({
shop_id,
username: this.username,
password: this.password,
client_id: this.client_id,
client_secret: this.client_secret,
}).toString(),
}).then(response => response.json);
} else {
throw new Error('No password or refresh token provided, one is necessary to login.');
}
const login_response = await requestUrl({
url: 'https://cloud.pocketbook.digital/api/v1.0/auth/login/knv',
method: 'POST',
contentType: 'application/x-www-form-urlencoded',
body: login_body.toString(),
}).then(response => response.json);
this.access_token = login_response.access_token;
this.refresh_token = login_response.refresh_token;
@ -202,7 +207,7 @@ export class PocketbookCloudLoginClient {
}
async getAccessToken(): Promise<string> {
// TODO: check if access token is still valid, use refresh token to get a new one, ...
// check if access token is still valid, use refresh token to get a new one, ...
if (!this.access_token || this.access_token_valid_until < new Date()) {
await this.login();
}

View file

@ -15,7 +15,7 @@ export default class PocketbookCloudHighlightsImporterPlugin extends Plugin {
// This adds a simple command that can be triggered anywhere
this.addCommand({
id: 'pocketbook-cloud-importer-perform-import',
name: 'Pocketbook Cloud: Import highlights & notes',
name: 'Import highlights & notes',
callback: () => {
this.importer.importHighlights();
},
@ -29,6 +29,8 @@ export default class PocketbookCloudHighlightsImporterPlugin extends Plugin {
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
// this gets serialized as a string under the hood - this is to restore it to a Date object. Probably not the best way to do this, happy to learn how to do this better
this.settings.access_token_valid_until = new Date(this.settings.access_token_valid_until);
}
async saveSettings() {

View file

@ -13,7 +13,7 @@
"state": {
"type": "markdown",
"state": {
"file": "Pocketbook Cloud Highlights API.md",
"file": "Highlights/Befriend Your Brain/metadata.md",
"mode": "source",
"source": false
}
@ -85,7 +85,7 @@
"state": {
"type": "backlink",
"state": {
"file": "Pocketbook Cloud Highlights API.md",
"file": "Highlights/Befriend Your Brain/metadata.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
@ -102,7 +102,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "Pocketbook Cloud Highlights API.md",
"file": "Highlights/Befriend Your Brain/metadata.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
@ -125,7 +125,7 @@
"state": {
"type": "outline",
"state": {
"file": "Pocketbook Cloud Highlights API.md"
"file": "Highlights/Befriend Your Brain/metadata.md"
}
}
}
@ -148,6 +148,7 @@
},
"active": "cf61624d22560890",
"lastOpenFiles": [
"Pocketbook Cloud Highlights API.md",
"Highlights/Datenintensive Anwendungen designen/metadata.md",
"Highlights/Getting Things Done The Art of Stress-free Productivity/metadata.md",
"Highlights/Getting Things Done The Art of Stress-free Productivity/highlights/BE1FCB97-B5F5-504A-B645-C37E32DAFD16.md",
@ -156,7 +157,6 @@
"Highlights/From Conflict to Community/highlights/45cdb958-6059-48df-a76f-376046d03fe5.md",
"Highlights/From Conflict to Community/highlights/218a7270-d3da-4eeb-ab10-019a80f46f8f.md",
"Highlights/From Conflict to Community/highlights/4213c9e0-2f8d-46e0-8ae0-e2fdb45cd590.md",
"Pocketbook Cloud Highlights API.md",
"Testnote.md"
]
}