python3.x - python连oanda的模拟交易api时现HTTP Error 401: UNAUTHORIZED?
问题描述
用python3连oanda的模拟交易api时现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()
报错: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后面少了一个空格headers = {’Authorization’:’Bearer ’+access_token}回答2:
问题已解决,谢谢各位关注: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
相关文章:
1. objective-c - 从朋友圈跳到我的APP 如何实现?2. java - PHP开发微信无法获取到signature,timestamp,nonce3. 请教一个python字符串处理的问题?4. HTML5禁止img预览该怎么解决?5. 怎么可以实现在手机浏览器看到链接的title属性,就是鼠标放上去会有一个tip效果的6. 如何分别在Windows下用Winform项模板+C#,在MacOSX下用Cocos Application项目模板+Objective-C实现一个制作游戏的空的黑窗口?7. 网页爬虫 - python爬虫用BeautifulSoup爬取<s>元素并写入字典,但某些div下没有这一元素,导致自动写入下一条,如何解决?8. html5 - h5+中webview的show方法有延迟9. javascript - vscode alt+shift+f 格式化js代码,通不过eslint的代码风格检查怎么办。。。10. javascript - html 中select如何修改样式,鼠标悬浮时改变option样式,有没有插件啊
