Decap CMS本地代理访问私有GitHub仓库持续返回404 Not Found问题求助
Decap CMS本地代理访问私有GitHub仓库持续返回404 Not Found问题求助
各位开发者朋友,我最近在搭建Decap CMS的独立实例,通过本地代理后端连接私有GitHub仓库,但遇到了一个卡了好久的问题:每次尝试从CMS UI发布新条目时,都会收到「Failed to load entry」或「API Error」的提示,查看控制台和代理日志发现,GitHub API始终返回404 Not Found。
我已经反复确认过:目标仓库和对应的文件夹确实存在,而且配置的个人访问令牌(PAT)也赋予了全仓库权限,但问题依然没有解决。下面是我的详细排查信息,麻烦大家帮忙分析下可能的原因。
错误日志详情
PAT验证日志
--- PAT Verification --- PAT loaded: ghp_1...kphmC # 隐私处理后的令牌
Decap CMS代理日志
getMedia操作(404)
--- Decap CMS Proxy Log --- Decap CMS Action: getMedia GitHub URL to be fetched: https://api.github.com/repos/random-user/random-repo/contents/assets/uploads?ref=main ... GitHub Response Status: 404 Fallback: GitHub returned 404 for a LISTING path. Returning an empty array to the CMS. ...
persistEntry操作(404)
--- Decap CMS Proxy Log --- Decap CMS Action: persistEntry GitHub URL to be fetched: https://api.github.com/repos/random-user/random-repo/contents/news/entry.md ... GitHub Response Status: 404 GitHub Response Data (first 200 chars): {"message":"Not Found","documentation_url":"https://docs.github.com/rest","status":"404"}
我的配置代码
config.yml
backend: name: proxy proxy_url: http://localhost:3000/api/github branch: main media_folder: "assets/uploads" public_folder: "/assets/uploads" collections: - name: "posts" label: "Posts" folder: "news" create: true fields: - {label: "Title", name: "title", widget: "string"} - {label: "Body", name: "body", widget: "markdown"}
server.js
// 加载.env文件中的密钥 require('dotenv').config(); const express = require('express'); const path =require('path'); const apiHandler = require('./api/github.js'); const app = express(); const port = 3000; app.use(express.json()); app.use('/api/github', apiHandler); app.use(express.static(path.join(__dirname, 'public'))); app.get('/*', (req, res) => { res.sendFile(path.join(__dirname, 'public', 'index.html')); }); app.listen(port, () => { console.log(`✅ Server is working! Listening at http://localhost:${port}`); });
github.js(代理核心逻辑)
const fetch = require("node-fetch"); module.exports = async (req, res) => { const { action, params } = req.body; // 这里替换为了示例仓库名 let githubUrl = `https://api.github.com/repos/random-user/random-repo`; // 日志中已验证PAT已加载,此处省略验证逻辑 if (action === 'entriesByFolder') { githubUrl += `/contents/${params.folder}?ref=${params.branch}`; } else if (action === 'getEntry') { githubUrl += `/contents/${params.path}?ref=${params.branch}`; } else if (action === 'getMedia') { githubUrl += `/contents/${params.mediaFolder}?ref=${params.branch}`; } else if (action === 'persistEntry') { const file = params.dataFiles[0]; githubUrl += `/contents/${file.path}`; } // 后续的fetch请求和响应处理逻辑(已正确处理列表类请求的404 fallback) };
我的疑问
- 既然已经确认仓库结构和路径完全匹配,为什么GitHub API会返回404?
- 日志显示PAT已成功加载,我也尝试过用
Authorization: token和Authorization: Bearer两种格式携带令牌,但访问私有仓库时依然返回404,是不是令牌的认证逻辑有隐性问题?
麻烦各位帮忙点拨下,实在是卡在这里找不到突破口了😭
内容来源于stack exchange




