如何获取用友ERP的动态Token?

2024-01-10 10:32:20

在以往的EDI项目案例中,实现EDI系统与用友ERP系统的集成,用友ERP系统对外提供动态Token,EDI系统应该如何获取呢?

获取动态token
创建 Script端口,获取动态Token

1.添加脚本

知行之桥EDI系统是一个标准化的低代码平台,如果遇到现有功能端口无法实现需求的情况,可以在Script端口的设置选项卡下添加脚本文件。对于安全性要求较高的ERP系统,会在其接口文档中注明要求的加签方式,这部分将会通过编写脚本来实现。

如果要获取动态Token,则需要在设置选项卡下添加脚本:

get_token1.png

<!-- NOTE: Do not edit arc:info -->
<arc:info title="Custom Script" desc="This script will be executed when a file is processed.">
  
 
<input name="ConnectorId"   desc="The id of this connector." />
  <input name="WorkspaceId"   desc="The workspace of this connector." />
  <input name="MessageId"     desc="The message id." />
  <input name="FilePath"      desc="The path of the file being processed." />
  <input name="FileName"      desc="The name of the file being processed." />
  <input name="Attachment#"   desc="The path of the attachment being processed." />
  <input name="Header:*"      desc="The message headers of the file being processed." />
  <output name="Data"         desc="The data that will be written to the file in the Receive folder." />
  <output name="Encoding"     desc="The encoding that will be used to write the file in the Receive folder." />
  <output name="FileName"     desc="The name of the output file in Receive folder." />
  <output name="FilePath"     desc="The path of file that will be written to Receive folder." />
  <output name="Header:*"     desc="The message headers of the file being written." />
</arc:info>
 
<rsb:set attr="code.format" value="HMAC" />
<rsb:set attr="code.hmackey" value="用友提供的hmackey" />
<rsb:set attr="code.hmacalgorithm" value="用友要求的算法类型" />
<rsb:set attr="code.hmacbits" value="用友要求的信息" />
<rsb:set attr="code.outformat" value="用友要求的编码方式" />
 
<rsb:set attr="time.stamp" value="[_| now | todate('UNIXMILIS')]"/>
 
<rsb:set attr="code.data" value="用友提供的appKey"/>
 
<rsb:call op="encEncode" in="code" out="encode">
  <rsb:set attr="sign.code" value="[encode.enc:encodeddata | urlencode()]" />
</rsb:call>
 
<!--调用ERP登录信息-->
<rsb:set attr="http.url" value="这里填写ERP提供的请求地址"/>
<rsb:call op="httpGet" in="http" out="out">
  <rsb:set attr="login.response" value="[out.http:content]"/>
 
 
<!--检查登录结果-->
  <rsb:set attr="login.successContent" value='"code":"00000"'/>
  <rsb:check value="[login.response | contains([login.successContent])]">
    <rsb:set attr="result.cookie" value="[login.response]"/>
    <rsb:else>
      <rsb:throw code="login.error" description="登陆ERP系统失败,请重试! 错误信息如下:[login.response]"/>
    </rsb:else>
  </rsb:check>
</rsb:call>
<rsb:set attr="In.file" value="D:\Token\Token.json"/>
<rsb:set attr="In.data" value="[login.response]"/>
<rsb:call op="fileCreate" in="In"/>

具体的脚本编写需要结合ERP系统提供的接口文档进行定制化的开发,在这里编写的脚本将会获取动态Token并放在指定文件夹(D:\Token\Token.json)中。

2.根据用友ERP提供的接口文档设置获取时间

调用接口令牌是应用调用开放平台业务接口的凭证,有效期为两个小时,需要在知行之桥EDI系统中,设置为每两个小时获取一次。

进入Script端口的自动化选项卡下,勾选接收,表示端口将会按照设置的计划自动运行脚本。接下来将执行间隔设置为Minute,下方的 分 设置为120,表示每2小时自动执行一次脚本。

获得动态token之后,EDI系统即可调用具体的业务接口,从ERP系统中获取具体的业务数据。

EDI系统向ERP系统中推送数据
创建一个Script端口,获取Token

在新创建的Script端口的设置选项卡下,添加如下脚本:

<!-- NOTE: Do not edit arc:info -->
<arc:info title="Custom Script" desc="This script will be executed when a file is processed.">
  
<input name="ConnectorId"   desc="The id of this connector." />
  <input name="WorkspaceId"   desc="The workspace of this connector." />
  <input name="MessageId"     desc="The message id." />
  <input name="FilePath"      desc="The path of the file being processed." />
  <input name="FileName"      desc="The name of the file being processed." />
  <input name="Attachment#"   desc="The path of the attachment being processed." />
  <input name="Header:*"      desc="The message headers of the file being processed." />
  <output name="Data"         desc="The data that will be written to the file in the Receive folder." />
  <output name="Encoding"     desc="The encoding that will be used to write the file in the Receive folder." />
  <output name="FileName"     desc="The name of the output file in Receive folder." />
  <output name="FilePath"     desc="The path of file that will be written to Receive folder." />
  <output name="Header:*"     desc="The message headers of the file being written." />
</arc:info>
 
<arc:set attr="In.file" value="D:\Token\Token.json"/>
<arc:call op="fileRead" in="In">
  <arc:set attr="jsonIn.uri" value="[In.file]"/>
  <arc:set attr="jsonIn.jsonpath" value="/json/data"/>
  <arc:call op="jsonDOMSearch" in="jsonIn">
    <arc:set attr="_message.header:token" value="[jsonpath('access_token')]"/>
  </arc:call>
</arc:call>
<arc:set attr="out.filepath" value="[FilePath]"/>
<arc:push item="out"/>

上述代码实现的主要功能是,获取之前存放在指定路径下的token,并将其写入文件的Header中。用户可以在此Script端口的输出选项卡下,查看消息的处理结果,点击文件即可预览消息内容,token将会被存放在其他消息头部中,效果如下所示:

get_token3.png

创建一个REST端口,推送订单数据

在REST端口的设置选项卡下,设置方法&URL。

get_token4.png

方法设置为 POST,URL设置为ERP接口文档中提供的订单请求地址。

接下来在高级设置选项卡下的高级设置中,勾选允许在 URL 中使用 ArcScript。

get_token5.png

以上便是获取动态token的全部内容了,由于不同ERP系统的具体需求可能会有差异,因此用户在参考上述内容时,如果遇到任何问题欢迎交流。

扩展阅读:EDI是什么?

文章来源:https://blog.csdn.net/Cara_EDI_Consultant/article/details/135495743
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。