2016-07-28 538 views
1

這是我第一次使用Facebook憑證登錄。我希望能夠通過我的賬戶查詢Airbnb中的房源。我在Airbnb的原始帳戶是通過Facebook登錄。以下是airbnb頁面上的示例請求:http://airbnbapi.org/#login-by-facebook使用Airbnb登錄或API登錄

我不知道在哪裏可以得到我的client_id和Facebook的訪問令牌。雖然它指向https://developers.facebook.com/docs/facebook-login/access-tokens以獲取用戶訪問令牌,但如果我理解正確,它需要我創建一個應用程序。我不確定我需要使用哪種認證流程才能使用Airbnb API。

我已經看過Airbnb文檔來搜索client_id,但沒用。

這是我到目前爲止有:

import requests 
import json 

API_URL = "https://api.airbnb.com" 
LISTING_ENDPOINT= "https://api.airbnb.com/v2/search_results" 

post_query = { 
    "client_id": "I HAVE NO IDEA WHERE TO GET IT", 
    "locale": "en-US", 
    "currency":"USD", 
    "assertion_type":"https://graph.facebook.com/me" 
    "assertion":"HOW SHOULD I GET THIS ONE?", 
    "prevent_account_creation":True 
} 

# I think this should be able to log me in and I should be able to query listings 
_ = requests.post(API_URL, post_query).json() 

query = { 
    "client_id":"FROM ABOVE", 
    "user_lat": "40.00", 
    "user_long":"-54.31" 
} 


listings = requests.get(LISTING_ENDPOINT, json=query).json() 
+0

似乎沒有公開的API,但訪問私有的似乎微不足道 –

+0

我如何得到我的客戶端ID? – mousecoder

回答

1

我碰到了同樣的問題來了,你。我終於明白了。我使用的工具是請求庫的高級功能,即Session(),用於保存Cookie。使用第三方帳戶登錄的重要部分是找到我們需要發佈Cookie的鏈接。以下是我的代碼。

import requests 
x=requests.Session() #savig the cookies when you click the "log in with facebook button" 
y=requests.Session() #saving the cookies for parsing the airbnb listing. 
account={'email':'your_facebook_account','pass':'your_facebook_ps'} 
log_in_with_facebook_click=x.post("https://www.airbnb.jp/oauth_connect?from=facebook_login&service=facebook") 

#all the cookies up to now is saved in "x" 
my_first_time_cookies=x.cookies.get_dict() 
real_login_link=log_in_with_facebook_click.url 

real_log_in=y.post(real_login_link,account,cookies=my_first_time_cookies) 
#the real login link is saved in "log_in_with_facebook" 
#pass the cookies and your facebook account information to the real login link 
#you should have logged into airbnb.For testing the log in, we do the following. We check the reservation data. 

from bs4 import BeautifulSoup 
page=1 
test=y.get("https://www.airbnb.jp/my_reservations?page="+str(page)) 
#Remember that the cookies we use to access airbnb website after loggin in is saved in "y" 
test_html=BeautifulSoup(test.text,'lxml') 
print(test_html.text) 
#you should have looked your tenants reservation information.