0

I'm trying to login github using python requests. In the login page, I find I need the following data to post to server.

       commit: Sign in
       authenticity_token:    3YV9FG87QiYCga1Ue3IGMRSh88uDWBBhVW2Plwd22uDe+CBPbK/n+I+TC0ozTBsvk7QEPjNdFLvCil/9mdMr5A==    ga_id: 642223273.1583769258
       login: XXX
       password: XXX
       webauthn-support: supported
       webauthn-iuvpaa-support: unsupported
       return_to: 
       required_field_57c4: 
       timestamp: 1583770320678
       timestamp_secret:    fdc0b523bcd384d633c714f78fc8fd5d909dde3a05c0501345f358af2890fdec

I can get 'ga_id' from login page using chrome, but when I use python request to get login page.html, I found it's content is blank.

The following data was getting by chrome:

meta name="octolytics-dimension-ga_id" content="740927323.1583770634" class="js-octo-ga-id"

while when I use python requests, I got data like this, but the content is blank.

meta name="octolytics-dimension-ga_id" content="" class="js-octo-ga-id"

import requests
import re

def login():

    headers = {
            'Referer': 'https://github.com/',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
            'Host': 'github.com',
    }

    url1 = "https://github.com/login"
    response = session.get(url1, headers=headers).content
    print(response)

When I print response, I found the "octolytics-dimension-ga_id" content is blank. Why?

3
  • 2
    Instead of logging in from the web page, did you check Github REST API? developer.github.com/v3 Commented Mar 10, 2020 at 9:39
  • You really should use the API as suggested by ionpoint. Commented Mar 10, 2020 at 10:22
  • Thanks very much, I got it. Commented Mar 10, 2020 at 14:30

1 Answer 1

2

In case if you would like to know how to login using your code, so here is the correct way. But be informed that GitHub is already offering API which you can use for the login.

import requests
from bs4 import BeautifulSoup
import re


def Main(url):
    with requests.Session() as req:
        r = req.get(url)
        soup = BeautifulSoup(r.text, 'html.parser')
        token = soup.find("input", {'data-csrf': 'true'}).get("value")
        timest = soup.find("input", {'name': 'timestamp'}).get("value")
        timestsc = soup.find(
            "input", {'name': 'timestamp_secret'}).get("value")
        rq = soup.find(
            "input", {'type': 'text', 'name': re.compile("^required_field")}).get("name")
        data = {
            'commit': 'Sign+in',
            'authenticity_token': token,
            'login': '[email protected]',
            'password': 'lalala',
            'webauthn-support': 'supported',
            'webauthn-iuvpaa-support': 'unsupported',
            'return_to': '',
            rq: '',
            'timestamp': timest,
            'timestamp_secret': timestsc
        }
        r = req.post(url, data=data)
        print(r.text)


Main("https://github.com/session")

Since we haven't included valid user/pass so if you checked the output. you will find the following which confirm we are on the correct way.

Output:

Incorrect username or password.
Sign up to request clarification or add additional context in comments.

Comments

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.