Download test-current-token.php from CatPtain/VvvebJs: direct link, hf CLI and curl.
- Browser
- Download file 13.2 kB
-
https://huggingface.co/spaces/CatPtain/VvvebJs/resolve/main/test-current-token.php
- Command line
-
hf download hf://spaces/CatPtain/VvvebJs/test-current-token.php
-
curl -L -o test-current-token.php https://huggingface.co/spaces/CatPtain/VvvebJs/resolve/main/test-current-token.php
13.2 kB
| // 测试当前 GitHub Token 配置 | |
| // 根据截图中的信息进行测试 | |
| // 从截图推断的配置 | |
| $config = [ | |
| 'owner' => 'CaPaCaptain', // 从截图中的仓库访问看到 | |
| 'repo' => 'VvvebJs_huggingface_db_01', // 从截图中看到 | |
| 'branch' => 'main', | |
| 'path' => 'pages/', | |
| 'token' => $_ENV['GITHUB_TOKEN'] ?? getenv('GITHUB_TOKEN') ?: '' | |
| ]; | |
| function testGitHubAPI($url, $token, $method = 'GET', $data = null) { | |
| $ch = curl_init(); | |
| curl_setopt($ch, CURLOPT_URL, $url); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); | |
| curl_setopt($ch, CURLOPT_TIMEOUT, 15); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, [ | |
| 'Authorization: token ' . $token, | |
| 'User-Agent: VvvebJs-TokenTest/1.0', | |
| 'Accept: application/vnd.github.v3+json', | |
| 'Content-Type: application/json' | |
| ]); | |
| if ($data) { | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); | |
| } | |
| $response = curl_exec($ch); | |
| $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
| $error = curl_error($ch); | |
| curl_close($ch); | |
| return [ | |
| 'success' => $httpCode >= 200 && $httpCode < 300, | |
| 'code' => $httpCode, | |
| 'response' => $response, | |
| 'error' => $error | |
| ]; | |
| } | |
| $results = []; | |
| if ($_POST['test'] ?? false) { | |
| // 测试 1: Token 有效性 | |
| $results['token'] = testGitHubAPI('https://api.github.com/user', $config['token']); | |
| // 测试 2: 仓库访问 | |
| $repoUrl = "https://api.github.com/repos/{$config['owner']}/{$config['repo']}"; | |
| $results['repo'] = testGitHubAPI($repoUrl, $config['token']); | |
| // 测试 3: 分支检查 | |
| $branchUrl = "https://api.github.com/repos/{$config['owner']}/{$config['repo']}/branches/{$config['branch']}"; | |
| $results['branch'] = testGitHubAPI($branchUrl, $config['token']); | |
| // 测试 4: 写入权限测试 | |
| $testFile = 'token-test-' . date('Y-m-d-H-i-s') . '.html'; | |
| $testContent = '<!DOCTYPE html><html><head><title>Token Test</title></head><body><h1>GitHub Token 测试</h1><p>创建时间: ' . date('Y-m-d H:i:s') . '</p></body></html>'; | |
| $createUrl = "https://api.github.com/repos/{$config['owner']}/{$config['repo']}/contents/{$config['path']}{$testFile}"; | |
| $results['write'] = testGitHubAPI($createUrl, $config['token'], 'PUT', [ | |
| 'message' => 'VvvebJs Token 测试文件', | |
| 'content' => base64_encode($testContent), | |
| 'branch' => $config['branch'] | |
| ]); | |
| } | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <title>当前 GitHub Token 测试</title> | |
| <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> | |
| </head> | |
| <body> | |
| <div class="container mt-5"> | |
| <div class="row"> | |
| <div class="col-md-8 mx-auto"> | |
| <h1 class="mb-4">🔑 当前 GitHub Token 配置测试</h1> | |
| <!-- 配置信息 --> | |
| <div class="card mb-4"> | |
| <div class="card-header bg-info text-white"> | |
| <h3 class="mb-0">📋 从截图推断的配置</h3> | |
| </div> | |
| <div class="card-body"> | |
| <div class="row"> | |
| <div class="col-md-6"> | |
| <p><strong>GitHub Owner:</strong> <code><?= htmlspecialchars($config['owner']) ?></code></p> | |
| <p><strong>Repository:</strong> <code><?= htmlspecialchars($config['repo']) ?></code></p> | |
| <p><strong>Token 状态:</strong> <?= !empty($config['token']) ? '✅ 已配置' : '❌ 未找到' ?></p> | |
| </div> | |
| <div class="col-md-6"> | |
| <p><strong>Branch:</strong> <code><?= htmlspecialchars($config['branch']) ?></code></p> | |
| <p><strong>Path:</strong> <code><?= htmlspecialchars($config['path']) ?></code></p> | |
| <p><strong>完整 URL:</strong><br><small><code>https://github.com/<?= htmlspecialchars($config['owner']) ?>/<?= htmlspecialchars($config['repo']) ?></code></small></p> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <?php if (!empty($config['token'])): ?> | |
| <!-- 测试按钮 --> | |
| <div class="card mb-4"> | |
| <div class="card-body text-center"> | |
| <form method="POST"> | |
| <button type="submit" name="test" value="1" class="btn btn-primary btn-lg"> | |
| 🧪 测试 Token 权限 | |
| </button> | |
| </form> | |
| </div> | |
| </div> | |
| <?php if (!empty($results)): ?> | |
| <!-- 测试结果 --> | |
| <div class="card mb-4"> | |
| <div class="card-header"> | |
| <h3>🔬 测试结果</h3> | |
| </div> | |
| <div class="card-body"> | |
| <!-- Token 测试 --> | |
| <div class="mb-4"> | |
| <h5>1. Token 验证</h5> | |
| <?php $test = $results['token']; ?> | |
| <div class="alert alert-<?= $test['success'] ? 'success' : 'danger' ?>"> | |
| <strong>结果:</strong> <?= $test['success'] ? '✅ Token 有效' : '❌ Token 无效' ?> (HTTP <?= $test['code'] ?>) | |
| <?php if ($test['success'] && $test['response']): ?> | |
| <?php $user = json_decode($test['response'], true); ?> | |
| <br><strong>当前用户:</strong> <?= htmlspecialchars($user['login'] ?? 'Unknown') ?> | |
| <br><strong>用户名:</strong> <?= htmlspecialchars($user['name'] ?? 'N/A') ?> | |
| <br><strong>用户类型:</strong> <?= htmlspecialchars($user['type'] ?? 'N/A') ?> | |
| <?php endif; ?> | |
| </div> | |
| </div> | |
| <!-- 仓库测试 --> | |
| <div class="mb-4"> | |
| <h5>2. 仓库访问测试</h5> | |
| <?php $test = $results['repo']; ?> | |
| <div class="alert alert-<?= $test['success'] ? 'success' : 'danger' ?>"> | |
| <strong>结果:</strong> <?= $test['success'] ? '✅ 仓库可访问' : '❌ 仓库无法访问' ?> (HTTP <?= $test['code'] ?>) | |
| <?php if ($test['success'] && $test['response']): ?> | |
| <?php $repo = json_decode($test['response'], true); ?> | |
| <br><strong>仓库全名:</strong> <?= htmlspecialchars($repo['full_name'] ?? 'Unknown') ?> | |
| <br><strong>私有仓库:</strong> <?= ($repo['private'] ?? false) ? '是' : '否' ?> | |
| <br><strong>默认分支:</strong> <?= htmlspecialchars($repo['default_branch'] ?? 'Unknown') ?> | |
| <?php if (isset($repo['permissions'])): ?> | |
| <br><strong>权限:</strong> | |
| - Admin: <?= $repo['permissions']['admin'] ? '✅' : '❌' ?> | |
| - Push: <?= $repo['permissions']['push'] ? '✅' : '❌' ?> | |
| - Pull: <?= $repo['permissions']['pull'] ? '✅' : '❌' ?> | |
| <?php endif; ?> | |
| <?php endif; ?> | |
| </div> | |
| </div> | |
| <!-- 分支测试 --> | |
| <div class="mb-4"> | |
| <h5>3. 分支检查</h5> | |
| <?php $test = $results['branch']; ?> | |
| <div class="alert alert-<?= $test['success'] ? 'success' : 'warning' ?>"> | |
| <strong>结果:</strong> <?= $test['success'] ? '✅ 分支存在' : '⚠️ 分支问题' ?> (HTTP <?= $test['code'] ?>) | |
| <?php if ($test['success'] && $test['response']): ?> | |
| <?php $branch = json_decode($test['response'], true); ?> | |
| <br><strong>分支名:</strong> <?= htmlspecialchars($branch['name'] ?? 'Unknown') ?> | |
| <br><strong>最新提交:</strong> <?= htmlspecialchars(substr($branch['commit']['sha'] ?? '', 0, 8)) ?> | |
| <?php elseif ($test['code'] == 404): ?> | |
| <br><strong>提示:</strong> 分支 "<?= $config['branch'] ?>" 不存在。可能需要使用 "master" 分支。 | |
| <?php endif; ?> | |
| </div> | |
| </div> | |
| <!-- 写入测试 --> | |
| <div class="mb-4"> | |
| <h5>4. 文件写入权限测试</h5> | |
| <?php $test = $results['write']; ?> | |
| <div class="alert alert-<?= $test['success'] ? 'success' : 'danger' ?>"> | |
| <strong>结果:</strong> <?= $test['success'] ? '✅ 写入成功' : '❌ 写入失败' ?> (HTTP <?= $test['code'] ?>) | |
| <?php if ($test['success']): ?> | |
| <br><strong>🎉 恭喜!Token 配置完全正确,可以正常保存文件到 GitHub。</strong> | |
| <br>您可以开始使用 VvvebJs 编辑器了! | |
| <?php else: ?> | |
| <br><strong>错误详情:</strong> | |
| <pre style="font-size: 12px; max-height: 200px; overflow-y: auto;"><?= htmlspecialchars($test['response']) ?></pre> | |
| <?php endif; ?> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <!-- 建议配置 --> | |
| <div class="card"> | |
| <div class="card-header"> | |
| <h3>⚙️ 推荐的环境变量配置</h3> | |
| </div> | |
| <div class="card-body"> | |
| <p>在您的 Hugging Face Space Settings 中设置以下环境变量:</p> | |
| <div class="bg-light p-3 rounded"> | |
| <code>GITHUB_TOKEN=your_token_here</code><br> | |
| <code>GITHUB_OWNER=<?= htmlspecialchars($config['owner']) ?></code><br> | |
| <code>GITHUB_REPO=<?= htmlspecialchars($config['repo']) ?></code><br> | |
| <code>GITHUB_BRANCH=<?= $results['branch']['success'] ? $config['branch'] : 'master' ?></code><br> | |
| <code>GITHUB_PATH=<?= htmlspecialchars($config['path']) ?></code><br> | |
| <code>STORAGE_TYPE=github</code><br> | |
| </div> | |
| <?php if (!$results['branch']['success'] && $results['branch']['code'] == 404): ?> | |
| <div class="alert alert-warning mt-3"> | |
| <strong>⚠️ 注意:</strong> 检测到分支 "main" 不存在,建议使用 "master" 分支。 | |
| </div> | |
| <?php endif; ?> | |
| </div> | |
| </div> | |
| <?php endif; ?> | |
| <?php else: ?> | |
| <!-- Token 未配置 --> | |
| <div class="alert alert-danger"> | |
| <h5>❌ GitHub Token 未配置</h5> | |
| <p>请在环境变量中设置 <code>GITHUB_TOKEN</code>。</p> | |
| <p>Token 应该是您在 GitHub 截图中创建的那个。</p> | |
| </div> | |
| <?php endif; ?> | |
| <!-- 导航 --> | |
| <div class="text-center mt-4"> | |
| <a href="config.php" class="btn btn-secondary me-2">🔙 返回配置</a> | |
| <a href="github-token-helper.html" class="btn btn-info me-2">📖 Token 指南</a> | |
| <a href="editor.html" class="btn btn-primary">🎨 打开编辑器</a> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </body> | |
| </html> |