增加获取SharePoint站点ID,更改中国区EndPoint

This commit is contained in:
xiaoman1221 2025-06-13 03:29:12 +08:00
parent 36741886a5
commit 6b3fa5b6e7
2 changed files with 112 additions and 8 deletions

View File

@ -140,12 +140,16 @@
<textarea id="refresh-token" class="form-control" rows="3" readonly onclick="autoCopy(this)"></textarea>
</div>
<div class="mb-3">
<label for="sharepoint_url" class="form-label">SharePoint Site URL</label>
<label for="sharepoint-url" class="form-label">SharePoint Site URL</label>
<label for="sharepoint-url"></label><input type="text" id="sharepoint-url" class="form-control">
</div>
<div class="d-grid gap-2 mb-3">
<button class="btn btn-primary" onclick="getSiteID()">获取 SharePoint 站点ID</button>
</div>
<div class="mb-3">
<label for="sharepoint-id" class="form-label">SharePoint Site URL</label>
<textarea id="sharepoint-id" class="form-control" rows="3" readonly onclick="autoCopy(this)"></textarea>
</div>
<div class="text-muted text-center">
<p style="text-align:center">
本工具所有信息只以Cookie形式存储于浏览器本地<br>
@ -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;

View File

@ -7,8 +7,8 @@ const driver_map: Record<string, string[]> = {
'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) {
}