Updated on 2024-04-02 GMT+08:00

Python 3 API Example

This section uses Image Tagging as an example to describe how to call Python 3 APIs.

# encoding:utf-8

import requests
import base64

url = "https://{endpoint}/v2/{project_id}/image/tagging"
token = "Actual token value obtained by the user"
headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}

imagepath = r'data/image-tagging.jpg'
with open(imagepath, "rb") as bin_data:
    image_data = bin_data.read()
image_base64 = base64.b64encode(image_data).decode("utf-8")  # Use Base64 encoding of images.
data= {"image": image_base64}  # Set either the URL or the image.

response = requests.post(url, headers=headers, json=data, verify=False)
print(response.text)
Table 1 Parameter description

Parameter

Description

url

API request URL, for example, https://{endpoint}/v2/{project_id}/image/tagging.

token

A token is a user's access credential, which includes user identities and permissions. When you call an API to access a cloud service, a token is required for identity authentication.

For details about how to obtain the token, see Authentication.

imagePath

Image path. An image file path or image URL is supported. The URL can be an HTTP/HTTPS or OBS URL.