curl 命令有效,但 pycurl 命令不适用于 smartsheet

Curl command works but not pycurl command for smartsheet

我能够使用 Curl 命令成功更新智能表:

curl https://api.smartsheet.com/2.0/sheets/sheetID/rows \
> -H "Authorization: Bearer token" \
> -H "Content-Type: application/json" \
> -X PUT \
> -d '[{"id": rid, "locked" : false}]'

但是当我尝试使用 Pycurl 在 python 脚本中执行相同操作时:我收到授权错误。我不确定我在这里做错了什么。

c = pycurl.Curl()
c.setopt(pycurl.URL,url)
c.setopt(pycurl.HTTPHEADER, ['Authorization: Bearer ' + token])
c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json'])
data = json.dumps({"id": rid, "locked": False })
c.setopt(pycurl.CUSTOMREQUEST, "PUT")
c.setopt(pycurl.POSTFIELDS,data)
c.setopt(pycurl.POSTFIELDSIZE, len(data))
c.setopt(pycurl.VERBOSE, 1)
c.perform()
c.close()

我收到一个错误:

* About to connect() to api.smartsheet.com port 443 (#0)
*   Trying 198.101.138.130...
* Connected to api.smartsheet.com (198.101.138.130) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
* Server certificate:
*   subject: CN=api.smartsheet.com,O="Smartsheet.com, Inc.",L=Bellevue,ST=Washington,C=US,serialNumber=6#####7,businessCategory=Private Organization,incorporationState=Washington,incorporationCountry=US
*   start date: Jul 09 00:00:00 2015 GMT
*   expire date: Jul 08 23:59:59 2017 GMT
*   common name: api.smartsheet.com
*   issuer: CN=Symantec Class 3 EV SSL CA - G3,OU=Symantec Trust Network,O=Symantec Corporation,C=US
> POST /2.0/sheets/sheetid/rows HTTP/1.1
User-Agent: PycURL/7.29.0
Host: api.smartsheet.com
Content-Type: application/json
Accept: application/json
Content-Length: 40

* upload completely sent off: 40 out of 40 bytes
< HTTP/1.1 403 Forbidden
< Date: Mon, 15 Aug 2016 15:17:55 GMT
< Content-Type: application/json;charset=UTF-8
< Content-Length: 116
< Connection: close
< 
{
  "errorCode" : 1004,
  "message" : "You are not authorized to perform this action.",
  "refId" : "za6gmwnreg78"
* Closing connection 0
}403

我不确定哪里出了问题。如果有人可以建议可以做什么(同时仍在使用 pycurl)或任何其他解决方法,那就太好了!请帮忙!谢谢!

我认为问题是由于您两次调用 c.setopt(pycurl.HTTPHEADER, ...) 引起的,第二次调用覆盖了第一次调用中设置的身份验证令牌。

尝试用以下内容替换两个 c.setopt 调用:

c.setopt(pycurl.HTTPHEADER, ['Authorization: Bearer ' + token, 'Content-Type: application/json', 'Accept: application/json'])