net8获取泛微token以及访问api示例

工作中涉及到调用泛微的场景,官方的示例又臭又长,抽空用NET8简化了写法,为了简化http访问,用了Flurl.Http这个库。在座各位大佬,我们直接就看代码了
 
using System.Security.Cryptography;
using System.Text.Json;
using Flurl.Http;

//请按照官方内容做好初始化工作 https://e-cloudstore.com/doc.html?appId=af09c25938714c26b9736f535ca20fc9
//后端接口清单 https://e-cloudstore.com/ec/api/applist/index.html#/

// 可以改写为扩展函数方便使用
var RSAEncrypt = (string spk, string payload) =>
{
    using var rsa = RSA.Create();
    rsa.ImportSubjectPublicKeyInfo(Convert.FromBase64String(spk), out _);
    var PlainTextBArray  = System.Text.Encoding.UTF8.GetBytes(payload);
    var CypherTextBArray = rsa.Encrypt(PlainTextBArray, RSAEncryptionPadding.Pkcs1);
    var Result = Convert.ToBase64String(CypherTextBArray);
    return Result;
};

var appid = "42520974-83D6-41F6-8773-0780A6EAB3D6";

// 1、注册仅需要进行一次
using var LocalRSA = RSA.Create();
var publicPem = LocalRSA.ExportRSAPublicKeyPem();
var privatePem = LocalRSA.ExportRSAPrivateKeyPem();
var registerDto = await "http://127.0.0.1/api/ec/dev/auth/regist"
    .WithHeader("appid", appid)
    .WithHeader("cpk", publicPem)
    .PostAsync()
    .ReceiveJson<JsonDocument>();
System.Console.WriteLine(registerDto.RootElement.GetRawText());
System.Console.WriteLine();

// 2、获取secrit,spk,执行第1步后保存在ECOLOGY_BIZ_EC表中,分别对应字段SECRIT,server_public_key
var secrit = registerDto.RootElement.GetProperty("secrit").GetString();
var spk = registerDto.RootElement.GetProperty("spk").GetString();
System.Console.WriteLine(secrit);
System.Console.WriteLine(spk);
System.Console.WriteLine();

// 3、获取access_token
var encryptSecret = RSAEncrypt(spk, secrit);
var applytokenDto = await "http://127.0.0.1/api/ec/dev/auth/applytoken"
    .WithHeader("appid", appid)
    .WithHeader("secret", encryptSecret)
    .PostAsync()
    .ReceiveJson<JsonDocument>();

var token = applytokenDto.RootElement.GetProperty("token").GetString();
System.Console.WriteLine(token);
System.Console.WriteLine();


// 4、获取流程
var userId = "1";
var encryptUserId = RSAEncrypt(spk, userId);
var getAllWorkflowRequestListDto = await "http://127.0.0.1/api/workflow/paService/getAllWorkflowRequestList"
    .WithHeader("appid", appid)
    .WithHeader("token", token)
    .WithHeader("userId", encryptUserId)
    .PostAsync()
    .ReceiveJson<JsonDocument>();
System.Console.WriteLine(getAllWorkflowRequestListDto.RootElement.GetRawText());
System.Console.WriteLine();

 

posted @ 2023-12-15 17:41  i没昵称  阅读(345)  评论(0编辑  收藏  举报