'how to use github api token in python for requesting
I'm able to obtain Github api token in python using username and password but i'm not able to use that API-Token for requesting any POST/DELETE/PATCH.
How do we use Github API-Tokens for making any request. For eg, i have API-Token lets say 'hbnajkjanjknjknh23b2jk2kj2jnkl2...'
now for requesting
#i'm providing username and API-Token in headers like
self.header = {'X-Github-Username': self.username,
'X-Github-API-Token': self.api_token
}
#then requesting(post) to create a gist
r = requests.post(url, headers=headers)
But i'm always getting 401 error with Bad Crediantials message.
What's the proper way to use API-Tokens without inputting the password
Solution 1:[1]
Here's some code that might help you out.
Examples:
Example 1 (auth):
username = 'user'
token = 'token'
login = requests.get('https://api.github.com/search/repositories?q=github+api', auth=(username,token))
Example 2 (headers):
headers = {'Authorization': 'token ' + token}
login = requests.get('https://api.github.com/user', headers=headers)
print(login.json())
Example 3 (delete repo):
user = 'username'
repo = 'some_repo' # Delete this repo
headers = {'Authorization': 'token ' + token}
login = requests.delete('https://api.github.com/' + 'repos/' + user + '/' + repo, headers=headers)
Example 4 (create repo):
repo = 'some_repo'
description = 'Created with api'
payload = {'name': repo, 'description': description, 'auto_init': 'true'}
login = requests.post('https://api.github.com/' + 'user/repos', auth=(user,token), data=json.dumps(payload))
You might want to take a look at the following docs:
I hope this helps.
Solution 2:[2]
Using the requests python library, you need to inform the Github account API Token on the request Headers.
Example:
authorization = f'token {token}'
headers = {
"Accept": "application/vnd.github.v3+json",
"Authorization" : authorization,
}
Then you can call the resource you wish, example:
r = requests.post(
url=url,
data=json_data,
headers=headers
)
You can find more details on the Github API official documentation
Solution 3:[3]
I followed the API documentation and tried to set the Authorization header in my call to requests.get(). However, this always returned a 401 error. So I checked the headers from Response object's request attribute and found that the Authorization header from my request had been rewritten. After a little digging in the source code, I found that when the request calls for HTTP Basic Auth, the Authorization header gets rewritten (see here).
So, the solution was to pass the token in an auth tuple when calling requests.get(). For example:
requests.get('https://api.github.com/notifications', auth=(my_username, my_token))
Unfortunately, this answer (currently the highest-voted one) won't work, as the method from that answer is precisely what I was using before trying the solution above.
Solution 4:[4]
from github import Github
import requests
import pandas as pd
import datetime
token='.....................'
g=Github(token,per_page=10000)
repos=g.search_repositories(query="q:rgpd")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zeznzo |
| Solution 2 | user15164966 |
| Solution 3 | Erik |
| Solution 4 | ilboudo kader |
