cf部署获取acess token

代码部分来源此贴

可在变量设置refresh token,即可打开网页即用

cf-worker代码部分
const TOKEN_URL_NEO = 'https://token.oaifree.com/api/auth/refresh';
const TOKEN_URL_OAI = 'https://auth0.openai.com/oauth/token';

// 从环境变量中获取 refresh_token
const ENV_REFRESH_TOKEN = globalThis.refresh_token || '';

async function handleRequest(request) {
    const { pathname } = new URL(request.url);

    // 根路径返回 HTML 页面
    if (pathname === "/") {
        return new Response(generateHTML(), {
            headers: { 'Content-Type': 'text/html;charset=UTF-8' },
        });
    } 
    // /get-token 路径获取 Access Token
    else if (pathname === "/get-token") {
        const formData = await request.formData();
        const refreshToken = formData.get("refresh_token");
        const mode = formData.get("mode");
        return await getAccessToken(refreshToken, mode);
    } 
    // 其他路径返回 404
    else {
        return new Response("Not Found", { status: 404 });
    }
}

// 获取 access token 的函数
async function getAccessToken(refreshToken, mode) {
    let url = mode === 'oai' ? TOKEN_URL_OAI : TOKEN_URL_NEO;
    let body, headers;

    if (mode === 'neo') {
        headers = { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' };
        body = `refresh_token=${encodeURIComponent(refreshToken)}`;
    } else {
        headers = { 'Content-Type': 'application/json' };
        body = JSON.stringify({
            redirect_uri: "com.openai.chat://auth0.openai.com/ios/com.openai.chat/callback",
            grant_type: "refresh_token",
            client_id: "pdlLIX2Y72MIl2rhLhTE9VV9bN905kBh",
            refresh_token: refreshToken
        });
    }

    const response = await fetch(url, {
        method: 'POST',
        headers: headers,
        body: body
    });
    
    const result = await response.json();
    if (result.access_token) {
        return new Response(JSON.stringify({ access_token: result.access_token }), {
            headers: { 'Content-Type': 'application/json;charset=UTF-8' }
        });
    } else {
        return new Response(JSON.stringify({ error: result.detail || '获取失败,请检查Refresh Token是否正确' }), {
            headers: { 'Content-Type': 'application/json;charset=UTF-8' },
            status: 400
        });
    }
}

// 生成美化后的 HTML 页面
function generateHTML() {
    return `
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Access Token 获取工具</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                background-color: #F0F8FF;
                padding: 20px;
                margin: 0;
            }
            .container {
                max-width: 600px;
                margin: 50px auto;
                padding: 30px;
                background-color: #FFFFFF;
                border: 2px solid #87CEFA;
                border-radius: 10px;
                box-shadow: 0 4px 8px rgba(135, 206, 250, 0.2);
                text-align: center;
            }
            h1 {
                color: #4682B4;
            }
            button, select {
                padding: 12px 24px;
                margin-top: 20px;
                font-size: 16px;
                font-weight: bold;
                color: #FFFFFF;
                background-color: #87CEFA;
                border: none;
                border-radius: 5px;
                cursor: pointer;
                transition: background-color 0.3s ease;
            }
            button:hover {
                background-color: #4682B4;
            }
            input, textarea {
                width: 90%;
                margin: 20px auto;
                padding: 15px;
                border: 2px solid #87CEFA;
                border-radius: 5px;
                font-size: 14px;
                display: block;
            }
            #copyButton {
                display: none;
            }
            .button-container {
                display: flex;
                justify-content: space-around;
                flex-wrap: wrap;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>Access Token 获取工具</h1>
            <label for="modeSelect">选择模式:</label>
            <select id="modeSelect">
                <option value="neo" selected>Neo</option>
                <option value="oai">OAI</option>
            </select>
            <input type="text" id="refreshTokenInput" placeholder="请输入您的 Refresh Token" value="${ENV_REFRESH_TOKEN}">
            <button id="getTokenButton">获取 Access Token</button>
            <textarea id="accessTokenText" readonly placeholder="获取到的 Access Token 将显示在此处"></textarea>
            <div class="button-container">
                <button id="copyButton">点击复制</button>
                <button id="jumpButton">前往 GPT</button>
            </div>
        </div>
    
        <script>
            document.getElementById('getTokenButton').addEventListener('click', async () => {
                const refreshToken = document.getElementById('refreshTokenInput').value;
                const mode = document.getElementById('modeSelect').value;
                if (!refreshToken) {
                    alert('请输入您的 Refresh Token');
                    return;
                }
                try {
                    const response = await fetch('/get-token', {
                        method: 'POST',
                        body: new URLSearchParams({ refresh_token: refreshToken, mode: mode })
                    });
                    const data = await response.json();
                    if (data.access_token) {
                        const accessTokenText = document.getElementById('accessTokenText');
                        accessTokenText.value = data.access_token;
                        document.getElementById('copyButton').style.display = 'inline';
                    } else {
                        alert('获取 Access Token 失败: ' + data.error);
                    }
                } catch (error) {
                    alert('请求出错,请检查网络连接或刷新页面重试。');
                    console.error(error);
                }
            });
    
            document.getElementById('copyButton').addEventListener('click', () => {
                const accessTokenText = document.getElementById('accessTokenText');
                accessTokenText.select();
                document.execCommand('copy');
                const copyButton = document.getElementById('copyButton');
                copyButton.textContent = '已复制';
                setTimeout(() => {
                    copyButton.textContent = '点击复制';
                }, 3000);
            });
    
            document.getElementById('jumpButton').addEventListener('click', () => {
                window.location.href = 'https://new.oaifree.com/';
            });
        </script>
    </body>
    </html>
    `;
}

// 监听 fetch 事件
addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request));
});

个人部署网页

13 个赞

一直没用过这个,这个 Refresh Token 和直接访问 https://chatgpt.com/api/auth/session 获取到的是一样的吗?

3 个赞

本质是一样的,只是走的始皇的接口,不用我们自己挂梯

3 个赞

感谢大佬分享

2 个赞

RT 怎么获取?

1 个赞

可以参考这个,二级之后在站内获取是最方便的了

收藏了,以后试试

感谢,我一直不知道rft怎么用 :joy:

谢谢楼主,到时候试试

多个账号是不是要建多个woker

能否用rt,10天自动刷新at

多个账号我建议是用油猴脚本了,因为new.oai前面可以自己加前缀.
用油猴脚本识别站点,refresh token内置到脚本内,自动点击登录框并且获取acess token登录,我记得站点里好像有佬友发过

这个是要用rt的,自动刷新的话,是要直接输出到哪里呢?

原来如此,我搞错了

可以看一下这个佬友的帖子
然后油猴脚本可以对匹配网站进行更改
比如
1.new***
2.new***
对应各自的rt

感谢佬友分享

好的 多谢 我去研究下

已部署,感谢佬友分享!

感谢分享,rt的输入框type可以改成password
临时用这个对了页面加密:(加密后无法访问)

可以使用这个套个加密:

const PASSWORD = globalThis.password || '';  // 在环境变量中设置访问密码

addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
    const url = new URL(request.url);
    const cookie = request.headers.get('Cookie') || '';

    // 检查Cookie中是否包含正确的密码
    if (cookie.includes(`password=${PASSWORD}`)) {
        return handleTokenRequest(request);
    }

    // 检查URL参数中是否包含正确的密码
    const params = new URLSearchParams(url.search);
    if (params.get('password') === PASSWORD) {
        const newHeaders = new Headers({ 'Content-Type': 'text/html' });
        newHeaders.append('Set-Cookie', `password=${PASSWORD}; path=/`);

        return new Response(generateHTML(), {
            status: 200,
            headers: newHeaders
        });
    }

    // 返回密码输入表单
    return new Response(`
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Password Protection</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
                height: 100vh;
                background-color: #f4f4f4;
                margin: 0;
            }
            .container {
                background-color: #fff;
                padding: 20px;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                border-radius: 8px;
                text-align: center;
                max-width: 400px;
            }
            input[type="password"] {
                padding: 10px;
                font-size: 16px;
                margin-right: 10px;
                width: 200px;
                border: 1px solid #ddd;
                border-radius: 4px;
                margin-bottom: 20px;
            }
            button {
                padding: 10px 20px;
                font-size: 16px;
                cursor: pointer;
                border: none;
                border-radius: 4px;
                background-color: #007bff;
                color: white;
            }
            button:hover {
                background-color: #0056b3;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <form method="GET">
                <input type="password" name="password" placeholder="Enter password">
                <br>
                <button type="submit">Login</button>
            </form>
        </div>
    </body>
    </html>
    `, {
        headers: { 'Content-Type': 'text/html' }
    });
}
1 个赞