Skip to content

Commit

Permalink
change onedrive auth scope to app folder
Browse files Browse the repository at this point in the history
  • Loading branch information
troyeguo committed Jan 2, 2024
1 parent 1801573 commit 85ea015
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/assets/locales/zh-TW/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"Downloading, please wait": "下載中,請稍候",
"Uploading, please wait": "上傳中,請稍候",
"Import": "從本機導入",
"Backup": "備份和還原",
"Backup": "備份",
"Search my library": "搜尋我的書庫",
"Search my notes": "搜索我的筆記",
"Search my highlights": "搜索我的重點",
Expand Down
2 changes: 1 addition & 1 deletion src/constants/driveList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const driveList = [
id: 4,
name: "OneDrive",
icon: "onedrive",
url: `https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=${driveConfig.onedriveClientId}&scope=files.readwrite offline_access&response_type=code&redirect_uri=${driveConfig.callbackUrl}`,
url: `https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=${driveConfig.onedriveClientId}&scope=files.readwrite.appfolder offline_access&response_type=code&redirect_uri=${driveConfig.callbackUrl}`,
},
{
id: 5,
Expand Down
22 changes: 11 additions & 11 deletions src/utils/syncUtils/onedrive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { restore } from "./restoreUtil";
import StorageUtil from "../serviceUtils/storageUtil";
import { driveConfig } from "../../constants/driveList";
import axios from "axios";

class OneDriveUtil {
static UploadFile(blob: any) {
return new Promise<boolean>(async (resolve, reject) => {
Expand All @@ -17,7 +16,7 @@ class OneDriveUtil {
});
const accessToken = res.data.access_token; // 替换为实际的访问令牌
const uploadSessionUrl =
"https://graph.microsoft.com/v1.0/me/drive/root:/Apps/KoodoReader/" +
"https://graph.microsoft.com/v1.0/me/drive/special/approot:/" +
file.name +
":/createUploadSession";

Expand All @@ -40,7 +39,7 @@ class OneDriveUtil {
},
});

console.log("File uploaded successfully:", response.data);
console.log("File uploaded successfully:", response);
} catch (error) {
console.error("Error occurred during file upload:", error);
resolve(false);
Expand All @@ -50,27 +49,28 @@ class OneDriveUtil {
}
static DownloadFile() {
return new Promise<boolean>(async (resolve, reject) => {
const filename = "data.zip";
var refresh_token = StorageUtil.getReaderConfig("onedrive_token") || "";
let res = await axios.post(driveConfig.onedriveRefreshUrl, {
refresh_token,
redirect_uri: driveConfig.callbackUrl,
});
const accessToken = res.data.access_token; // 替换为实际的访问令牌
const downloadUrl = `https://graph.microsoft.com/v1.0/me/drive/root:/Apps/KoodoReader/data.zip:/content`;

const downloadUrl = `https://graph.microsoft.com/v1.0/me/drive/special/approot:/${filename}:/content`;
console.log(accessToken);
try {
const response = await axios.get(downloadUrl, {
responseType: "blob",
headers: {
Authorization: "Bearer " + accessToken,
responseType: "blob", // 设置响应类型为 Blob
},
});

// 从响应中获取文件内容
const fileContent = response.data;
let fileTemp = new File([fileContent], "data.zip", {
let blobTemp: any = new Blob([response.data], {
type: "application/zip",
});
let fileTemp = new File([blobTemp], filename, {
lastModified: new Date().getTime(),
type: fileContent.type,
type: blobTemp.type,
});
let result = await restore(fileTemp);
if (!result) resolve(false);
Expand Down

0 comments on commit 85ea015

Please sign in to comment.