BaiduApiHelper.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using RestSharp;
  2. namespace OASystem.API.OAMethodLib.BaiduApi
  3. {
  4. public class BaiduApiHelper
  5. {
  6. const string API_KEY = "0OuunXiLEX3OL6hB4bhXvKyS";
  7. const string SECRET_KEY = "GZ6HwIGxQlGH1O4MADm0DpO5BcLyhpx2";
  8. public static string _apiTableV2(string Base64)
  9. {
  10. var client = new RestClient($"https://aip.baidubce.com/rest/2.0/ocr/v1/table?access_token={GetAccessToken()}");
  11. client.Timeout = -1;
  12. var request = new RestRequest(Method.POST);
  13. request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
  14. request.AddHeader("Accept", "application/json");
  15. // image 可以通过 GetFileBase64Content('C:\fakepath\tb.jpg') 方法获取
  16. request.AddParameter("image", Base64);
  17. IRestResponse response = client.Execute(request);
  18. return response.Content;
  19. }
  20. /**
  21. * 使用 AK,SK 生成鉴权签名(Access Token)
  22. * @return 鉴权签名信息(Access Token)
  23. */
  24. static string GetAccessToken()
  25. {
  26. var client = new RestClient($"https://aip.baidubce.com/oauth/2.0/token");
  27. //client.Timeout = -1;
  28. var request = new RestRequest("", Method.POST);
  29. request.AddParameter("grant_type", "client_credentials");
  30. request.AddParameter("client_id", API_KEY);
  31. request.AddParameter("client_secret", SECRET_KEY);
  32. IRestResponse response = client.Execute(request);
  33. Console.WriteLine(response.Content);
  34. var result = JsonConvert.DeserializeObject<dynamic>(response.Content);
  35. return result.access_token.ToString();
  36. }
  37. }
  38. }