2

how is it going?

So I used a bash script to create a remote repository using a password to access a endpoint like this:

NEWVAR="{\"name\":\"$githubrepo\",\"private\":\"true\"}"
curl -u $USERNAME https://api.github.com/user/repos -d "$NEWVAR"

However, GitHub is going to not allow developers to access endpoints using passwords anymore. So my question is how do I create a remote repository using a personal access token?

5

1 Answer 1

4

use --header to transmit authorization:

#!/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

See my answer here for a featured implementation example: https://stackoverflow.com/a/57634322/7939871

Sign up to request clarification or add additional context in comments.

4 Comments

So I am a beginner programmer and I have never used API's before so I dont really understand this code. With this said, I got the error "curl: (22) The requested URL returned an error: 400 Bad Request". What should I do about this? I inserted all the data including my token and what i want my github repo to be called.
@SrikarKarra I can only help about your authentication question here. You did not mention what you intended to accomplish by calling the GitHub's API methods. To find out how to call methods and what parameters and JSON reply it gives. I direct you to docs.github.com/en/rest.
The main purpose of this script is to create a remote repo using a bash script. I originally used password authentication which worked perfectly, however, GitHub will soon be removing this feature. So I am trying to use a personal access token. Here is the repository github.com/Srikar-Karra/CTDIR. I am getting the 400 bad request error by using the code provided in the answer above. So could you think of some possible issues?
@SrikarKarra I just fixed the GitHub API url needed for the creation call. Also tested it actually works by creating a testrepository with my script above on my GitHub account. So it shall be a perfectly working base for you to expand on it. You need a GitHub API token with the repo scope to be able to create repository using this token.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.