因此,查看文檔後,我是如何設置我的GET和POST請求。我首先必須對我們的身份驗證執行POST以取回持有者令牌。一旦我有了這個令牌,我創建了一個局部變量來存儲令牌。然後做出我想要的任何GET請求並傳入令牌。以下是顯然你會填寫自己的用戶名,密碼,網址和令牌的方式,但你明白了。
POST請求 - 讓我的承載令牌
#import requests library for making REST calls
import requests
import json
#specify url
url = 'YOUR URL GOES HERE'
#Create payload that will be passed to API for authentication
payload = {
#This will be the data that you need to POST to the REST API.
In my example, in order to get back a web token, I just needed username and password.
"username": "USERNAME",
"password": "PASSWORD"
}
#Fill in headers
headers = {'content-type': 'application/json'}
#Call REST API
response = requests.post(url, data=json.dumps(payload), headers=headers)
#Print Response
print(response.text)
GET請求 - 要找回數據,而使用承載令牌從上面
#import requests library for making REST calls
import requests
#specify url
url = 'YOUR URL GOES HERE'
token = "THIS WAS WHERE I STORED MY TOKEN"
headers = {'Authorization': 'Bearer ' + token}
response = requests.get(url, headers=headers)
#Print Response
print (response.text)
來源
2015-10-12 18:19:17
Tim
如果您使用像Flask這樣的框架,您將節省很多麻煩。請求也捆綁在其中。看看:http://flask.pocoo.org/ – idjaw