2017-11-06 2 views
0

Ich versuche, meine Ziele von Vuforias API zu bekommen, aber ich kann nicht den letzten Wert der Überschrift "Authorization" übergeben, die eine codierte Daten ist, der Fehler das ich bin immer ist dies:Unicode-Objekte müssen vor dem Hashing codiert werden Python 3.6 Vuforia

Unicode-Objekte vor Hashing

dies in try-Schnipsel des Codes codiert werden muss, ich bin nach den vuforia's documentation aber immer noch, ist etwas falsch mit meinem Code und ich don habe keine ahnung was es ist

import base64 
import hashlib 
import hmac 

import requests 
from flask import Flask, request 
from email.utils import formatdate 
import logging 

app = Flask(__name__) 


@app.route('/') 
def hello_world(): 
    try: 
     import http.client as http_client 
    except ImportError: 
     # Python 2 
     import httplib as http_client 
    http_client.HTTPConnection.debuglevel = 1 

    logging.basicConfig() 
    logging.getLogger().setLevel(logging.DEBUG) 
    requests_log = logging.getLogger("requests.packages.urllib3") 
    requests_log.setLevel(logging.DEBUG) 
    requests_log.propagate = True 

    url = 'https://vws.vuforia.com/targets' 
    req = requests.Request('GET', url) 
    req.headers = setHeaders(req) 
    resp = requests.Session().send(req.prepare()) 

    return resp.text 


def compute_md5_hex(data): 
    """Return the hex MD5 of the data""" 
    h = hashlib.md5() 
    h.update(data) 
    return h.hexdigest() 


def compute_hmac_base64(key, data): 
    """Return the Base64 encoded HMAC-SHA1 using the provide key""" 
    h = hmac.new(key, None, hashlib.sha1) 
    h.update(data) 
    return base64.b64encode(h.digest()) 


def setHeaders(request): 
    date = formatdate(None, localtime=False, usegmt=True) 
    accessKey = "ce1500fhfth429279173fd839f9d414532014a3da" 
    secret_key = b"5d3fdawd7211447c35be607ae5a08ec794a09d71d" 
    headers = {'Date': date, 'Authorization': "VWS " + accessKey + ":" + tmsSignature(request, secret_key)} 

    return headers 


def tmsSignature(request, secretKey): 
    method = request.method 
    contentType = "" 
    hexDigest = "d41d8cd98f00b204e9800998ecf8427e" 
    if method == "GET" or method == "POST": 
     pass 
     # Do nothing because the strings are already set correctly 
    elif method == "POST" or method == "PUT": 
     contentType = "application/json" 
     # If this is a POST or PUT the request should have a request body 
     hexDigest = compute_md5_hex(request) 
    else: 
     print("ERROR: Invalid content type passed to Sig Builder") 

    # Date in the header and date used to calculate the hash must be the same 
    dateValue = formatdate(None, localtime=False, usegmt=True) 
    requestPath = str(request.url) 
    components_to_sign = list() 
    components_to_sign.append(method) 
    components_to_sign.append(str(hexDigest)) 
    components_to_sign.append(str(contentType)) 
    components_to_sign.append(str(dateValue)) 
    components_to_sign.append(str(requestPath)) 
    string_to_sign = "\n".join(components_to_sign) 
    shaHashed = "" 
    try: 
     shaHashed = compute_hmac_base64(secretKey, string_to_sign) 
    except Exception as e: 
     print("ERROR ", e) 
    return shaHashed 


if __name__ == '__main__': 
    app.run() 

Antwort

1

Blick in yo ur hmac_base64_key Funktion, dieser bestimmte Anruf ist die Ursache:

h.update(data) 

Da, dass das ist update Funktion aus der hmac Bibliothek, daß das Eingangsbyte anstelle die Zeichenfolge/Unicode (Besuche die Dokumentation auf hmac werden muß, das bedeutet zu hashlib für seine update Signatur).

So scheint es, wie das Update einfach ist:

h.update(data.encode("utf8")) # or other encoding you want to use 

Beachten Sie, dass Sie benötigen, um wieder den Rückgabewert von compute_hmac_base64 (shaHashed) zu Zeichenfolge ändern, da Sie es mit einer Schnur in setHeaders sind verketten .

(Ich nehme einen Python 3-Code, obwohl Sie eine Überprüfung für Python 2 in Ihrem Code übrigens haben, da Sie dieses Python 3 markiert haben).

+0

das war es, danke – AND4011002849

Verwandte Themen