2016-01-22 56 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)