如何在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: %sn' "$github_repo"
else
printf 'Could not create the repository: %sn' "$github_repo" >&2
printf 'The GitHub API replied with this JSON:n%sn' "$json_reply" >&2
fi

关于一个特色实现示例,请参阅我的回答:https://stackoverflow.com/a/57634322/7939871

最新更新