blackcatjlu 最近的时间轴更新
blackcatjlu

blackcatjlu

V2EX 第 567716 号会员,加入于 2022-01-02 15:43:47 +08:00
blackcatjlu 最近回复了
2023-11-16 22:42:01 +08:00
回复了 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   ·   实用小工具   ·   3566 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 13ms · UTC 05:03 · PVG 13:03 · LAX 21:03 · JFK 00:03
Developed with CodeLauncher
♥ Do have faith in what you're doing.