From 6b3fa5b6e748b6bb910f4413b4fd4bcaebb39e68 Mon Sep 17 00:00:00 2001 From: xiaoman1221 Date: Fri, 13 Jun 2025 03:29:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96SharePoint?= =?UTF-8?q?=E7=AB=99=E7=82=B9ID,=E6=9B=B4=E6=94=B9=E4=B8=AD=E5=9B=BD?= =?UTF-8?q?=E5=8C=BAEndPoint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/index.html | 112 +++++++++++++++++++++++++++++++++++++++++++++- src/oneui.ts | 8 +--- 2 files changed, 112 insertions(+), 8 deletions(-) diff --git a/public/index.html b/public/index.html index 27aaa34..6ef1298 100644 --- a/public/index.html +++ b/public/index.html @@ -140,12 +140,16 @@
- +
+
+ + +

本工具所有信息只以Cookie形式存储于浏览器本地
@@ -277,9 +281,113 @@ } getToken(); - function getSiteID(){ + // 获取站点ID + function getSiteID() { + const siteUrl = document.getElementById("sharepoint-url").value.trim(); + const access_token = document.getElementById("access-token").value.trim(); + const refresh_token = document.getElementById("refresh-token").value.trim(); + const client_uid = document.getElementById("client-id").value.trim(); + const client_key = document.getElementById("app-secret").value.trim(); + const site_type = document.getElementById("site-select").value; + const idElement = document.getElementById("sharepoint-id"); + // 定义站点的API Endpoint + const GATEWAYS = { + "onedrive_go": "https://graph.microsoft.com/v1.0/sites/", + "onedrive_cn": "https://microsoftgraph.chinacloudapi.cn/v1.0/sites/", + "onedrive_us": "https://graph.microsoft.us/v1.0/sites/", + "onedrive_de": "https://graph.microsoft.de/v1.0/sites/" + }; + + // 定义错误信息 + const ERROR_MESSAGES = { + MISSING_CREDENTIALS: "请先填写客户端ID和应用机密", + MISSING_TOKENS: "请获取Token", + MISSING_URL: "请填写您的SharePoint URL", + NOT_SUPPORTED: "仅支持OneDrive相关API", + NOT_FOUND: "站点不存在", + BAD_REQUEST: "获取出现问题,请检查权限和站点URL,站点URL示例:https://demo.sharepoint.com/site/demo", + DEFAULT: "请求发生错误" + }; + + // 验证 + if (!client_uid || !client_key) { + idElement.value = ERROR_MESSAGES.MISSING_CREDENTIALS; + return; + } + if (!access_token || !refresh_token) { + idElement.value = ERROR_MESSAGES.MISSING_TOKENS; + return; + } + if (!siteUrl) { + idElement.value = ERROR_MESSAGES.MISSING_URL; + return; + } + if (!site_type.includes("onedrive")) { + idElement.value = ERROR_MESSAGES.NOT_SUPPORTED; + return; + } + if (!GATEWAYS[site_type]) { + idElement.value = ERROR_MESSAGES.DEFAULT; + return; + } + + // 获取ID + try { + const urlParts = siteUrl.replace("https://", "").split("/"); + const site_hostname = urlParts[0]; + const site_sub_path = urlParts[1]; + const site_name = urlParts[2]; + const site_path = site_sub_path + "/" +site_name; + const reqUrl = `${GATEWAYS[site_type]}${site_hostname}:/${site_path}`; + const headers = { + "Accept": "application/json", + "Content-Type": "application/json", + "Authorization": `Bearer ${access_token}` + }; + + fetch(reqUrl, { + method: "GET", + headers: headers + }) + .then(async (res) => { + if (!res.ok) { + if (res.status === 404) { + idElement.value = ERROR_MESSAGES.NOT_FOUND; + return; + } else if (res.status === 400) { + idElement.value = ERROR_MESSAGES.BAD_REQUEST; + return; + } else { + idElement.value = `${ERROR_MESSAGES.DEFAULT} (HTTP ${res.status})`; + return; + } + } + + try { + const result = await res.json(); + if (result.id) { + idElement.value = result.id; + } else if (result.error) { + idElement.value = result.error.message || ERROR_MESSAGES.DEFAULT; + } else { + idElement.value = ERROR_MESSAGES.DEFAULT; + } + } catch (error) { + idElement.value = ERROR_MESSAGES.DEFAULT; + console.error("处理响应时出错:", error); + } + }) + .catch((error) => { + idElement.value = ERROR_MESSAGES.BAD_REQUEST; + console.error("请求失败:", error); + }); + } catch (error) { + idElement.value = ERROR_MESSAGES.BAD_REQUEST; + console.error("URL解析失败:", error); + } } + //手动切换主题模式 function toggleTheme() { const html = document.documentElement; diff --git a/src/oneui.ts b/src/oneui.ts index 4ebfd48..f9188c9 100644 --- a/src/oneui.ts +++ b/src/oneui.ts @@ -7,8 +7,8 @@ const driver_map: Record = { 'https://login.microsoftonline.com/common/oauth2/v2.0/token' ], "onedrive_cn": [ - 'https://login.chinacloudapi.cn/common/oauth2/v2.0/authorize', - 'https://microsoftgraph.chinacloudapi.cn/common/oauth2/v2.0/token' + 'https://login.chinacloudapi.cn/common/oauth2/authorize', + 'https://login.chinacloudapi.cn/common/oauth2/token' ], "onedrive_de": [ 'https://login.microsoftonline.de/common/oauth2/v2.0/authorize', @@ -122,8 +122,4 @@ export async function oneToken(c: Context) { + `&client_uid=${client_uid}` + `&client_key=${client_key}`); } -} -// 获取SharePoint Site ID ############################################################################## -export async function getSiteID(c: Context) { - } \ No newline at end of file