如何调用API?
前提条件
租户已订阅API
配置简介
1.请求token
以ISDP+查询问题单API接口调用作为示例,获取token口令地址如下:
beta环境地址:
https://isdpcloud-beta.sd.huawei.com/oauth2/oauth/rest_token
生产环境地址:
https://isdpcloud.huawei.com/oauth2/oauth/rest_token
请求类型: post
以下以测试beta环境地址为例Java调用示例:
请求参数:
body : {
"client_id":"isdp-saas-openapi",
"client_secret":"isdp-saas-openapi",
"grant_type":"password",
"username": "********************",
"password":""********************"
};
注:clien_id、client_secret、grant_type为固定参数, "username"和"password"在ISDP+对应租户后台管理创建,详见19.10章:如何订阅API?。
响应参数:
{
"access_token": "1367d1b0-4716-475d-8b27-c80c5f7bd3ce",
"token_type": "bearer",
"refresh_token": "579b9204-3f35-42b9-b148-239d5396b25f",
"expires_in": 1799,
"scope": "all",
"plat_user": {
"userId": XXXX,
"userNo": "sdcpOjhXZoX27Vdstxnm3QqBpwNMdYcz",
"userName": "XXXX beta",
"userType": "API",
"email": null,
"phone": null,
"tenantId": 1451,
"status": "0",
"deleteFlag": 0,
"integrationAccount": null,
"pwdUpdateDate": "2020-12-27T16:00:00.000+00:00",
"password": null,
"salt": null,
"machineCode": null,
"fingerMark": 0,
"faceRecog": 0,
"facesetToken": null,
"currentLoginType": "password",
"openId": null
}
}
图13.3-1 请求token
图13.3-2 请求token示例
2.从响应参数里面获取token口令
获取token口令,token = token_type + " "+ access_token,中间为英文输入法下的一个空格。
图13.3-3 获取token口令
以上图示例token口令为:bearer 1367d1b0-4716-475d-8b27-c80c5f7bd3ce,beaer后加一个英文输入法下的空格
3.传token调用接口
测试坏境:
https://isdpcloud-beta.sd.huawei.com/openapi/v1/openTask/createTask
生产环境:
https://isdpcloud.huawei.com/openapi/v1/openTask/createTask
请求类型: post
Authorization为:bearer 1367d1b0-4716-475d-8b27-c80c5f7bd3ce
图13.3-4 传token调用接口
Body参数:
图13.3-5 Body参数
Python调用示例:
mport json
import urllib3
import requests
BETA_TOKEN_URL = "https://isdpcloud-beta.sd.huawei.com/oauth2/oauth/rest_token"
PRO_TOKEN_URL = "https://isdpcloud.huawei.com/oauth2/oauth/rest_token"
def get_token(beta_or_pro):
url = BETA_TOKEN_URL if beta_or_pro == 'beta' else PRO_TOKEN_URL
headers = {
"Content-Type": "application/json",
}
data_json = {
"client_id": "isdp-saas-openapi",
"client_secret": "isdp-saas-openapi",
"grant_type": "password",
"username": "sdcpOjhXZoX27Vdstxnm3QqBpwNMdYcz",
"password": "iEVJNcwUzc7Dr5LHU2Lol0am2lQluW8R"
}
data_str = json.dumps(data_json)
urllib3.disable_warnings() # 忽略SSL告警
resp = requests.request("POST", url, data=data_str, headers=headers, verify=False)
# print("get_token.result = ", resp.text)
if resp.status_code == 200:
json_result = json.loads(resp.text)
if "access_token" in json_result:
result = json_result['token_type'] + " " + json_result['access_token']
return result
else:
return ""
else:
return ""
def call_api(token, url, data_json):
headers = {
"Content-Type": "application/json",
"Authorization": token,
}
data_str = json.dumps(data_json)
urllib3.disable_warnings() # 忽略SSL告警
resp = requests.request("POST", url, data=data_str, headers=headers, verify=False)
print(resp.status_code, resp.text)
if __name__ == '__main__':
token_str = get_token('beta')
print("token =", token_str)
api = "https://isdpcloud-beta.sd.huawei.com/openapi/v1/openTask/createTask"
data = {
"taskQueries": [{
"customerTaskId": 1234567899,
}]
}
call_api(token_str, api, data)