python3.x - python連oanda的模擬交易api時(shí)現(xiàn)HTTP Error 401: UNAUTHORIZED?
問題描述
用python3連oanda的模擬交易api時(shí)現(xiàn)HTTP Error 401: UNAUTHORIZED,新手求教:import urllib.parseimport urllib.request
url = ’https://api-fxpractice.oanda....’access_token = ’a554db3a48ac8180a6996a5547ba1663-ac5947e64456cc5842a34f4ce05e4380’account_id = ’cawa11’values = {’accountId’:account_id}headers = {’Authorization’:’Bearer’+access_token}
data = urllib.parse.urlencode(values).encode('utf-8')
req = urllib.request.Request(url, data, headers)response = urllib.request.urlopen(req)the_page = response.read()
報(bào)錯(cuò):Traceback (most recent call last): File 'C:UserslenovoDesktopbb.py', line 36, in <module>
response = urllib.request.urlopen(req)
File 'C:Python34liburllibrequest.py', line 161, in urlopen
return opener.open(url, data, timeout)
File 'C:Python34liburllibrequest.py', line 469, in open
response = meth(req, response)
File 'C:Python34liburllibrequest.py', line 579, in http_response
’http’, request, response, code, msg, hdrs)
File 'C:Python34liburllibrequest.py', line 507, in error
return self._call_chain(*args)
File 'C:Python34liburllibrequest.py', line 441, in _call_chain
result = func(*args)
File 'C:Python34liburllibrequest.py', line 587, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: UNAUTHORIZED
問題解答
回答1:#Bearer后面少了一個(gè)空格headers = {’Authorization’:’Bearer ’+access_token}回答2:
問題已解決,謝謝各位關(guān)注:import requestsimport json instruments = ’EUR_USD’account_id = ’cawa11’access_token = ’a554db3a48ac8180a6996a5547ba1663-ac5947e64456cc5842a34f4ce05e4380’params = {’instruments’:instruments,’accountId’:account_id} headers = {’Authorization’:’Bearer ’+access_token}r = requests.get('https://api-fxpractice.oanda.com/v1/prices',headers = headers, params=params) price = r.json()print(price’prices’[’instrument’].replace(’_’,’/’),’:’,round((price’prices’[’ask’]+price’prices’[’bid’])/2,4))輸出:EUR/USD : 1.0905
相關(guān)文章:
1. css3 - 沒明白盒子的height隨width的變化這段css是怎樣實(shí)現(xiàn)的?2. java - 根據(jù)月份查詢多個(gè)表里的內(nèi)容怎么實(shí)現(xiàn)好?3. python3.x - c++調(diào)用python34. javascript - 在 model里定義的 引用表模型時(shí),model為undefined。5. php工具中的mysql還是5.1以下的,請(qǐng)問如何才能升級(jí)到5.1以上?6. css3 - 這個(gè)右下角折角用css怎么畫出來?7. atom開始輸入!然后按tab只有空格出現(xiàn)沒有html格式出現(xiàn)8. javascript - 移動(dòng)端自適應(yīng)9. android - 課程表點(diǎn)擊后浮動(dòng)后邊透明可以左右滑動(dòng)的界面是什么?10. apache - 想把之前寫的單機(jī)版 windows 軟件改成網(wǎng)絡(luò)版,讓每個(gè)用戶可以注冊(cè)并登錄。類似 qq 的登陸,怎么架設(shè)服務(wù)器呢?
