CoreAPIHttpService.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Http;
  5. using System.Net.Http.Json;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using TEAMModelOS.SDK.Extension;
  9. namespace TEAMModelOS.SDK.Models.Service
  10. {
  11. public class CoreAPIHttpService
  12. {
  13. private readonly HttpClient _httpClient;
  14. public CoreAPIHttpService(HttpClient httpClient)
  15. {
  16. _httpClient = httpClient;
  17. }
  18. /// <summary>
  19. /// 隐式登录
  20. /// </summary>
  21. /// <param name="clientID"></param>
  22. /// <param name="clientSecret"></param>
  23. /// <param name="location"></param>
  24. /// <param name="url"></param>
  25. /// <param name="data"></param>
  26. /// <returns></returns>
  27. public async Task<(int code ,string content)> Implicit(string clientID, string clientSecret, string location, string url,Dictionary<string,string> data)
  28. {
  29. if (location.Contains("China"))
  30. {
  31. location = "China";
  32. }
  33. else if (location.Contains("Global"))
  34. {
  35. location = "Global";
  36. }
  37. var token = await CoreTokenExtensions.CreateAccessToken(clientID, clientSecret, location);
  38. _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.AccessToken}");
  39. HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync(url, data);
  40. if (responseMessage.StatusCode == HttpStatusCode.OK)
  41. {
  42. string content=await responseMessage.Content.ReadAsStringAsync();
  43. return (200,content);
  44. }
  45. else if (responseMessage.StatusCode == HttpStatusCode.Unauthorized)
  46. {
  47. return (401,null);
  48. }
  49. else
  50. {
  51. return (500,null);
  52. }
  53. }
  54. }
  55. }