ice_kk 2025-01-13 12:43 采纳率: 0%
浏览 37

使用python调用T+开放平台api接口,提示提供的服务名称不正确的错误

img

代码主体是参考了官方提供的chanjet_openapi_python_sdk,在调用开放平台T+api 《请购单》的请购单列表查询 接口时,提示提供的服务名称(PurchaseRequisitionOpenApi/FindVoucherList)不正确

首先,我根据common_api_demo文件,改写了获取token的方法,能成功建立链接并使用demo提供的查询存货的方法,所以应该可以排除获取token的相关问题。

img


代码大致如下:

刷新开放平台token的示例

def refresh_token(client):
    req = RefreshTokenRequest()
    req.request_uri = '/auth/refreshToken'
    # 创建数据库连接
    tp_connect = pypyodbc.connect(driver='{SQL Server}', server='tcp:192.168.****,1433',
                                  database='UFTData534****', uid='*****',
                                  pwd='********')
    # 获取所有表单数据
    cursor = tp_connect.cursor()
    sql = "select refresh_token ,DATEADD(DAY ,-1 ,expire_date) as expire_date from dbo.aa_tplus_token"
    cursor.execute(sql)  # 执行sql语句
    data = cursor.fetchone()  # 读取查询结果
    # 授权码
    refresh_token = data[0]
    # 过期日期
    expire_date = data[1]
    # 获取当前时间
    now = datetime.now()
    # 格式化成字符串
    formatted_date = now.strftime("%Y-%m-%d")
    # 当日期到达过期日期,则用refresh_token刷新token (29天有效期)
    if expire_date == formatted_date:
        params = {
            'grantType': 'refresh_token',
            'appKey': '******',
            'refreshToken': refresh_token
        }
        req.add_query_params(params)
        result = client.execute(req, RefreshTokenResponse)
        res = result.result
        print(res)
        # 授权码
        access_token = res.access_token
        #print(access_token)
        # 刷新授权码
        refresh_token = res.refresh_token
        # 应用名称
        app_name = res.app_name
        # 用户永久授权码
        user_auth_permanent_code = res.user_auth_permanent_code

        sql = f"update dbo.aa_tplus_token set access_token = '{access_token}' ,refresh_token = '{refresh_token}' \
                ,app_name = '{app_name}' ,user_auth_permanent_code = '{user_auth_permanent_code}' \
                ,token_date = convert(date,getdate(),23) , expire_date = DATEADD(DAY , 29 ,getdate()) ,write_time = getdate()"

        cursor.execute(sql)

    cursor.close()
    cursor.commit()

def get_token():
    # print("aaaa")
    # 设置自定义日志配置
    ChanjetLogger(logging.INFO)

    client = ChanjetClient('https://openapi.chanjet.com')
    # 请填入您从开放平台申请下来的appKey
    client.app_key = '****'
    # 请填入您从开放平台申请下来的appSecret
    client.app_secret = '******'
    # 刷新开放平台token
    refresh_token(client)

    # 创建数据库连接
     tp_connect = pypyodbc.connect(driver='{SQL Server}', server='tcp:192.168.****,1433',
                                  database='UFTData534****', uid='*****',
                                  pwd='********')
    # 获取所有表单数据
    cursor = tp_connect.cursor()
    sql = "select access_token from dbo.aa_tplus_token"
    cursor.execute(sql)  # 执行sql语句
    data = cursor.fetchone()  # 读取查询结果
    #授权码
    access_token = data[0]
    # 填入开放平台openToken
    client.open_token = access_token

    cursor.close()
    cursor.commit()

    return client

然后根据提供的案例仿写了request、content、response,以下为代码案例,问题不知道出在哪里,请大家帮忙指正

# -*- coding: utf-8 -*-
# @Time    : 2025/1/10 16:46
# @Author  : kyrie_ice
# @Desc    : 查询请购单明细列表请求类
from chanjet_openapi_python_sdk.chanjet_request import ChanjetRequest


class QueryPurchaseRequisitionRequest(ChanjetRequest):

    def get_http_method(self):
        return 'post'




# -*- coding: utf-8 -*-
# @Time    : 2025/1/11 15:01
# @Author  : kyrie_Ice
# @Desc    : 请购单查询条件的请求实体
from chanjet_openapi_python_sdk.chanjet_content import ChanjetContent

class QueryPurchaseRequisitionContent(ChanjetContent):

    def __init__(self):
        # 需要返回的字段名
        #self.SelectFields = ["PurchaseRequisition.Code","PurchaseRequisition.VoucherDate","PurchaseRequisition.RequisitionPerson.Code","PurchaseRequisition.RequisitionPerson.Name","PurchaseRequisition.SuggPartner.Code","PurchaseRequisition.SuggPartner.Name","PurchaseRequisition.RequireDate","PurchaseRequisition.VoucherState.ID","PurchaseRequisition.Memo","PurchaseRequisition.Maker"]
        self.SelectFields = ["PurchaseRequisition.ID","PurchaseRequisition.VoucherDate","PurchaseRequisition.Code"]
        # 需要传入的参数
        self.ParamDic = {} 
        # 开始页
        self.pageIndex = 0
        # 显示的页大小
        self.pageSize = 6666

    # class ParamDic:
    #
    #     def __init__(self,code):
    #         """
    #         存货查询条件实体初始化方法
    #         :param code: 请购单单据编号
    #         :type code: str
    #         :param inventory_class_code: 存货分类编码
    #         :type inventory_class_code: str
    #         :param is_stop: 是否停用
    #         :type is_stop: bool
    #         :param is_sale: 是否销售属性
    #         :type is_sale: bool
    #         """
    #
    #
    #
    #         self.Code = code
    #         # self.InventoryClass = self.InventoryClassInfo(inventory_class_code)
    #         # self.Disabled = is_stop
    #         # self.IsSale = is_sale


    class ParamDic:

        def __init__(self,code):

            self.PurchaseRequisition = self.PurchaseRequisitionInfo(code)

        class PurchaseRequisitionInfo:

            def __init__(self,code):
                """
                   存货查询条件实体初始化方法
                   :param code: 请购单单据编号
                   :type code: str
                """
                self.Code = code



# -*- coding: utf-8 -*-
# @Time    : 2025/1/11 09:24
# @Author  : kyrie_ice
# @Desc    : 查询请购单明细列表返回值实体
from chanjet_openapi_python_sdk.chanjet_response import ChanjetResponse

class QueryPurchaseRequistionResponse(ChanjetResponse):

       def __init__(self, data=None):

        self.PurchaseRequisition = self.PurchaseRequisitionInfo()

        if data:
            self.__dict__ = data

    class PurchaseRequisitionInfo:

        def __init__(self):

            #单据编号
            self.Code = ''
            # 单据日期
            self.VoucherDate = '1900-01-01'
            # 请购人
            self.RequisitionPerson = self.RequisitionPersonInfo()
            # 建议供应商
            self.SuggPartner = self.SuggPartnerInfo()
            # 需求日期
            self.RequireDate = '1900-01-01'
            # 单据状态
            self.VoucherState = self.VoucherStateInfo()
            # 备注
            self.Memo = ''
            # 制单人
            self.Maker = ''

        class RequisitionPersonInfo:

            def __init__(self):
                # 请购人id
                self.ID = ''
                # 请购人编码
                self.Code = ''
                # 请购人名称
                self.Name = ''

        class SuggPartnerInfo:

            def __init__(self):
                # 建议供应商id
                self.ID = ''
                # 建议供应商编码
                self.Code = ''
                # 建议供应商名称
                self.Name = ''
                # 建议供应商别名
                self.PartnerAbbName = ''

        class VoucherStateInfo:

            def __init__(self):
                # 单据状态id
                self.ID = ''
                # 单据状态编码
                self.Code = ''
                # 单据状态名称
                self.Name = ''

    def __str__(self):
        return str(self.__dict__)

  • 写回答

2条回答 默认 最新

  • 关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    根据您提供的代码和错误信息,问题可能出在请求的服务名称上。在调用API时,您需要确保使用的服务名称是正确的。根据您提供的错误信息,您使用的服务名称是PurchaseRequisitionopenApi/FindVoucherList,但是根据常见的命名规范,这个服务名称可能应该是PurchaseRequisition.FindVoucherList

    请尝试将您的请求中的服务名称更改为正确的名称,然后再试一次。如果仍然遇到问题,请确保您已正确配置了所有的请求头和请求内容,包括必要的参数和授权信息。此外,还要确保您正在使用正确的API版本和正确的URL路径。您可以参考官方文档或联系API提供商以获取更多关于如何正确使用API的信息。

    评论

报告相同问题?

问题事件

  • 修改了问题 1月13日
  • 修改了问题 1月13日
  • 修改了问题 1月13日
  • 创建了问题 1月13日