我試圖從API(oanda)獲取簡單的JSON貨幣匯率響應。
但接收各種錯誤代碼,如'無效語法'。python中curl請求的問題
這是我更新的代碼:
import requests
import json
from optparse import OptionParser
def connect_to_stream():
"""
Environment <Domain>
fxTrade stream-fxtrade.oanda.com
fxTrade Practice stream-fxpractice.oanda.com
sandbox stream-sandbox.oanda.com
"""
# Replace the following variables with your personal ones
domain = 'stream-fxpractice.oanda.com'
access_token = 'xxxxxxxxxxxxxxxx'
account_id = 'xxxxxxxxx'
instruments = "EUR_USD"
try:
s = requests.Session()
url = "https://" + domain + "/v1/prices"
headers = {'Authorization' : 'Bearer ' + access_token,
# 'X-Accept-Datetime-Format' : 'unix'
}
params = {'instruments' : instruments, 'accountId' : account_id}
req = requests.Request('GET', url, headers = headers, params = params)
pre = req.prepare()
resp = s.send(pre, stream = True, verify = False)
return resp
except Exception as e:
s.close()
print "Caught exception when connecting to stream\n" + str(e)
response = urllib2.urlopen("https://api-fxpractice.oanda.com/v1/prices?instruments=EUR_USD")
data = json.load(response)
print data
對不起,我編輯的代碼,並留出了錯誤信息。我是,然而,能夠通過這裏https://github.com/oanda/oandapy使用OANDA的Python包裝來解決這個問題。
也許你也可以分享你得到的錯誤? – pagid
不要嗚咽。避免不必要的背景。另外,您應該添加您收到的確切錯誤消息。 –
@Nathaniel Ford感謝哥們指出我過度的wh,聲,沒有意識到我是如此煩人。我會嘗試發佈我以前的錯誤,以及.. – MacD