2016-01-22 57 views
0

我已经死了:)oauth2 without api key

目标是登录到使用OAuth2的网站。但是我需要运行的部分没有与之关联的API。所以我需要登录,只需使用用户名和密码,然后导航到相关页面并进行屏幕抓取以获取我的数据。

我敢肯定,问题不在于它坐在这个键盘上的网站。但我搜索的例子,并尝试了一大堆的猜测,但没有任何工作

帮助将被感激地接受。

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'] 
+0

使用用户名和密码,您可以像登录人一样使用登录表单登录。 – furas

+0

但是,如何将授权连接到下一个页面请求(我显然在这里丢失)。我在Python中,所以没有运行浏览器。 – cryptoref

+0

使用'request'(使用'requests.Session()')来完成一切 - 获取登录表单,POST你的登录名和密码,获取下一页,获取数据。但是您的脚本必须像浏览器一样运行 - 所以请使用Chrome中的“开发人员工具”或Firefox中的“Firebug”来查看从浏览器发送到服务器以及从服务器发送到浏览器的数据。 – furas

回答

1

像这么多的东西,它只是一个用户错误

代码让我的网​​页就是这么简单。下面的代码可以做到这一点。感谢Furas的指针。

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)