token authentication rest and python

139 views
Skip to first unread message

George Siddiqui

unread,
Aug 15, 2016, 10:30:50 AM8/15/16
to DevTargetProcess
Dear TargetProcess,

I've got the basic authentication working from python using your samples (Thank you!)

I've had less luck trying to replicate this behaviour in using access tokens from a script.

In the chrome browser the access tokens work just fine. 

Currently when I attempt to connect via urllib2 I get the following error?

Please could you point me in the right direction for making urllib2 to respect the URL correctly?


request = urllib2.Request("https://unmarkedtek.tpondemand.com/api/v1/Projects/2/?access_token=MTp6b******6NS9mMEpqMDBLRkhoVFBWSXZMa2hLMktUYWRzTkNYc3IxYm0wPQ==")

>>> response = urllib2.urlopen(request)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen

    return opener.open(url, data, timeout)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 431, in open

    response = self._open(req, data)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 449, in _open

    '_open', req)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain

    result = func(*args)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1240, in https_open

    context=self._context)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1197, in do_open

    raise URLError(err)

urllib2.URLError: <urlopen error [Errno 54] Connection reset by peer>

zh...@targetprocess.com

unread,
Aug 15, 2016, 12:36:20 PM8/15/16
to DevTargetProcess
Have you tried ?token= instead of ?access_token= ?

tsar...@targetprocess.com

unread,
Sep 6, 2016, 12:05:08 PM9/6/16
to DevTargetProcess
Hello, George.

If issue is still relevant, let's try to find a reason :).

As from https://www.freebsd.org/cgi/man.cgi?query=errno&sektion=2:

54 ECONNRESET Connection reset by peer. A connection was forcibly closed
             by a peer.  This normally results from a loss of the connection
             on the remote socket due to a timeout or a reboot.

Since you use urllib2 and raw request in browser works well, let's assume problems with Python 2.7 urllib2 TLS1.2 request handling.

Some free sources warn about urllib2 till Python 3.4 doesn't support TLS1.2:
You can use Python 3.4 and upper, or try to follow this link to add support of TLS1.2 in Python 2.7 (requires build of PyOpenSSL fork https://github.com/enernoc/pyopenssl): https://gist.github.com/thom-nic/6510373  

Hope, this helps!

On Monday, August 15, 2016 at 5:30:50 PM UTC+3, George Siddiqui wrote:
Reply all
Reply to author
Forward
0 new messages