cryptoref cryptoref - 16 days ago 6
Python Question

oauth2 without api key

I'm dead lost :)

Goal is to logon to a web site that uses OAuth2. However the section I need to run, doesn't have an API associated with it. So I need to login, just using the username and password, and then navigate to the page in question and do a screen scrape to get my data.

I'm sure the problem isn't at the web site it's sitting at this keyboard. But i've searched for examples and tried a whole bunch of guesses, but nothing is working

Help would be gratefully accepted.

import sys
import requests
import oauth2 as oauth

r = requests.get(logon_url)
consumer = oauth.Consumer(key=user, secret=password)
client = oauth.Client(consumer)
resp, content = client.request(r.url, "GET")
token_url = resp['content-location']

# At this point i'm lost i'm just guessing on the rest
# the next doesn't give an error but i'm sure it's wrong
resp2, content2 = client.request(token_url, 'GET')

# save the cookie, i do have a cookie but not sure what i have
auth_token = resp['set-cookie']

Answer

Like so many things, it's just a user error

code to get me to the page is so simple. And the following code does the trick. Thanks to Furas for the pointer.

    with requests.session() as s1:
         # get login form
         r = s1.get(logon_url)
         # post the username and password
         resp = s1.post(r.url,data=payload)
         # get the admin page
         resp2 = s1.get(page_url)