0

I been trying to generate a Token for self-hosted machines using the GitHub-API for my Org.

Using Curl it works as expected from GitHubs doc

curl -L \
  -X POST \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer <MY-TOKEN>" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/orgs/<MY-ORG>/actions/runners/registration-token

response

{
  "token": "RUNNER_TOKEN",
  "expires_at": "2023-09-01T23:47:56.042+02:00"
}

Then I tried to re-create this is Python, using the requests lib

def get_runner_token(token, org):
    url = f"https://api.github.com/orgs/{org}/actions/runners/registration-token"
    authorization = f'Bearer {token}' # Tried both as Bearer and token
    headers = CaseInsensitiveDict() # Tried as normal dict as well
    headers['Accept'] = "application/vnd.github+json" # Tried removing accept and API version
    headers['Authorization'] = authorization
    headers['X-GitHub-Api-Version'] = "2022-11-28"
    payload = {} #Tried with and without payload...
    print("Sending request to", url, "with headers", headers)
    x = requests.post(url, headers=headers, data=payload)
    print(x, x.json())

No matter what I do I keep getting the response

<Response [401]> {'message': 'Bad credentials', 'documentation_url': 'https://docs.github.com/rest'}

If don't send the token as an arg through a function like so:

def get_runner_token(org):
    token = "..."
    # Same as code above....

It works? I don't understand Can anybody see what I might be doing wrong?

While very similar to Github API works in curl but not python - bad credentials. None of the answers worked for me

3
  • 1
    Your code works for me as written (specifically, this code, with org and token filled out with appropriate values). Commented Sep 1, 2023 at 23:55
  • @larsks Okay, if I do just what you did it also work. But if I do what I actually doing (sending them in as args to a function). It does not work? So looks like the error is not where I thought it was. I'll update my question with the more correct code (question updated) Commented Sep 2, 2023 at 6:04
  • 1
    @larsks After even more attempts to figure our what went wrong. It seems that some extra (invisible) characters got added when I copied the token to the (windows) terminal when calling the python function. So the token was wrong. Was not until i compared the one copied in code and the one sent from the terminal that I noticed it. So thank you for indirectly solving my problem Commented Sep 2, 2023 at 6:15

0

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.