简体   繁体   中英

Using Python Requests to Create a Token for GitHub Enterprise

I am trying to create a token for my script so that it has access to the private repo on my GitHub Enterprise account. I am currently trying this:

username = 'my_username'
password = getpass.getpass('GitHub Password: ')

payload = {
    'note': 'info about token'
}
url = 'https://github.my_company_name.com/api/v3/authorizations'
r = requests.post(url, auth=(username, password), data=payload)

but I get this error:

requests.exceptions.ConnectionError: ('Connected aborted.', error(10061, 'No connection could be made because the target machine actively refused it'))

Any help would be greatly appreciate, thanks!

import requests
from bs4 import BeautifulSoup
URL="https://github.com/session"

# Define User Agent
headers={"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36"}

username="username"
password="password"

# Create session to crawl authenticated sessions
s=requests.Session()

#Update User Agent in the session created
s.headers.update(headers)

#Get URL using the session created
r=s.get(URL)

#Parse the webspage to find the 'authenticity_token'

soup=BeautifulSoup(r.content)
authenticity_token= soup.find("input",{"name":"authenticity_token"})['value']

# Create Login Data
login_data={"authenticity_token":authenticity_token,
"login":username,
"password":password,
"commit":"Sign in"}

#Now login
r=s.post(URL, data=login_data)

#Test it
soup=BeautifulSoup(r.content)
print soup.find(class_="css-truncate-target").text #This should print your username

You can do all that you want once logged in :-)

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM