GitHub
GitHub
通过托管的 OAuth 认证访问 GitHub REST API。管理仓库、议题、拉取请求、提交、分支、用户等。
快速开始
# 获取认证用户
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/github/user')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
基础 URL
https://gateway.maton.ai/github/{native-api-path}
替换{native-api-path}为实际的 GitHub API 端点路径。网关将请求代理到api.github.com并自动注入您的 OAuth 令牌。
认证
所有请求都需要在 Authorization 头部包含 Maton API 密钥:
Authorization: Bearer $MATON_API_KEY
环境变量:将您的 API 密钥设置为MATON_API_KEY:
export MATON_API_KEY="YOUR_API_KEY"
获取您的 API 密钥
- 登录或注册账户:maton.ai
- 前往maton.ai/settings
- 复制您的 API 密钥
连接管理
请在以下地址管理您的 GitHub OAuth 连接https://ctrl.maton.ai。
列出连接
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections?app=github&status=ACTIVE')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
创建连接
python <<'EOF'
import urllib.request, os, json
data = json.dumps({'app': 'github'}).encode()
req = urllib.request.Request('https://ctrl.maton.ai/connections', data=data, method='POST')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
req.add_header('Content-Type', 'application/json')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
获取连接
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections/{connection_id}')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
响应:
{
"connection": {
"connection_id": "83e7c665-60f6-4a64-816c-5e287ea8982f",
"status": "ACTIVE",
"creation_time": "2026-02-06T03:00:43.860014Z",
"last_updated_time": "2026-02-06T03:01:06.027323Z",
"url": "https://connect.maton.ai/?session_token=...",
"app": "github",
"metadata": {}
}
}
在浏览器中打开返回的url以完成 OAuth 授权。
删除连接
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections/{connection_id}', method='DELETE')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
指定连接
如果您有多个 GitHub 连接,请使用Maton-Connection请求头来指定使用哪一个:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://gateway.maton.ai/github/user')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
req.add_header('Maton-Connection', '83e7c665-60f6-4a64-816c-5e287ea8982f')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
如未指定,网关将使用默认(最早)的活动连接。
API 参考
用户
获取已验证用户
GET /github/user
通过用户名获取用户
GET /github/users/{username}
列出用户
GET /github/users?since={user_id}&per_page=30
仓库
列出用户仓库
GET /github/user/repos?per_page=30&sort=updated
查询参数:类型(全部、所有者、公开、私有、成员),排序(创建时间、更新时间、推送时间、全名),方向(升序、降序),每页数量,页码
列出组织仓库
GET /github/orgs/{org}/repos?per_page=30
获取仓库
GET /github/repos/{owner}/{repo}
创建仓库(用户)
POST /github/user/repos
Content-Type: application/json
{
"name": "my-new-repo",
"description": "一个新仓库",
"private": true,
"auto_init": true
}
创建仓库(组织)
POST /github/orgs/{org}/repos
Content-Type: application/json
{
"name": "my-new-repo",
"description": "一个新仓库",
"private": true
}
更新仓库
PATCH /github/repos/{owner}/{repo}
Content-Type: application/json
{
"description": "更新后的描述",
"has_issues": true,
"has_wiki": false
}
删除仓库
DELETE /github/repos/{owner}/{repo}
仓库内容
列出内容
GET /github/repos/{owner}/{repo}/contents/{path}
获取文件内容
GET /github/repos/{owner}/{repo}/contents/{path}?ref={branch}
创建或更新文件
PUT /github/repos/{owner}/{repo}/contents/{path}
Content-Type: application/json
{
"message": "创建新文件",
"content": "SGVsbG8gV29ybGQh",
"branch": "main"
}
注意:content必须是Base64编码。
删除文件
DELETE /github/repos/{owner}/{repo}/contents/{path}
Content-Type: application/json
{
"message": "删除文件",
"sha": "{file_sha}",
"branch": "main"
}
分支
列出分支
GET /github/repos/{owner}/{repo}/branches?per_page=30
获取分支
GET /github/repos/{owner}/{repo}/branches/{branch}
重命名分支
POST /github/repos/{owner}/{repo}/branches/{branch}/rename
Content-Type: application/json
{
"new_name": "new-branch-name"
}
合并分支
POST /github/repos/{owner}/{repo}/merges
Content-Type: application/json
{
"base": "main",
"head": "feature-branch",
"commit_message": "Merge feature branch"
}
提交
列出提交
GET /github/repos/{owner}/{repo}/commits?per_page=30
查询参数:sha(分支名称或提交 SHA),path(文件路径),author,committer,since,until每页数量页码获取提交GET /github/repos/{owner}/{repo}/commits/{ref}
比较两次提交
GET /github/repos/{owner}/{repo}/compare/{base}...{head}
问题
列出仓库问题
GET /github/repos/{owner}/{repo}/issues?state=open&per_page=30
查询参数:
状态
(开放、关闭、全部),标签,负责人,创建者,被提及者,排序方式,sort,方向,自从,每页,页码
获取议题
GET /github/repos/{owner}/{repo}/issues/{issue_number}
创建议题
POST /github/repos/{owner}/{repo}/issues
Content-Type: application/json
{
"title": "发现一个错误",
"body": "错误描述在此",
"labels": ["bug"],
"assignees": ["用户名"]
}
更新议题
PATCH /github/repos/{owner}/{repo}/issues/{issue_number}
Content-Type: application/json
{
"state": "已关闭",
"state_reason": "已完成"
}
锁定议题
PUT /github/repos/{owner}/{repo}/issues/{issue_number}/lock
Content-Type: application/json
{
"lock_reason": "已解决"
}
解锁议题
DELETE /github/repos/{owner}/{repo}/issues/{issue_number}/lock
议题评论
列出议题评论
GET /github/repos/{owner}/{repo}/issues/{issue_number}/comments?per_page=30
创建议题评论
POST /github/repos/{owner}/{repo}/issues/{issue_number}/comments
Content-Type: application/json
{
"body": "这是一条评论"
}
更新议题评论
PATCH /github/repos/{owner}/{repo}/issues/comments/{comment_id}
Content-Type: application/json
{
"body": "已更新的评论"
}
删除议题评论
DELETE /github/repos/{owner}/{repo}/issues/comments/{comment_id}
标签
列出标签
GET /github/repos/{owner}/{repo}/labels?per_page=30
创建标签
POST /github/repos/{owner}/{repo}/labels
Content-Type: application/json
{
"name": "priority:high",
"color": "ff0000",
"description": "高优先级议题"
}
里程碑
列出里程碑
GET /github/repos/{owner}/{repo}/milestones?state=open&per_page=30
创建里程碑
POST /github/repos/{owner}/{repo}/milestones
Content-Type: application/json
{
"title": "v1.0",
"state": "open",
"description": "首次发布",
"due_on": "2026-03-01T00:00:00Z"
}
拉取请求
列出拉取请求
GET /github/repos/{owner}/{repo}/pulls?state=open&per_page=30
查询参数:状态(开启、关闭、全部),来源分支,目标分支,排序方式,方向,每页数量,页码
获取拉取请求
GET /github/repos/{owner}/{repo}/pulls/{pull_number}
创建拉取请求
POST /github/repos/{owner}/{repo}/pulls
Content-Type: application/json
{
"title": "新功能",
"body": "变更描述",
"head": "功能分支",
"base": "主分支",
"draft": false
}
更新拉取请求
PATCH /github/repos/{owner}/{repo}/pulls/{pull_number}
Content-Type: application/json
{
"title": "更新后的标题",
"state": "closed"
}
列出拉取请求的提交
GET /github/repos/{owner}/{repo}/pulls/{pull_number}/commits?per_page=30
列出拉取请求的文件
GET /github/repos/{owner}/{repo}/pulls/{pull_number}/files?per_page=30
检查是否已合并
GET /github/repos/{owner}/{repo}/pulls/{pull_number}/merge
合并拉取请求
PUT /github/repos/{owner}/{repo}/pulls/{pull_number}/merge
Content-Type: application/json
{
"commit_title": "合并拉取请求",
"merge_method": "squash"
}
合并方法:merge、squash、rebase
拉取请求审查
列出审查
GET /github/repos/{owner}/{repo}/pulls/{pull_number}/reviews?per_page=30
创建审查
POST /github/repos/{owner}/{repo}/pulls/{pull_number}/reviews
Content-Type: application/json
{
"body": "看起来不错!",
"event": "APPROVE"
}
事件:APPROVE、REQUEST_CHANGES、COMMENT
搜索
搜索仓库
GET /github/search/repositories?q={query}&per_page=30
查询示例:
tetris+language:python- 包含"tetris"的Python仓库react+stars:>10000- 包含"react"且星标数超过1万的仓库
搜索议题
GET /github/search/issues?q={query}&per_page=30
查询示例:
bug+is:open+is:issue- 包含"bug"的开放议题author:username+is:pr- 按作者搜索拉取请求
搜索代码
GET /github/search/code?q={query}&per_page=30
查询示例:
addClass+repo:facebook/react- 在特定仓库中搜索"addClass"function+extension:js- JavaScript函数
注意:代码搜索在广泛查询时可能会超时。
搜索用户
GET /github/search/users?q={query}&per_page=30
组织
列出用户组织
GET /github/user/orgs?per_page=30
注意:需要read:org授权范围。
获取组织信息
GET /github/orgs/{org}
列出组织成员
GET /github/orgs/{org}/members?per_page=30
速率限制
获取速率限制
GET /github/rate_limit
响应:
{
"rate": {
"limit": 5000,
"remaining": 4979,
"reset": 1707200000
},
"resources": {
"core": { "limit": 5000, "remaining": 4979 },
"search": { "limit": 30, "remaining": 28 }
}
}
分页
GitHub 使用基于页面和基于链接的分页:
GET /github/repos/{owner}/{repo}/issues?per_page=30&page=2
响应头中包含分页链接:
Link: <url>; rel="next", <url>; rel="last"
常见的分页参数:
per_page: 每页结果数(最大100,默认30)page: 页码(默认1)
部分端点使用基于游标的分页,带有since参数(例如,列出用户)。
代码示例
JavaScript
const response = await fetch(
'https://gateway.maton.ai/github/repos/owner/repo/issues?state=open&per_page=10',
{
headers: {
'Authorization': `Bearer ${process.env.MATON_API_KEY}`
}
}
);
const issues = await response.json();
Python
import os
import requests
response = requests.get(
'https://gateway.maton.ai/github/repos/owner/repo/issues',
headers={'Authorization': f'Bearer {os.environ["MATON_API_KEY"]}'},
params={'state': 'open', 'per_page': 10}
)
issues = response.json()
注意
- 仓库名称不区分大小写,但API会保留大小写
- 每个仓库中的议题编号和拉取请求编号共享同一序列
- 创建/更新文件时,内容必须采用Base64编码
- 速率限制:认证用户每小时5000次请求,搜索每分钟30次
- 过于宽泛的搜索模式可能导致查询超时
- 部分接口要求特定的OAuth权限范围(例如,
read:org用于组织操作)。若收到权限范围错误,请通过support@maton.ai联系Maton技术支持,并说明您需要的具体操作/API及使用场景 - 重要提示:使用curl命令时,若URL包含方括号,请使用
curl -g以禁用通配符解析 - 重要提示:将curl输出通过管道传输至
jq或其他命令时,部分Shell环境中可能无法正确展开$MATON_API_KEY等环境变量
错误处理
| 状态码 | 含义 |
|---|---|
| 400 | 缺少GitHub连接 |
| 401 | Maton API密钥无效或缺失 |
| 403 | 禁止访问 - 权限或范围不足 |
| 404 | 资源未找到 |
| 408 | 请求超时(常见于复杂搜索) |
| 422 | 验证失败 |
| 429 | 请求频率受限 |
| 4xx/5xx | 来自GitHub API的透传错误 |
故障排除:API密钥问题
- 检查
MATON_API_KEY环境变量是否已设置:
echo $MATON_API_KEY
- 通过列出连接验证API密钥是否有效:
python <<'EOF'
import urllib.request, os, json
req = urllib.request.Request('https://ctrl.maton.ai/connections')
req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}')
print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2))
EOF
故障排除:无效的应用名称
- 确保您的URL路径以
github开头。例如:
- 正确:
https://gateway.maton.ai/github/user - 错误:
https://gateway.maton.ai/api.github.com/user


微信扫一扫,打赏作者吧~