我在您的代码中发现了以下问题:
- 功能。RANDOM - 我们需要整数值(不在 0 和 1 之间)
- 功能。时间戳。您计算“时间戳”两次。最好使用一次“时间戳”
- (def createSignature) - 正如您已经知道的那样......您的代码使用了其他算法,而不是我们需要的。
我建议您使用以下代码,上面的错误已被修改。结果,您将获得以下身份验证: --------- 请求 ------------------------------- - --------- 请求用户授权 --------- --------- 请求设备参数 ----------
# -*- encoding: utf-8 -*-
# Link: http://quickblox.com/developers/Authentication_and_Authorization#Signature_generation
import json
import requests
import sha
import hmac
#========== YOUR DATA =======================
application_id = 'XXXX'
authorization_key = 'xxxxxxx-XXX-XX'
authorization_secret = 'XXXXXXXXXXXXXXXXXX'
var_login = 'user1'
var_password = 'password1'
# ===========================================
platform = "ios" # like you want
udid = "7847674035" # like you 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 With 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 With 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()