M
mahsan9861
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
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