hmjz100/LinkSwift · error · Error
[百度网盘] 获取令牌失败
Error message
[百度网盘] 获取令牌失败
What it means
Fallback error in getLink(): when getToken() rejects with a non-Error value (a string, plain object, or undefined), the catch handler wraps it into a proper Error, defaulting to the message '[百度网盘] 获取令牌失败'. It means the OAuth authorization round-trip failed and no usable baidu_access_token was obtained.
Source
Thrown at (改)网盘直链下载助手.user.js:6063
const subDirs = res.list.filter(f => f.isdir);
if (subDirs.length > 0) {
files = files.concat(await get(subDirs));
}
}
if (cnt >= 50) {
$doc.find(".loading-popup .swal2-html-container").html(`<div>已获取 ${proc} 个文件~</div><div>休息 3 秒...</div>`);
await base.sleep(3000);
cnt = 0;
}
}
return files;
};
return await get(dirs);
},
async getLink() {
let token = base.getValue("baidu_access_token") || await this.getToken().catch(e => {
if (e instanceof Error) throw e;
throw new Error(e?.message || e || "[百度网盘] 获取令牌失败");
});
// 回退授权
if (!token) {
message.info("提示:<br/>稍后请在新标签页中授权助手哦~");
base.delValue("baidu_access_token");
await base.sleep(3300);
GM_openInTab(config.$baidu.api.getAccessToken, { active: true, insert: true, setParent: true })
let attempts = 0;
const interval = setInterval(() => {
if (base.getValue("baidu_access_token")) {
clearInterval(interval);
token = base.getValue("baidu_access_token")
}
attempts++;
if (attempts > 120) {
clearInterval(interval);
throw new Error("提示:<br/>时间太长,我先撤下啦~");
View on GitHub (pinned to 417ea5e28a)
Solutions
- Re-run the flow and complete the authorization in the popup tab (click 授权/Allow)
- Check the script's Baidu appkey/secret configuration in config.$baidu.api
- Log the original rejection value in getToken to see the real cause before it gets replaced by the generic message
- Verify GM_xmlhttpRequest works (userscript manager permissions, CORS) for the OAuth endpoint
- Check network connectivity / corporate proxy blocking openapi.baidu.com
Example fix
// before
throw new Error(e?.message || e || "[百度网盘] 获取令牌失败");
// after
console.error("getToken failed with:", e); // preserve root cause
const detail = typeof e === "object" ? JSON.stringify(e) : String(e);
throw new Error(`[百度网盘] 获取令牌失败: ${detail || "未知原因"}`); Defensive patterns
Strategy: try-catch
Validate before calling
// pre-check OAuth prerequisites before calling getLink
if (typeof GM_xmlhttpRequest !== "function") throw new Error("userscript 管理器不支持 GM_xmlhttpRequest,无法获取令牌");
if (!config.$baidu.api.appKey) throw new Error("未配置百度应用 appKey"); Type guard
function isOAuthTokenResponse(v) {
return !!v && typeof v === "object"
&& typeof v.access_token === "string" && v.access_token.length > 0
&& (!v.error || v.error === undefined);
} Try / catch
try {
await baidu.getLink();
} catch (e) {
if (String(e.message).includes("获取令牌失败")) {
base.delValue("baidu_access_token");
const ok = await runAuthorizationFlow(); // ensure user completes the popup
if (ok) await baidu.getLink(); else showAuthInstructions();
} else throw e;
} Prevention
- Always complete the authorization popup tab and click Allow before retrying
- Check that the configured Baidu appKey/open API URL are current
- Verify GM_xmlhttpRequest permissions in your userscript manager (Tampermonkey/Violentmonkey)
- Preserve the original rejection value (console.log) before it is replaced by the generic message
- Test token acquisition on a clean profile to rule out extension interference
When it happens
Trigger: this.getToken() rejects with a non-Error rejection value — e.g., the authorization page returned an error string, the token exchange request returned an object without access_token, or the promise rejected with undefined/null.
Common situations: User closes the Baidu authorization tab before consenting; Baidu OAuth endpoint returns {error: '...'} instead of a token; network failure during token exchange; userscript manager (GM_xmlhttpRequest) blocked or misconfigured; scope/appkey misconfigured in the script's config.
Related errors
AI-assisted analysis of hmjz100/LinkSwift@417ea5e28a (2026-09-02).
Data as JSON: /api/errors/2da851cdaafd0c30.
Report an issue: GitHub.