hmjz100/LinkSwift · error · Error

AccessTokenInvalid

AccessTokenInvalid

Error message

提示:<br/>访问令牌过期了,请刷新后重试~<br/>代码:AccessTokenInvalid

What it means

This userscript (网盘直链下载助手) throws this error when the Aliyun Drive (阿里云盘) backend replies with code "AccessTokenInvalid" for a get-link request. It means the Bearer token sent in the Authorization header is expired or revoked, so the server refuses to issue the download link. The script aborts the batch instead of silently writing a failed dlink, because a stale token requires a re-login/refresh rather than a retry with the same token.

Source

Thrown at (改)网盘直链下载助手.user.js:6643

				if (cached.expires > Date.now()) {
					Object.assign(item, cached.data);
					proc++;
					return false;
				}

				temp.glinks.splice(idx, 1); // 过期删除
				return true;
			});

			for (let i = 0; i < pending.length; i += size) {
				// 当前批次
				const batch = pending.slice(i, i + size);

				await Promise.all(batch.map(async (item) => {
					const res = await base.post(config.$aliyun.api.getLink, { drive_id: item.driveId, file_id: item.fileId }, { "Authorization": token, "X-Canary": "client=windows,app=adrive,version=v6.0.0" });

					if (!res || !res.url || res.code) {
						if (res.code == "AccessTokenInvalid") throw new Error("提示:<br/>访问令牌过期了,请刷新后重试~<br/>代码:" + res.code);
						if (res.code) {
							batch.forEach(item => item.dlink = `获取下载地址失败,服务器说:${res.code},刷新后再试试吧~`);
						} else {
							throw new Error("提示:<br/>获取下载链接失败,刷新网页后再试试吧~");
						}
					};

					batch.forEach(_item => {
						Object.assign(_item, res);
						temp.glinks.push({ id: _item.file_id, expires: (Date.now() + 5 * 60 * 1000), data: res });
					});

					proc++;
					$doc.find(".swal2-html-container").html(`已获取 ${proc} / ${pending.length} 个链接`);
				}));

				// 批次间休息
				if (i + size < pending.length) await base.sleep(1000);

View on GitHub (pinned to 417ea5e28a)

Solutions

  1. Refresh the Aliyun Drive page (F5) so the script picks up a fresh access token, then retry the download.
  2. Log out of Aliyun Drive and log back in to obtain a brand-new token.
  3. Clear the script's stored token (via Tampermonkey storage or the script's settings/reset) and re-authorize.
  4. Check that the token being passed to base.post is the current one, not one captured earlier before a refresh.

Example fix

// before: token captured once and reused
token = base.getStorage('aliyun_token');
await base.post(config.$aliyun.api.getLink, data, { "Authorization": token });

// after: detect invalid token, refresh, retry once
const res = await base.post(config.$aliyun.api.getLink, data, { "Authorization": token });
if (res.code === "AccessTokenInvalid") {
  token = await refreshToken(); // obtain new token before retrying
  return base.post(config.$aliyun.api.getLink, data, { "Authorization": token });
}
Defensive patterns

Strategy: retry

Validate before calling

if (!token || Date.now() >= tokenExpiresAt) {
  token = await refreshToken();
}

Type guard

function hasValidToken(t) {
  return typeof t === 'string' && t.length > 0 && Date.now() < getTokenExpiry(t);
}

Try / catch

try {
  await fetchLink(token);
} catch (e) {
  if (e.message.includes('AccessTokenInvalid')) {
    const fresh = await refreshToken();
    await fetchLink(fresh);
  } else throw e;
}

Prevention

When it happens

Trigger: The script POSTs to config.$aliyun.api.getLink with an Authorization header containing a token that the Aliyun server has expired; res.code === "AccessTokenInvalid" in the batched Promise.all handler at line 6643.

Common situations: The user logged into Aliyun Drive a long time ago so the cached token expired; the user logged out and back in elsewhere invalidating the old token; the script refreshed the token in another tab and the current page holds a stale copy; Aliyun invalidated tokens server-side (e.g. after password change or security event).

Related errors


AI-assisted analysis of hmjz100/LinkSwift@417ea5e28a (2026-09-02). Data as JSON: /api/errors/8b15fbf792916abd. Report an issue: GitHub.