ISDP-如何调用API?:操作步骤

时间:2024-04-28 09:50:05

操作步骤

  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+对应租户后台管理创建,详见1.2节OpenAPI订阅说明。

    响应参数:

    {

    "access_token": "1367******************f7bd3ce",

    "token_type": "bearer",

    "refresh_token": "579b******************9d5396b25f",

    "expires_in": 1799,

    "scope": "all",

    "plat_user": {

    "userId": XXXX,

    "userNo": "sdcpOj******************BpwNMdYcz",

    "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

    }

    }

    图1 请求token
    图2 请求token示例

  2. 从响应参数里面获取token口令

    获取token口令,token = token_type + " "+ access_token,中间为英文输入法下的一个空格。

    图3 获取token口令

    以上图示例token口令为:bearer 1367d******************27-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 1367******************f7bd3ce

    图4 传token调用接口

    Body参数:

    图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": "sdcpO******************wNMdYcz",

    "password": "iEVJNc******************QluW8R"

    }

    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)

support.huaweicloud.com/usermanual-isdp/ISDP-0325.html