Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53876
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-09-09 09:36 -0700 |
| Message-ID | <37fa354c-691c-45f9-ab31-807f86340e4d@googlegroups.com> (permalink) |
| Subject | python REST API access fails after adding date to URL |
| From | mahsan9861@gmail.com |
Hi,
So the REST API calls work great with out the URL appended to the URL.However as soon as I do add the URL, because I want to retrieve the data on a daily basis, the calls fail and the server will return a 401 and say signature invalid.The code is below:
import oauth2 as oauth
import time
from random import getrandbits
from base64 import b64encode
import hashlib
import urllib
import hmac
import base64
import urllib2
import httplib
import requests
CONSUMER_KEY='xxxx'
CONSUMER_SECRET='xxxx'
TOKEN_KEY='xxxx'
TOKEN_SECRET='xxxx'
def getURL(baseURL,params):
sortedParams = sorted(params.items())
for head in sortedParams:
print head[0]
print head[1]
baseURL += '&' + head[0] + '="' + head[1] + '"'
print baseURL
return baseURL
#set header parameters
http_headers = {}
http_headers['oauth_version'] = '1.0'
http_headers['oauth_nonce'] = oauth.generate_nonce()
http_headers['oauth_consumer_key'] = CONSUMER_KEY
http_headers['oauth_signature_method'] = 'HMAC-SHA1'
http_headers['oauth_token'] = TOKEN_KEY
http_headers['oauth_timestamp'] = str(int(time.time()))
#base url
base_url = 'https://dacv.liveperson.net/dataAccess/account/accountNumber/visitorSession'
#sort headers in lexicographical order and encode url
params = urllib.urlencode(sorted(http_headers.items()))
encoded_url = urllib.quote_plus(base_url)
#generate base string with all headers,encoded and ready to generate signature
signature_base_string = 'GET&' + encoded_url + '&' + urllib.quote_plus(params)
#generate signiture
key = CONSUMER_SECRET + '&' + TOKEN_SECRET
hashed = hmac.new(key, signature_base_string, hashlib.sha1)
http_headers['oauth_signature'] = urllib.quote_plus(base64.b64encode(hashed.digest()))
#set up header
oauth_header = 'OAuth oauth_consumer_key="' + http_headers['oauth_consumer_key'] + '",' +'oauth_nonce="' + http_headers['oauth_nonce'] + '",' +'oauth_signature_method="' + http_headers['oauth_signature_method'] + '",' +'oauth_token="' + http_headers['oauth_token'] + '",' +'oauth_timestamp="' + http_headers['oauth_timestamp'] + '",' +'oauth_version="' + http_headers['oauth_version'] + '",' +'oauth_signature="' + http_headers['oauth_signature'] + '"'
req = urllib2.Request(base_url)
req.add_header('Authorization',oauth_header)
response = urllib2.urlopen(req)
print response
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
python REST API access fails after adding date to URL mahsan9861@gmail.com - 2013-09-09 09:36 -0700
Re: python REST API access fails after adding date to URL dieter <dieter@handshake.de> - 2013-09-10 08:15 +0200
Re: python REST API access fails after adding date to URL mahsan9861@gmail.com - 2013-09-10 07:33 -0700
Re: python REST API access fails after adding date to URL mahsan9861@gmail.com - 2013-09-10 08:42 -0700
Re: python REST API access fails after adding date to URL mahsan9861@gmail.com - 2013-09-10 08:44 -0700
csiph-web