Encode-python error 3.6

0
Traceback (most recent call last):
  File "uns.py", line 64, in <module>expiration_date="2017-08-27",
  File "uns.py", line 53, in create_user print(response.text)
  File "F:\Python\Python36\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 1822-1823: character maps to <undefined>

uns.py :

from uuid import uuid4
from hashlib import sha256
from urllib.parse import urlencode, parse_qsl
from requests.auth import AuthBase
import requests


API_URL = "https://web-stage.voxy.com/partner_api"
API_KEY = "vSMgpl4"
API_SECRET = "Oe9qFkHUqsDWhc"


class VoxyAuth(AuthBase):
    """Attaches HTTP Voxy Authentication Header to the given Request object."""
    def __init__(self, api_key, api_secret):
        self.api_key = api_key
        self.api_secret = api_secret

    def __call__(self, request):
        request.headers['Authorization'] = "Voxy {}:{}".format(
            API_KEY,
            self.generate_signature(request)
        )
        # request.headers['Content-Type'] = "application/x-www-form-urlencoded"
        return request

    def generate_signature(self, request):
        if request.body:
            data = parse_qsl(request.body)
        else:
            data = {}
        url_encoded_data = urlencode(sorted(data))
        concat_string_data = self.api_secret.encode("utf-8") + url_encoded_data.encode("utf-8")
        hashed_data = sha256(concat_string_data)
        return hashed_data.hexdigest()


def create_user(external_user_id, first_name, email_address, native_language, expiration_date, **additional):
    url = "{}/partners/users/{}".format(API_URL, external_user_id)
    data = {
        "external_user_id": external_user_id,
        "first_name": first_name,
        "email_address": email_address,
        "native_language": native_language,
        "expiration_date": expiration_date
    }
    data.update(additional)
    response = requests.post(url, data=data, auth=VoxyAuth(API_KEY, API_SECRET))
    print("========= CREATE USER ===========")
    print("created user with external_user_id: {}".format(external_user_id))
    print(url)
    print(response)
    print(response.text)
    print("========= CREATE USER ===========")


random_string = uuid4().hex

create_user(
    external_user_id="1059",
    first_name="Gringo",
    email_address="[email protected]",
    native_language="pt",
    expiration_date="2017-08-27",
)
    
asked by anonymous 23.02.2017 / 13:14

0 answers