简体   繁体   中英

Curl command works but not pycurl command for smartsheet

I am able to successfully update the smartsheet using Curl Commands:

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}]'

But when I try to do the same in my python script using Pycurl: I get authorization error. I am not sure what I am doing wrong here.

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()

I get an error:

* 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

I am not sure what is wrong. It would be great if someone could suggest what can be done (while still using pycurl) or also any other workaround! Please help! Thanks!

I think the issue is caused by your calling c.setopt(pycurl.HTTPHEADER, ...) twice, and the second invocation overwrites the auth token set in the first.

Try replacing the two c.setopt calls with the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM