You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用GitHub API创建私有仓库时遭遇404错误的排查与解决

GitHub私有仓库创建404错误排查与解决

初始问题

尝试用PHP代码为GitHub账号创建私有仓库时,收到404错误:

{
    "message":"Not Found",
    "documentation_url":"https://docs.github.com/rest",
    "status":"404"
}

使用的PHP代码如下:

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://api.github.com/MY_USERNAME/repos',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_HTTPHEADER => array(
        "Accept: application/vnd.github+json",
        "Authorization: Bearer MY_TOKEN",
        "X-GitHub-Api-Version: 2022-11-28",
        "User-Agent: MY_APP_NAME"
    ),
    CURLOPT_POSTFIELDS => array(
        "name" => "test-repo-123",
        "description" => "This is your first repo!",
        "homepage" => "https://MY_URL",
        "private" => true,
        "is_template" => true
    ),
));

$response = curl_exec($curl);

curl_close($curl);

已创建GitHub App并安装到个人账号,若移除请求头里的"User-Agent: MY_APP_NAME",会收到以下错误:

Request forbidden by administrative rules. Please make sure your request has a User-Agent header (https://docs.github.com/en/rest/overview/resources-in-the-rest-api#user-agent-required). Check https://developer.github.com for other possible causes.

MY_USERNAME, MY_TOKEN, MY_APP_NAME, MY_URL均已填入正确信息,询问问题所在。

更新后问题

修正代码后仍收到404错误:

{
    "message":"Not Found",
    "documentation_url":"https://docs.github.com/rest/repos/repos#create-a-repository-for-the-authenticated-user",
    "status":"404"
}

修改为通过GitHub OAuth获取token的代码如下:

Route::get('/route1', function () {
    if (Session::get('authorization') == '1') {
        Session::put('authorization', '2');

        $code = request()->input('code');

        if (!$code) {
            return abort(404);
        }
    
        $res = Http::post('https://github.com/login/oauth/access_token', [
            'client_id' => 'MY_CLIENT_ID',
            'client_secret' => 'MY_SECRET',
            'code' => $code,
            'redirect_uri' => 'https://MY_URL/route1',
        ]);
    
        $query_str = $res->body();
        $data = [];
        parse_str($query_str, $data);

        Session::forget('authorization');
        if (!isset($data['access_token'])) {
            return abort(404);
        }
        
        $curl = curl_init();
    
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api.github.com/user/repos',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_HTTPHEADER => array(
                "Accept: application/vnd.github+json",
                "Authorization: Bearer {$data['access_token']}",
                "X-GitHub-Api-Version: 2022-11-28",
                "User-Agent: TryfonEuAuth"
            ),
            CURLOPT_POSTFIELDS => array(
                "name" => "my-new-repo-name",
                "description" => "This is your first repo!",
                "homepage" => "https://MY_HOMEPAGE",
                "private" => true,
                "is_template" => true
            ),
        ));
    
        $response = curl_exec($curl);
    
        curl_close($curl);
    
        Log::info("aa", [$response]);
    
        dd($response);
    }
});

Route::get('/oauth', function () {
    Session::put('authorization', '1');
    return redirect('https://github.com/login/oauth/authorize?client_id=MY_CLIENT&state=MY_HASH');
})->middleware('auth.basic');

询问当前仍报错的原因。

解决方案

问题核心是请求体需以RAW JSON格式发送,而非数组形式。无需使用OAuth,直接在GitHub个人面板生成有效期30天的token即可。可用代码如下:

$res = Http::withHeaders([
        'Authorization' => 'Bearer MY_TOKEN',
        'Content-Type' => 'application/json',
        'User-Agent' => 'Laravel/11.31',
    ])->withBody(
'{
"name":"my-test-repo-2",
"description":"test repo 2!",
"homepage":"https://github.com",
"private":true,
"is_template":true
}'
    )->post('https://api.github.com/user/repos');

内容的提问来源于stack exchange,提问作者mpaw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 19:29:50