V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  blackcatjlu  ›  全部回复第 1 页 / 共 1 页
回复总数  2
174 天前
回复了 blackcatjlu 创建的主题 OpenAI ChatGPT Plus 强制开通,是怎么做到的?
async function fetchToken() {
try {
const response = await fetch('/api/auth/session');

if (!response.ok) throw new Error(`${response.status} ${response.statusText}`);
const responseData = await response.json();
const { accessToken } = responseData;

return accessToken;
} catch (error) {
console.error('session 获取过程中出现错误', error);
}
}

async function fetchUrl(token) {
try {
const response = await fetch('/backend-api/payments/checkout', {
method: 'POST',
headers: {
// 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/119.0', // 默认使用浏览器的 UA
'Authorization': `Bearer ${token}`
},
redirect: 'follow',
referrerPolicy: 'no-referrer'
});

if (!response.ok) throw new Error(`${response.status} ${response.statusText}`);
const urlObject = await response.json();
return urlObject;
} catch (error) {
console.error('跳转过程中出现错误', error);
}
}

(async () => {
const token = await fetchToken();
if (token) {
const urlObject = await fetchUrl(token);
if (urlObject && urlObject.url) {
window.open(urlObject.url, '_blank'); // 在新标签页中打开 URL
}
}
})();
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3306 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 12ms · UTC 13:30 · PVG 21:30 · LAX 06:30 · JFK 09:30
Developed with CodeLauncher
♥ Do have faith in what you're doing.