{"record":{"id":"2da851cdaafd0c30","repo":"hmjz100/LinkSwift","slug":"error","errorCode":null,"errorMessage":"[百度网盘] 获取令牌失败","messagePattern":"\\[百度网盘\\] 获取令牌失败","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"（改）网盘直链下载助手.user.js","lineNumber":6063,"sourceCode":"\t\t\t\t\t\tconst subDirs = res.list.filter(f => f.isdir);\r\n\t\t\t\t\t\tif (subDirs.length > 0) {\r\n\t\t\t\t\t\t\tfiles = files.concat(await get(subDirs));\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\tif (cnt >= 50) {\r\n\t\t\t\t\t\t$doc.find(\".loading-popup .swal2-html-container\").html(`<div>已获取 ${proc} 个文件~</div><div>休息 3 秒...</div>`);\r\n\t\t\t\t\t\tawait base.sleep(3000);\r\n\t\t\t\t\t\tcnt = 0;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\treturn files;\r\n\t\t\t};\r\n\t\t\treturn await get(dirs);\r\n\t\t},\r\n\t\tasync getLink() {\r\n\t\t\tlet token = base.getValue(\"baidu_access_token\") || await this.getToken().catch(e => {\r\n\t\t\t\tif (e instanceof Error) throw e;\r\n\t\t\t\tthrow new Error(e?.message || e || \"[百度网盘] 获取令牌失败\");\r\n\t\t\t});\r\n\r\n\t\t\t// 回退授权\r\n\t\t\tif (!token) {\r\n\t\t\t\tmessage.info(\"提示：<br/>稍后请在新标签页中授权助手哦~\");\r\n\t\t\t\tbase.delValue(\"baidu_access_token\");\r\n\t\t\t\tawait base.sleep(3300);\r\n\t\t\t\tGM_openInTab(config.$baidu.api.getAccessToken, { active: true, insert: true, setParent: true })\r\n\t\t\t\tlet attempts = 0;\r\n\t\t\t\tconst interval = setInterval(() => {\r\n\t\t\t\t\tif (base.getValue(\"baidu_access_token\")) {\r\n\t\t\t\t\t\tclearInterval(interval);\r\n\t\t\t\t\t\ttoken = base.getValue(\"baidu_access_token\")\r\n\t\t\t\t\t}\r\n\t\t\t\t\tattempts++;\r\n\t\t\t\t\tif (attempts > 120) {\r\n\t\t\t\t\t\tclearInterval(interval);\r\n\t\t\t\t\t\tthrow new Error(\"提示：<br/>时间太长，我先撤下啦~\");\r","sourceCodeStart":6045,"sourceCodeEnd":6081,"githubUrl":"https://github.com/hmjz100/LinkSwift/blob/417ea5e28a3e1a90339710e17356e9fd22b8a34b/（改）网盘直链下载助手.user.js#L6045-L6081","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nthrow new Error(e?.message || e || \"[百度网盘] 获取令牌失败\");\n// after\nconsole.error(\"getToken failed with:\", e); // preserve root cause\nconst detail = typeof e === \"object\" ? JSON.stringify(e) : String(e);\nthrow new Error(`[百度网盘] 获取令牌失败: ${detail || \"未知原因\"}`);","handlingStrategy":"try-catch","validationCode":"// pre-check OAuth prerequisites before calling getLink\nif (typeof GM_xmlhttpRequest !== \"function\") throw new Error(\"userscript 管理器不支持 GM_xmlhttpRequest，无法获取令牌\");\nif (!config.$baidu.api.appKey) throw new Error(\"未配置百度应用 appKey\");","typeGuard":"function isOAuthTokenResponse(v) {\n  return !!v && typeof v === \"object\"\n    && typeof v.access_token === \"string\" && v.access_token.length > 0\n    && (!v.error || v.error === undefined);\n}","tryCatchPattern":"try {\n  await baidu.getLink();\n} catch (e) {\n  if (String(e.message).includes(\"获取令牌失败\")) {\n    base.delValue(\"baidu_access_token\");\n    const ok = await runAuthorizationFlow(); // ensure user completes the popup\n    if (ok) await baidu.getLink(); else showAuthInstructions();\n  } else throw e;\n}","preventionTips":["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"],"tags":["baidu-netdisk","oauth","token-acquisition","userscript"],"backgroundTag":"oauth-authorization-failed","analyzedSha":"417ea5e28a3e1a90339710e17356e9fd22b8a34b","analyzedAt":"2026-09-02T18:13:32.837Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}