Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

github.GithubException.GithubException: 401 {'message': 'Requires authentication', #1119

Open
sk193 opened this issue May 15, 2019 · 14 comments

Comments

@sk193
Copy link

sk193 commented May 15, 2019

Hello PyGithub Community - First off, thank you for supporting this tool you folks are the best!

Issue: I am attempting to authenticate to github enterprise using client_id and client_secret. I need to leverage PyGithub to post issues. Please let me know if there is a better route I should take, below are the errors I am hitting.

Code:

from github import Github

client_id=<xyz>
client_secret=<abc>
g = Github(base_url="https://<enterprise>/api/v3", client_id=client_id, client_secret=client_secret)

for repo in g.get_user().get_repos():
    print(repo.name)

repo = g.get_repo("<repo>")
repo.create_issue(title="This is a new issue", body="This is the issue body" 

Error:

Traceback (most recent call last):
    File "script.py", line 7, in <module>
      for repo in g.get_user().get_repos():
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/github/PaginatedList.py", line 64, in __iter__
      newElements = self._grow()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/github/PaginatedList.py", line 76, in _grow
      newElements = self._fetchNextPage()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/github/PaginatedList.py", line 201, in _fetchNextPage
      headers=self.__headers
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/github/Requester.py", line 275, in requestJsonAndCheck
      return self.__check(*self.requestJson(verb, url, parameters, headers, input, self.__customConnection(url)))
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/github/Requester.py", line 286, in __check
      raise self.__createException(status, responseHeaders, output)
  github.GithubException.GithubException: 401 {'message': 'Requires authentication', 'documentation_url': 'https://developer.github.com/enterprise/2.16/v3/repos/#list-your-repositories'}

Thanks again for all the help!

@stale
Copy link

stale bot commented Jul 14, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 14, 2019
@stale stale bot closed this as completed Jul 21, 2019
@mudit-mhjn
Copy link

Hey, I'm facing the same issue. Did you resolve it or any method you used to get around it?

@kbenzle
Copy link

kbenzle commented Mar 3, 2021

Hey, I'm facing the same issue. Did you resolve it or any method you used to get around it?

LOL, from 2019 to 4 days ago :) I'm having the same problem. You find anything?

@scottdangelo
Copy link

I'm having a similar issue. My scripts used to work, now I get this 401:

    headers, data = self.__requester.requestJsonAndCheck(
  File "/usr/local/lib/python3.9/site-packages/github/Requester.py", line 315, in requestJsonAndCheck
    return self.__check(
  File "/usr/local/lib/python3.9/site-packages/github/Requester.py", line 340, in __check
    raise self.__createException(status, responseHeaders, output)
github.GithubException.GithubException: 401 {"message": "Must authenticate to access this API.", "documentation_url": "https://developer.github.com/enterprise/2.21/v3"}

@scottdangelo
Copy link

@kbenzle @mudit-mhjn FYI, I opened a new issue:
#1865

@sekmani123
Copy link

same problem here

@iwangliguo
Copy link

I have the same problem now, is there any solutions? thanks.

@varchasa
Copy link

I have the same problem. Any solutions ?

@satyam-nykaa
Copy link

any solution ?

@iwangliguo
Copy link

iwangliguo commented Nov 3, 2021 via email

@MengFanjun020906
Copy link

MengFanjun020906 commented Mar 9, 2024

any solution?It is 2024 now😭

@sk193
Copy link
Author

sk193 commented Mar 9, 2024

Hello everyone, sorry I thought I replied to this post. I was able to fix this issue by doing the following.

Basically you need to generate access token and pass that through (look at what I am doing for rez and g in this sample code). Once authenticated you should be able to make a call to create an issue. This token will be alive for 10mins but you can alter that if needed.

Sorry it took 5 years to post but hope this helps!

#! /usr/bin/python3.7

import jwt 
import time
import requests
import json
from github import Github

keyFile = open("./123.pem", "r")
keyFileLines = keyFile.readlines()

keyString = ''.join(keyFileLines)

#print(keyString)

payload = {
    # issued at time
    'iat': time.time(),
    # JWT expiration time (10 minute maximum)
    'exp': int(time.time()) + (10 * 60),
    # GitHub App's identifier
    'iss': '14'
}

encodedJWT = jwt.encode(payload, keyString, algorithm="RS256")

header = {"Authorization": "Bearer {}".format(encodedJWT.decode()),
               "Accept": "application/vnd.github.machine-man-preview+json"}
    
#resp = requests.get('https://api.git.<domain>.net/app', headers=header)

#print(resp.status_code)
#print(resp.content.decode())
#print (encodedJWT)
###END AUTH###

rez = requests.post('https://git.<domain>.net/api/v3/installations/29/access_tokens', headers=header)
response = rez.json()
token = response['token']

g = Github(base_url="https://git.<domain>.net/api/v3", login_or_token=token)

repo = g.get_repo("Sysops/sk193-dev")
repo.create_issue(title="This is a new issue", body="Brah")

@EnricoMi
Copy link
Collaborator

EnricoMi commented Mar 9, 2024

Are you trying to authenticate an app installation as the user? The current way to do this is show in this example.

However, looks like this does not support Github Enterprise: #2766, #2485.
Good news: a fix is available: #2780

Can you please test if #2780 works for you?

@EnricoMi EnricoMi reopened this Mar 9, 2024
@stale stale bot removed the stale label Mar 9, 2024
@MengFanjun020906
Copy link

I will try, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants