0

我正在尝试使用kairos API 注册个人资料图片。我有以下 python 代码。

我的python脚本:

#!/usr/bin/python3

import requests
import io
import http.client
import base64
import json
import glob, os
from os.path import basename

dir_path = "/home/Kairos/profiles/msc/" 

os.chdir(dir_path)

for file in glob.glob("*.*"):
    img_path = file
    print(img_path)
    files = {'image': (img_path, open(dir_path + img_path, 'rb'), 'image/jpg', {'Expires': '0'})}

    headers = {
        'app_id': "xxxx",
        'app_key': "xxxxxxxxxxxxxxxxxxxxxx",
    }

    values_enrol = {
       'subject_id':'msc',
       'gallery_name':'mscGallary'
    }

    res = requests.post('https://api.kairos.com/enroll', headers=headers, files=files, data=values_enrol)

    print(res.content)

但是,我收到一个错误:

 "Errors": [
        {
            "ErrCode": 1002,
            "Message": "subject_id one or more required parameters are missing"
        },
        {
            "ErrCode": 1002,
            "Message": "gallery_name one or more required parameters are missing"
        }
    ]
}

我在这里做错了什么?请有人帮助我。

提前致谢。

4

1 回答 1

0

我找到了解决方案:

def build_payload(file):    
    if file is not None:
        image = extract_base64_contents(file)
    else:
        image = url

    values_enrol = {
       'image': image,
       'gallery_name': sys.argv[1],
    }

    return dict(values_enrol)

def extract_base64_contents(file):
    with open(file, 'rb') as fp:
        image = base64.b64encode(fp.read()).decode('ascii')
    return image
于 2018-02-05T13:33:58.233 回答