Bad Credentials for Github API

本小妞迷上赌 提交于 2020-12-06 06:37:27

问题


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?


回答1:


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"



回答2:


I was also facing similar issue.
Strangely using double-quotes instead of single-quotes for the headers solved the issue for me. Like below :

curl -i -H "Authorization: token $apiKey" ....


来源:https://stackoverflow.com/questions/27669504/bad-credentials-for-github-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!