0

(New to python)

I'm trying to make a simple authenticated put of a file... so I make two curls, the first one to authenticate (which prints the token out as expected) but when I use the same variable (token) to add it to the headers ("Authorization: Bearer %s" % str(token)) token is empty. What am I doing wrong here?

import urllib
import cStringIO
import pycurl
import requests
from urllib import urlencode
import os.path

# declarations
filename = "./profile.jpg"
response = cStringIO.StringIO()
c = pycurl.Curl()

# formdata
post_data = {'username': '...', 'password':'...'}
# Form data must be provided already urlencoded.
postfields = urlencode(post_data)
# Sets request method to POST,
# Content-Type header to application/x-www-form-urlencoded
# and data to send in request body.

print "*****************************************************"

# authenticate
c = pycurl.Curl()
c.setopt(c.POST, 1)
c.setopt(c.URL, "https://.../auth")
c.setopt(c.POSTFIELDS, postfields)
c.setopt(c.SSL_VERIFYPEER, 0)
c.setopt(c.SSL_VERIFYHOST, 0)
c.setopt(c.VERBOSE, 1)
c.perform()
c.close() 
token = response.getvalue()
print token

print "*****************************************************"

# upload file
filesize = os.path.getsize(filename)
fin = open(filename, 'rb')

c = pycurl.Curl()
c.setopt(c.PUT, 1)
c.setopt(c.URL, "https://.../avatar")
c.setopt(c.HTTPPOST, [("file", (c.FORM_FILE, filename))])
c.setopt(c.HTTPHEADER, [
    "Authorization: Bearer %s" % str(token), 
    "Content-Type: image/jpeg"
])
c.setopt(c.READFUNCTION, fin.read)
c.setopt(c.POSTFIELDSIZE, filesize)
c.setopt(c.SSL_VERIFYPEER, 0)
c.setopt(c.SSL_VERIFYHOST, 0)
c.setopt(c.VERBOSE, 1)
c.setopt(c.WRITEFUNCTION, response.write),
c.perform()
c.close() 
print response.getvalue()

print "*****************************************************"

Request:

> PUT ../avatar HTTP/1.1
User-Agent: PycURL/7.19.3 libcurl/7.35.0 GnuTLS/2.12.23 zlib/1.2.8 libidn/1.28 librtmp/2.3
Host: 127.0.0.1:8080
Accept: */*
Transfer-Encoding: chunked
Authorization: Bearer 
Expect: 100-continue

Response:

< HTTP/1.1 100 Continue
< HTTP/1.1 401 Unauthorized
< content-type: application/json; charset=utf-8
< cache-control: no-cache
< content-length: 86
< Date: Tue, 02 Jun 2015 19:09:29 GMT
< Connection: keep-alive
< 
* Connection #1 to host 127.0.0.1 left intact

{"statusCode":401,"error":"Unauthorized","message":"Incorrect Token or Token Expired"}
Bradley
  • 16
  • 3
  • 1
    What's the exact error you are getting? – James Buck Jun 02 '15 at 19:19
  • 401 unauthorized... but the output headers show: ... Authorization: Bearer ... No Token in the header – Bradley Jun 02 '15 at 20:26
  • @James - It's actually not an error - without the token in the Authorization header I would expect a 401. The problem is the token seems to be empty, however, at the `print token` line the token is printing to the console. – Bradley Jun 02 '15 at 20:30

1 Answers1

0

I think there is an encoding problem. The print function is able to output something without caring about the encoding. Looking at the PycURL quickstart documentation it mentions this issue. I would try to manipulate the encoding on this line:

"Authorization: Bearer %s" % str(token)

and try to do something like this instead:

"Authorization: Bearer %s" % token.decode('iso-8859-1')

(I would try .decode("utf-8") also, depending on what the encoding is)

You might need to change response = cStringIO.StringIO() to response = BytesIO(). I cannot give a definitive answer because I'm unsure about your setup.

EDIT: My suspicions about encoding affirmed by this post about cStringIO where it says that Unicode is not supported.

Community
  • 1
  • 1
Aaron D
  • 5,817
  • 1
  • 36
  • 51