0

NOTE: this is NOT a duplicate. This is the second time I had to post this because it was thought to be a duplicate but the other responses don't solve my particular issue. After correcting my original/previous post to reflect this, it still remained closed. So, here I am asking the same question again since the other one was closed immediately. Please read fully and try to understand my issue before marking it a duplicate or closed.

I am attempting to login to a site whose URL is "https://wedgfl.aquahawk.us/login" but having a hard time ever getting in. I've been using the following code to attempt this but when printing the page content, all I get back is the page without it actually being logged in.

#!/usr/bin/env python3
import requests

with requests.Session() as c:
    url = 'https://wedgfl.aquahawk.us/login'
    USERNAME = 'myemail@domain.tld'
    PASSWORD = 'MySecretPassword'
    c.get(url)
    #cookie = c.cookies['connect.sid']
    login_data = dict(username=USERNAME, password=PASSWORD, next='/')
    c.post(url, data=login_data, headers={"Referer": "https://wedgfl.aquahawk.us/login"})
    page = c.get('https://wedgfl.aquahawk.us/')
    print(page.content)

Once I am logged in successfully, the landing page should be "https://wedgfl.aquahawk.us" as you can see from my page = c.get('https://wedgfl.aquahawk.us/') part of my code. I thought maybe it was something with my cookie but even disabling/commenting it out has the same effect. Just in case you are wondering, I am indeed using Python 3

I have indeed pulled the form data I need from the page source. Perhaps my issue is that I am not calling the right key info from the form data? Here is what the info is when I view the page source after logging in:

<form id="userAuth" action="/login" method="POST" style="display: none">
  <div id="userAuthUserNameSubtitle" class="x-hidden">User Name or Email:</div>
  <input id="userAuthUserName" type="text" name="username" spellcheck="false" class="x-hidden">
  <div id="userAuthPasswordSubtitle" class="x-hidden">Password:</div>
  <input id="userAuthPassword" type="password" name="password" class="x-hidden">
  <input id="userAuthSubmit" type="submit" value="Sign In" class="x-hidden">
</form>

I've tried it this way as well just to verify if I am actually logging in but still have the same result:

import requests

payload = {
    'username': 'myemail@domain.tld',
    'password': 'MySecretPassword'
}

import sys

with requests.Session() as c:
    c.post('https://wedgfl.aquahawk.us/login', data=payload)
    r = c.get('https://wedgfl.aquahawk.us/')
    print "Something from the page that only shows after login" in r.content

I even followed the example from this post and still get the same result:

import requests

url = 'https://wedgfl.aquahawk.us/login'
values = {'username': 'myemail@domain.tld',
        'password': 'MySecretPassword'}

r = requests.post(url, data=values)
print(r.content)

I am either grabbing the wrong value keys from the form data or maybe I need more info in my code from the form data that this page requires in order to login?

Lukasz
  • 684
  • 6
  • 20
  • There is a proper process for getting questions reopened if you believe they were mistakenly closed. Reposting the question is not part of that process. Please see [this Meta post](https://meta.stackexchange.com/questions/36415/how-do-you-reopen-a-closed-question). – glibdud Dec 15 '17 at 18:19

1 Answers1

1

Check this solution, I have added the proper headers

import requests
import json

session = requests.Session()

loginUrl = "https://wedgfl.aquahawk.us/login"
loginHeaders = {
        "Host": "wedgfl.aquahawk.us",
        "Connection: keep-alive",
        "Accept": "application/json",
        "Origin": "https://wedgfl.aquahawk.us",
        "X-Requested-With": "XMLHttpRequest",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "Referer": "https://wedgfl.aquahawk.us/login",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "en-US,en;q=0.9",
}

loginData = {
    "username":"myemail@domain.tld",
    "password":"MySecretPassword"

}

#Data is posted as Json Data using json.dumps
loginResponse = session.post(loginUrl, data=json.dumps(loginData), headers=loginHeaders)
#If the above does not work try without json.dumps
loginResponse = session.post(loginUrl, data=loginData, headers=loginHeaders)
print(loginResponse)   #should print 200

#check if logged in 

verify = session.get("https://wedgfl.aquahawk.us/")
print(verify.text)
Stack
  • 2,660
  • 3
  • 14
  • 21
  • I get the same results. I think I know what the issue is. Once I am logged in, it seems to render some type of flash looking page or some animation format for the entire page, which probably can't be read doing it this way so it always just spits back the main page's format...sigh :(. Your answer does work because I tried it on a different page. Before I couldn't even get my code to work on a different page. So thanks for helping out! Why would the json format work and not the way I was doing it? – Tito Valentin Dec 16 '17 at 14:36
  • Because the website server is configured to accept data in json format as you can see in the headers : `Accept : application/json`, you can use chrome network tab to see the request, check the headers sent by the browser to the server. – Stack Dec 16 '17 at 18:54