Warm tip: This article is reproduced from serverfault.com, please click

Bad Credentials for Github API

发布于 2014-12-27 17:08:37

I have the following script, which I am trying to test out in bash, using curl to do a couple things, one is to create a new repo, the second - which is not implemented yet - is to get the git_url from the json thats returned, which I'm not sure if my parse_json function will let me do that and then finally to push a sample commit message to that repo.

the script is as follows:

#!/usr/bin/env bash

set -eux

# Json Function: parse_json 'json string' key
function parse_json()
{
    echo $1 | sed -e 's/[{}]/''/g' | awk -F=':' -v RS=',' "\$1~/\"$2\"/ {print}" | sed -e "s/\"$2\"://" | tr -d "\n\t" | sed -e 's/\\"/"/g' | sed -e 's/\\\\/\\/g' | sed -e 's/^[ \t]*//g' | sed -e 's/^"//'  -e 's/"$//'
}

git_create_repo() {
    read -e -p  "Please enter your API Key: " apiKey
    read -e -p  "Repo Name: " repoName
    read -e -p  "Repo Description: " repoDescription

    # Use the API to create a a repository
    response=$(curl -i -H 'Authorization: token $apiKey' \
        -d '{ \
                "name": "$repoName", \
                "description": "$repoDescription", \
                "private": false, \
                "license_template": "mit" \
            }' \
        https://api.github.com/AdamKyle/repos)

    echo $response
}

git_create_repo

When I go through all the steps I get:

{
  "message": "Bad credentials",
  "documentation_url": "https://developer.github.com/v3"
}

I am wondering if its because of the way I am putting in my api key with: curl -i -H 'Authorization: token $apiKey ...' I have tried "$apiKey" but even that doesn't work.

Ideas?

Questioner
TheWebs
Viewed
0
Lajos Veres 2014-12-28 02:33:07

I use it this way:

curl -X 'POST' -u $MY_AUTH https://api.github.com/...

where $MY_AUTH was generated in github website. It looks like:

export MY_AUTH="...hash...:x-oauth-basic"