bash - 如何使用个人访问令牌而不是使用 shell 脚本的密码来访问 Github API
问题描述
最近好吗?
因此,我使用 bash 脚本创建了一个远程存储库,使用密码访问这样的端点:
NEWVAR="{\"name\":\"$githubrepo\",\"private\":\"true\"}"
curl -u $USERNAME https://api.github.com/user/repos -d "$NEWVAR"
但是,GitHub 将不再允许开发人员使用密码访问端点。所以我的问题是如何使用个人访问令牌创建远程存储库?
解决方案
用于--header
传输授权:
#!/usr/bin/env sh
github_user='The GitHub user name'
github_repo='The repository name'
github_oauth_token='The GitHub API auth token'
# Create the JSON data payload arguments needed to create
# a GitHub repository.
json_data="$(
jq \
--null-input \
--compact-output \
--arg name "$github_repo" \
'{$name, "private":true}'
)"
if json_reply="$(
curl \
--fail \
--request POST \
--header 'Accept: application/vnd.github.v3+json' \
--header "Authorization: token $github_oauth_token" \
--header 'Content-Type: application/json' \
--data "$json_data" \
'https://api.github.com/user/repos'
)"; then
# Save the JSON answer of the repository creation
printf '%s' "$json_reply" >"$github_repo.json"
printf 'Successfully created the repository: %s\n' "$github_repo"
else
printf 'Could not create the repository: %s\n' "$github_repo" >&2
printf 'The GitHub API replied with this JSON:\n%s\n' "$json_reply" >&2
fi
在此处查看我的答案以获取特色实现示例: https ://stackoverflow.com/a/57634322/7939871
推荐阅读
- magento - Magento 1.9 订单评论添加做后端信息
- java - 我正在尝试在单独的屏幕上打印收据
- javascript - 使用 JQuery、HTML 和 CSS 同时为每个框单独生成随机颜色
- sql - 从sql中的开始日期列表中获取结束日期
- amazon-dynamodb - 如何在仅更新特定字段的发电机数据库中进行批量更新
- javascript - LocalStack - CloudFormation 返回错误代码:500
- azure - 如何可靠地将事件存储到 Azure CosmosDB 并仅一次分派到事件网格
- php - 使用 mongo 中的嵌套元素匹配更新值
- php - PHP 7.2.9 在随机时间内存不足
- math - 用二进制表示十进制数