Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用请求库将Curl转换为Python_Python_Curl_Python Requests - Fatal编程技术网

使用请求库将Curl转换为Python

使用请求库将Curl转换为Python,python,curl,python-requests,Python,Curl,Python Requests,我试图将Curl翻译成Python,但我出了点问题。请帮忙 卷曲: 我已经仔细检查了我的证件。我觉得代码有问题 我猜您缺少SSL证书验证和基本身份验证 您可以使用verify标志关闭SSL证书验证,并通过指定auth使用基本身份验证 from requests.auth import HTTPBasicAuth import requests import json def market_buy(): header = {"Accept": "application/json",

我试图将Curl翻译成Python,但我出了点问题。请帮忙

卷曲:


我已经仔细检查了我的证件。我觉得代码有问题

我猜您缺少
SSL证书验证
基本身份验证

您可以使用verify标志关闭SSL证书验证,并通过指定auth使用基本身份验证

from requests.auth import HTTPBasicAuth
import requests
import json

def market_buy():
    header = {"Accept": "application/json",
               "Authorization": "Bearer <my auth code>"
             }
    data = {
    "order": {
        "units": "100",
        "instrument": "EUR_USD",
        "timeInForce": "FOK",
        "type": "MARKET",
        "positionFill": "DEFAULT"
      }
    }
    url = "https://api-fxtrade.oanda.com/v3/accounts/<myaccount>/orders"
    r = requests.post(url, data=data, headers=header, auth=HTTPBasicAuth('admin', 'admin'), verify=False)    
    print(r.text)
market_buy()
from requests.auth导入HTTPBasicAuth
导入请求
导入json
def market_buy():
header={“Accept”:“application/json”,
“授权”:“持票人”
}
数据={
“命令”:{
“单位”:“100”,
“票据”:“欧元/美元”,
“时间信息”:“FOK”,
“类型”:“市场”,
“位置填充”:“默认值”
}
}
url=”https://api-fxtrade.oanda.com/v3/accounts//orders"
r=requests.post(url,data=data,headers=header,auth=HTTPBasicAuth('admin','admin'),verify=False)
打印(右文本)
市场(买)

我想上面的方法应该可以用。

从cURL到Python请求的直接转换如下:

from requests import post

headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer <AUTHENTICATION TOKEN>",
}

data = {
    "order": {
        "units": "100",
        "instrument": "EUR_USD",
        "timeInForce": "FOK",
        "type": "MARKET",
        "positionFill": "DEFAULT",
    }
}

post(
    "https://api-fxtrade.oanda.com/v3/accounts/<myaccount>/orders",
    headers=headers,
    data=data,
)
从请求导入post
标题={
“内容类型”:“应用程序/json”,
“授权”:“持票人”,
}
数据={
“命令”:{
“单位”:“100”,
“票据”:“欧元/美元”,
“时间信息”:“FOK”,
“类型”:“市场”,
“位置填充”:“默认值”,
}
}
职位(
"https://api-fxtrade.oanda.com/v3/accounts//orders",
标题=标题,
数据=数据,
)

您认为为什么需要身份验证?已包含授权标头。此外,如果您看到此错误
“授权不足,无法执行请求”。
它与授权相关,则该错误证明收到了响应,这意味着这不是SSL验证问题(在处理金融API时,您确实不应该禁用SSL验证),因此,它可能与SSL问题有关,也可能是站点在您尝试发送/接收请求时阻止了您。
verify=False
阻止客户端验证服务器证书。如果验证失败,您将不会收到任何类型的JSON响应-这将是在收到任何数据之前请求/urllib3级别的连接失败。我猜,只是猜测API的访问令牌已过期。在您的curl示例中,您设置了
内容类型
,但是在python中,您得到了
Accept
。它不应该影响授权,但您能让这两个匹配只是为了检查吗?
{"errorMessage":"Insufficient authorization to perform request."}
from requests.auth import HTTPBasicAuth
import requests
import json

def market_buy():
    header = {"Accept": "application/json",
               "Authorization": "Bearer <my auth code>"
             }
    data = {
    "order": {
        "units": "100",
        "instrument": "EUR_USD",
        "timeInForce": "FOK",
        "type": "MARKET",
        "positionFill": "DEFAULT"
      }
    }
    url = "https://api-fxtrade.oanda.com/v3/accounts/<myaccount>/orders"
    r = requests.post(url, data=data, headers=header, auth=HTTPBasicAuth('admin', 'admin'), verify=False)    
    print(r.text)
market_buy()
from requests import post

headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer <AUTHENTICATION TOKEN>",
}

data = {
    "order": {
        "units": "100",
        "instrument": "EUR_USD",
        "timeInForce": "FOK",
        "type": "MARKET",
        "positionFill": "DEFAULT",
    }
}

post(
    "https://api-fxtrade.oanda.com/v3/accounts/<myaccount>/orders",
    headers=headers,
    data=data,
)