C# 调用百度API批量识别发票,并存到EXCEL

C# 调用百度API批量识别发票,并存到EXCEL

![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/f406f2185184456daa9ba7829c26138d.png#pic_center)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using RestSharp;//依赖版本106.15.0 https://www.nuget.org/packages/RestSharp/106.15.0
using Newtonsoft.Json;
using IniParser;
using IniParser.Model; //https://www.nuget.org/packages/Newtonsoft.Json

namespace 发票识别
{
    public class rest
    {


        public string str1;
        public string str2;
        private void sstname()
        {

            var parser = new FileIniDataParser();
            string iniFilePath = "config.ini";
            try
            {
                IniData data = parser.ReadFile(iniFilePath);
                string sectionName = "Apidate"; // 替换为你的节名
                string keyName = "AKEY"; // 替换为你的键名
                string keysName = "SKEY"; // 替换为你的键名
                // 获取特定节和键的值
                string value = data[sectionName][keyName];
                string value1 = data[sectionName][keysName];
                str1 = value;
                str2 = value1;

            }
            catch (Exception ex)
            {
                //Console.WriteLine("Error reading INI file: " + ex.Message);
            }

        }

        //string API_KEY = str1.ToString();
        //string SECRET_KEY = str2;
        public static string StrPanth(string pathname)
        {
            var client = new RestClient($"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice?access_token={GetAccessToken()}");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            request.AddHeader("Accept", "application/json");
            // pdf_file 可以通过 GetFileBase64Content('C:\fakepath\2491816599   00003.PDF') 方法获取
            var str = GetFileContentAsBase64(pathname);
            request.AddParameter("pdf_file", str);
            request.AddParameter("seal_tag", "false");
            IRestResponse response = client.Execute(request);
            Thread.Sleep(200);
            return response.Content;
            //return result;

        }
        static string GetAccessToken()
        {
            var parser = new FileIniDataParser();
            string iniFilePath = "config.ini";

            IniData data = parser.ReadFile(iniFilePath);
            string sectionName = "Apidate"; // 替换为你的节名
            string keyName = "AKEY"; // 替换为你的键名
            string keysName = "SKEY"; // 替换为你的键名
                                      // 获取特定节和键的值
            string value = data[sectionName][keyName];
            string value1 = data[sectionName][keysName];

            var client = new RestClient($"https://aip.baidubce.com/oauth/2.0/token");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddParameter("grant_type", "client_credentials");
            request.AddParameter("client_id", value);
            request.AddParameter("client_secret", value1);
            IRestResponse response = client.Execute(request);
            //Console.WriteLine(response.Content);
            var result = JsonConvert.DeserializeObject<dynamic>(response.Content);
            return result.access_token.ToString();




        }
        static string GetFileContentAsBase64(string path)
        {
            using (FileStream filestream = new FileStream(path, FileMode.Open))
            {
                byte[] arr = new byte[filestream.Length];
                filestream.Read(arr, 0, (int)filestream.Length);
                string base64 = Convert.ToBase64String(arr);
                return base64;
            }
        }
    }
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码猩

如果可以请支持我一下哟!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值