def get_installation_token(installation):
"""
Get access token for installation
"""
now = datetime.datetime.now().timestamp()
if installation_token_expiry[installation] is None or now + 60 > installation_token_expiry[installation]:
# FIXME: if .netrc file is present, Authorization header will get
# overwritten, so need to figure out how to ignore that file.
if netrc_exists():
raise Exception("Authentication does not work properly if a ~/.netrc "
"file exists. Rename that file temporarily and try again.")
headers = {}
headers['Authorization'] = 'Bearer {0}'.format(get_json_web_token())
headers['Accept'] = 'application/vnd.github.machine-man-preview+json'
url = 'https://api.github.com/installations/{0}/access_tokens'.format(installation)
req = requests.post(url, headers=headers)
resp = req.json()
if not req.ok:
if 'message' in resp:
raise Exception(resp['message'])
else:
raise Exception("An error occurred when requesting token")
installation_token[installation] = resp['token']
installation_token_expiry[installation] = dateutil.parser.parse(resp['expires_at']).timestamp()
return installation_token[installation]
评论列表
文章目录