Authentication and getting a session token from Quickblox in Python -
i'm doing through rest api. 2 questions
1) want push existing data quickblox custom object. how many rest calls need? (i not clear whole state of affair involving computer security.) first (a) session token. , follow create new record here?
2) i'm trying session token i'm getting {"errors":{"base":["unexpected signature"]}}
response. here code genereate nonce, signature, , getting session token:
# of course these not 0, x, , y's. appid = '0000' authkey = 'xxxxxxxxxxx' authsecret = 'yyyyyyyyyyyyyy' def getnonce(): import random return random.random() def createsignature(nonce): import hashlib import hmac import binascii import time stringforsignature = 'application_id={id}&auth_key={auth_key}&nonce={nonce}×tamp={timestamp}'.format(id=appid, auth_key=authkey, nonce=nonce, timestamp=time.time()) hmacobj = hmac.new(authkey, stringforsignature, hashlib.sha1) return binascii.b2a_base64(hmacobj.digest())[:-1] # -1 rid of \n def getsessiontoken(): import time epoch = "%s" % int(time.time()) nonce = getnonce() params = {'application_id': appid, 'auth_key': authkey, 'timestamp': epoch, 'nonce': nonce, 'signature': createsignature(nonce)} jsondata = json.dumps(params) httpheaders = {'content-type': 'application/json', 'quickblox-rest-api-version': '0.1.0'} r = requests.post('https://api.quickblox.com/session.json', data=jsondata, headers = httpheaders) print 'status code:', r.status_code responsejson = r.text print responsejson response = json.loads(responsejson) getsessiontoken()
i suppose it's way signature generated causing problem?
i have found following problem in code:
- func. random - need integer value (not between 0 , 1)
- func. timestamp. calculate "timestamp" twice. better use 1 time "timestamp"
- (def createsignature) - alredy know... code use other algoruthm, need.
i recomend use following code, mistake above modified. result following auth: --------- request -------------------------------- --------- request user authorization --------- --------- request device parameters ----------
# -*- encoding: utf-8 -*- # link: http://quickblox.com/developers/authentication_and_authorization#signature_generation import json import requests import sha import hmac #========== data ======================= application_id = 'xxxx' authorization_key = 'xxxxxxx-xxx-xx' authorization_secret = 'xxxxxxxxxxxxxxxxxx' var_login = 'user1' var_password = 'password1' # =========================================== platform = "ios" # want udid = "7847674035" # want def gettimestampnonce(): import random import time return str(time.time()), str(random.randint(1, 10000)) def createsignaturesimple(timestamp, nonce): stringforsignature = 'application_id={id}&auth_key={auth_key}&nonce={nonce}×tamp={timestamp}'.format(id=application_id, auth_key=authorization_key, nonce=nonce, timestamp=timestamp) return hmac.new(authorization_secret, stringforsignature, sha).hexdigest() def getparamssimple(): timestamp, nonce = gettimestampnonce() return {'application_id': application_id, 'auth_key': authorization_key, 'timestamp': timestamp, 'nonce': nonce, 'signature': createsignaturesimple(timestamp, nonce)} def createsignatureuser(timestamp, nonce): stringforsignature = 'application_id={id}&auth_key={auth_key}&nonce={nonce}×tamp={timestamp}&user[login]={login}&user[password]={password}'.format(id=application_id, auth_key=authorization_key, nonce=nonce, timestamp=timestamp, login=var_login, password=var_password) return hmac.new(authorization_secret, stringforsignature, sha).hexdigest() def getparamsuser(): timestamp, nonce = gettimestampnonce() return {'application_id': application_id, 'auth_key': authorization_key, 'timestamp': timestamp, 'nonce': nonce, 'signature': createsignatureuser(timestamp, nonce), 'user': {'login': var_login, 'password': var_password}} def createsignaturedevice(timestamp, nonce): stringforsignature = 'application_id={id}&auth_key={auth_key}&device[platform]={platform}&device[udid]={udid}&nonce={nonce}×tamp={timestamp}&user[login]={login}&user[password]={password}'.format(id=application_id, auth_key=authorization_key, platform=platform, udid=udid, nonce=nonce, timestamp=timestamp, login=var_login, password=var_password) return hmac.new(authorization_secret, stringforsignature, sha).hexdigest() def getparamsdevice(): timestamp, nonce = gettimestampnonce() return {'application_id': application_id, 'auth_key': authorization_key, 'timestamp': timestamp, 'nonce': nonce, 'signature': createsignaturedevice(timestamp, nonce), 'user': {'login': var_login, 'password': var_password}, 'device': {'platform': platform, 'udid': udid}} def getsessiontoken(): httpheaders = {'content-type': 'application/json', 'quickblox-rest-api-version': '0.1.0'} requestpath = 'https://api.quickblox.com/session.json' print "====================================================" print "--------- request --------------------------------" jsondata = json.dumps(getparamssimple()) r = requests.post(requestpath, data=jsondata, headers = httpheaders) print 'status code:', r.status_code responsejson = r.text print responsejson print "====================================================" print "--------- request user authorization ---------" jsondata = json.dumps(getparamsuser()) r = requests.post(requestpath, data=jsondata, headers = httpheaders) print 'status code:', r.status_code responsejson = r.text print responsejson print "====================================================" print "--------- request device parameters ---------" jsondata = json.dumps(getparamsdevice()) r = requests.post(requestpath, data=jsondata, headers = httpheaders) print 'status code:', r.status_code responsejson = r.text print responsejson print "=====================================================" getsessiontoken()
Comments
Post a Comment