Maison  >  Questions et réponses  >  le corps du texte

python3.x - Erreur HTTP 401 : UNAUTHORIZED apparaît lorsque python se connecte à l'API de trading simulé d'oanda ?

用python3连oanda的模拟交易api时现Erreur HTTP 401 : UNAUTHORIZED,新手求教:
import urllib.parse
import urllib.request

url = 'https://api-fxpractice.oanda...' accountId':account_id}
headers = {'Autorisation' : 'Porteur'+access_token}

data = urllib.parse.urlencode(values).encode("utf-8")

req = urllib.request.Request(url, data, headers)

response = urllib.request.urlopen(req)

the_page = réponse.read()


报错:
Traceback (dernier appel le plus récent) :

Fichier "C:UserslenovoDesktopbb.py", ligne 36, dans <module>

response = urllib.request.urlopen(req)

Fichier "C:Python34liburlllibrequest.py", ligne 161, dans urlopen
return opener.open(url, data, timeout)

Fichier "C:Python34liburlllibrequest.py", ligne 469, en open

response = meth(req, response)

Fichier "C:Python34liburlllibrequest.py", ligne 579, dans http_response

'http', request, response, code, msg, hdrs)

Fichier "C:Python34liburlllibrequest.py", ligne 507, en erreur

return self._call_chain(*args)

Fichier "C:Python34liburlllibrequest.py", ligne 441, dans _call_chain

result = func(*args)

Fichier "C:Python34liburlllibrequest.py", ligne 587, dans http_error_default

raise HTTPError(req.full_url, code, msg, hdrs, fp)

urllib.error.HTTPError : Erreur HTTP 401 : NON AUTORISÉ

漂亮男人漂亮男人2712 Il y a quelques jours796

répondre à tous(2)je répondrai

  • 某草草

    某草草2017-05-18 10:58:04

    #Bearer后面少了一个空格
    headers = {'Authorization':'Bearer '+access_token}
    

    répondre
    0
  • PHP中文网

    PHP中文网2017-05-18 10:58:04

    问题已解决,谢谢各位关注:
    import request
    import json
    instruments = 'EUR_USD'
    account_id = 'cawa11'
    access_token = 'a554db3a48ac8180a6996a5547ba1663 -ac5947e64456cc5842a34f4ce05e4380'
    params = {'instruments':instruments,'accountId':account_id}
    headers = {'Autorisation':'Bearer'+access_token}
    r = requêtes.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))
    Valeur:EUR/USD : 1.0905

    répondre
    0
  • Annulerrépondre