fix minor bug of onedrive
This commit is contained in:
parent
7b67ba5b26
commit
7422956621
@ -674,10 +674,16 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
this.settings.onedrive.clientID,
|
this.settings.onedrive.clientID,
|
||||||
this.settings.onedrive.authority,
|
this.settings.onedrive.authority,
|
||||||
inputParams.code,
|
inputParams.code,
|
||||||
this.oauth2Info.verifier
|
this.oauth2Info.verifier,
|
||||||
|
async (e: any) => {
|
||||||
|
new Notice(t("protocol_onedrive_connect_fail"));
|
||||||
|
new Notice(`${e}`);
|
||||||
|
return; // throw?
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
if ((rsp as any).error !== undefined) {
|
if ((rsp as any).error !== undefined) {
|
||||||
|
new Notice(`${JSON.stringify(rsp)}`);
|
||||||
throw Error(`${JSON.stringify(rsp)}`);
|
throw Error(`${JSON.stringify(rsp)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -105,7 +105,8 @@ export const sendAuthReq = async (
|
|||||||
clientID: string,
|
clientID: string,
|
||||||
authority: string,
|
authority: string,
|
||||||
authCode: string,
|
authCode: string,
|
||||||
verifier: string
|
verifier: string,
|
||||||
|
errorCallBack: any
|
||||||
) => {
|
) => {
|
||||||
// // original code snippets for references
|
// // original code snippets for references
|
||||||
// const authResponse = await pca.acquireTokenByCode({
|
// const authResponse = await pca.acquireTokenByCode({
|
||||||
@ -123,28 +124,33 @@ export const sendAuthReq = async (
|
|||||||
// instead of using msal
|
// instead of using msal
|
||||||
// https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow
|
// https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow
|
||||||
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/graph-oauth?view=odsp-graph-online#code-flow
|
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/graph-oauth?view=odsp-graph-online#code-flow
|
||||||
const rsp1 = await request({
|
try {
|
||||||
url: `${authority}/oauth2/v2.0/token`,
|
const rsp1 = await request({
|
||||||
method: "POST",
|
url: `${authority}/oauth2/v2.0/token`,
|
||||||
contentType: "application/x-www-form-urlencoded",
|
method: "POST",
|
||||||
body: new URLSearchParams({
|
contentType: "application/x-www-form-urlencoded",
|
||||||
tenant: "consumers",
|
body: new URLSearchParams({
|
||||||
client_id: clientID,
|
tenant: "consumers",
|
||||||
scope: SCOPES.join(" "),
|
client_id: clientID,
|
||||||
code: authCode,
|
scope: SCOPES.join(" "),
|
||||||
redirect_uri: REDIRECT_URI,
|
code: authCode,
|
||||||
grant_type: "authorization_code",
|
redirect_uri: REDIRECT_URI,
|
||||||
code_verifier: verifier,
|
grant_type: "authorization_code",
|
||||||
}).toString(),
|
code_verifier: verifier,
|
||||||
});
|
}).toString(),
|
||||||
|
});
|
||||||
|
|
||||||
const rsp2 = JSON.parse(rsp1);
|
const rsp2 = JSON.parse(rsp1);
|
||||||
// log.info(rsp2);
|
// log.info(rsp2);
|
||||||
|
|
||||||
if (rsp2.error !== undefined) {
|
if (rsp2.error !== undefined) {
|
||||||
return rsp2 as AccessCodeResponseFailedType;
|
return rsp2 as AccessCodeResponseFailedType;
|
||||||
} else {
|
} else {
|
||||||
return rsp2 as AccessCodeResponseSuccessfulType;
|
return rsp2 as AccessCodeResponseSuccessfulType;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log.error(e);
|
||||||
|
await errorCallBack(e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -154,26 +160,31 @@ export const sendRefreshTokenReq = async (
|
|||||||
refreshToken: string
|
refreshToken: string
|
||||||
) => {
|
) => {
|
||||||
// also use Obsidian request to bypass CORS issue.
|
// also use Obsidian request to bypass CORS issue.
|
||||||
const rsp1 = await request({
|
try {
|
||||||
url: `${authority}/oauth2/v2.0/token`,
|
const rsp1 = await request({
|
||||||
method: "POST",
|
url: `${authority}/oauth2/v2.0/token`,
|
||||||
contentType: "application/x-www-form-urlencoded",
|
method: "POST",
|
||||||
body: new URLSearchParams({
|
contentType: "application/x-www-form-urlencoded",
|
||||||
tenant: "consumers",
|
body: new URLSearchParams({
|
||||||
client_id: clientID,
|
tenant: "consumers",
|
||||||
scope: SCOPES.join(" "),
|
client_id: clientID,
|
||||||
refresh_token: refreshToken,
|
scope: SCOPES.join(" "),
|
||||||
grant_type: "refresh_token",
|
refresh_token: refreshToken,
|
||||||
}).toString(),
|
grant_type: "refresh_token",
|
||||||
});
|
}).toString(),
|
||||||
|
});
|
||||||
|
|
||||||
const rsp2 = JSON.parse(rsp1);
|
const rsp2 = JSON.parse(rsp1);
|
||||||
// log.info(rsp2);
|
// log.info(rsp2);
|
||||||
|
|
||||||
if (rsp2.error !== undefined) {
|
if (rsp2.error !== undefined) {
|
||||||
return rsp2 as AccessCodeResponseFailedType;
|
return rsp2 as AccessCodeResponseFailedType;
|
||||||
} else {
|
} else {
|
||||||
return rsp2 as AccessCodeResponseSuccessfulType;
|
return rsp2 as AccessCodeResponseSuccessfulType;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log.error(e);
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -207,10 +218,6 @@ export const setConfigBySuccessfullAuthInplace = async (
|
|||||||
const getOnedrivePath = (fileOrFolderPath: string, remoteBaseDir: string) => {
|
const getOnedrivePath = (fileOrFolderPath: string, remoteBaseDir: string) => {
|
||||||
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/special-folders-appfolder?view=odsp-graph-online
|
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/special-folders-appfolder?view=odsp-graph-online
|
||||||
const prefix = `/drive/special/approot:/${remoteBaseDir}`;
|
const prefix = `/drive/special/approot:/${remoteBaseDir}`;
|
||||||
if (fileOrFolderPath.startsWith(prefix)) {
|
|
||||||
// already transformed, return as is
|
|
||||||
return fileOrFolderPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
let key = fileOrFolderPath;
|
let key = fileOrFolderPath;
|
||||||
if (fileOrFolderPath === "/" || fileOrFolderPath === "") {
|
if (fileOrFolderPath === "/" || fileOrFolderPath === "") {
|
||||||
@ -221,7 +228,12 @@ const getOnedrivePath = (fileOrFolderPath: string, remoteBaseDir: string) => {
|
|||||||
key = key.slice(0, key.length - 1);
|
key = key.slice(0, key.length - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
key = `${prefix}/${key}`;
|
if (key.startsWith("/")) {
|
||||||
|
log.warn(`why the path ${key} starts with '/'? but we just go on.`);
|
||||||
|
key = `${prefix}${key}`;
|
||||||
|
} else {
|
||||||
|
key = `${prefix}/${key}`;
|
||||||
|
}
|
||||||
return key;
|
return key;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -638,10 +650,9 @@ export const listAllFromRemote = async (client: WrappedOnedriveClient) => {
|
|||||||
|
|
||||||
export const getRemoteMeta = async (
|
export const getRemoteMeta = async (
|
||||||
client: WrappedOnedriveClient,
|
client: WrappedOnedriveClient,
|
||||||
fileOrFolderPath: string
|
remotePath: string
|
||||||
) => {
|
) => {
|
||||||
await client.init();
|
await client.init();
|
||||||
const remotePath = getOnedrivePath(fileOrFolderPath, client.remoteBaseDir);
|
|
||||||
// log.info(`remotePath=${remotePath}`);
|
// log.info(`remotePath=${remotePath}`);
|
||||||
const rsp = await client.getJson(
|
const rsp = await client.getJson(
|
||||||
`${remotePath}?$select=cTag,eTag,fileSystemInfo,folder,file,name,parentReference,size`
|
`${remotePath}?$select=cTag,eTag,fileSystemInfo,folder,file,name,parentReference,size`
|
||||||
@ -796,12 +807,11 @@ export const uploadToRemote = async (
|
|||||||
|
|
||||||
const downloadFromRemoteRaw = async (
|
const downloadFromRemoteRaw = async (
|
||||||
client: WrappedOnedriveClient,
|
client: WrappedOnedriveClient,
|
||||||
fileOrFolderPath: string
|
remotePath: string
|
||||||
): Promise<ArrayBuffer> => {
|
): Promise<ArrayBuffer> => {
|
||||||
await client.init();
|
await client.init();
|
||||||
const key = getOnedrivePath(fileOrFolderPath, client.remoteBaseDir);
|
|
||||||
const rsp = await client.getJson(
|
const rsp = await client.getJson(
|
||||||
`${key}?$select=@microsoft.graph.downloadUrl`
|
`${remotePath}?$select=@microsoft.graph.downloadUrl`
|
||||||
);
|
);
|
||||||
const downloadUrl: string = rsp["@microsoft.graph.downloadUrl"];
|
const downloadUrl: string = rsp["@microsoft.graph.downloadUrl"];
|
||||||
if (VALID_REQURL) {
|
if (VALID_REQURL) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user