Blazor & WASM in combination to get statistics from Spotify API for performing the song analysis. With separate microservices for auth, Spotify, user data tracking, and application, connected through gRPC with Polly.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AuthService.cs 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //using IdentityProvider.Protos.AuthService;
  2. using Grpc.Net.Client;
  3. using GrpcShared;
  4. using GrpcShared.DTO.Auth;
  5. using GrpcShared.Interfaces;
  6. using Microsoft.Extensions.Options;
  7. using Microsoft.Net.Http.Headers;
  8. using Newtonsoft.Json;
  9. using System.Net.Http.Headers;
  10. using System.Text;
  11. using System.Text.Json;
  12. namespace IdentityProvider.Services
  13. {
  14. public class AuthService : IAuthService
  15. {
  16. private readonly ILogger<AuthService> _logger;
  17. private readonly CodeRequest _params;
  18. private readonly IHttpClientFactory _httpClientFactory;
  19. public AuthService(ILogger<AuthService> logger, IOptions<CodeRequest> options, IHttpClientFactory httpClientFactory)
  20. {
  21. _logger = logger;
  22. _params = options.Value;
  23. _httpClientFactory = httpClientFactory;
  24. }
  25. public async Task<TokenResponse> GetAccessToken(TokenRequest tokenRequest)
  26. {
  27. var http = _httpClientFactory.CreateClient();
  28. string url = "https://accounts.spotify.com/api/token";
  29. http.BaseAddress = new Uri(url);
  30. //get client id and secret, and redirect uri from appsettings, convert to base64 and set as header
  31. var secrets = await GetAuthParams();
  32. byte[] contentType = Encoding.UTF8.GetBytes($"{secrets.ClientId}:{secrets.ClientSecret}");
  33. tokenRequest.redirect_uri = secrets.RedirectURI;
  34. //AUTHORIZATION HEADER
  35. http.DefaultRequestHeaders.Add(HeaderNames.Authorization, "Basic " + Convert.ToBase64String(contentType));
  36. //ACCEPT HEADER
  37. http.DefaultRequestHeaders.Accept.Add(
  38. new MediaTypeWithQualityHeaderValue("application/json"));
  39. //BODY PARAMS
  40. var requestBody = new Dictionary<string, string>();
  41. requestBody["grant_type"] = tokenRequest.grant_type;
  42. requestBody["code"] = tokenRequest.code!;
  43. requestBody["redirect_uri"] = tokenRequest.redirect_uri!;
  44. //REQUEST
  45. var response = await http.PostAsync(url, new FormUrlEncodedContent(requestBody));
  46. var contents = JsonConvert.DeserializeObject<TokenResponse>(await response.Content.ReadAsStringAsync());
  47. return new TokenResponse
  48. {
  49. access_token = contents!.access_token,
  50. refresh_token = contents!.refresh_token,
  51. expires_in = contents!.expires_in
  52. };
  53. }
  54. public async Task<CodeRequest> GetAuthParams()
  55. {
  56. var authParams = new CodeRequest
  57. {
  58. ClientId = _params.ClientId,
  59. RedirectURI = _params.RedirectURI,
  60. Scope = _params.Scope,
  61. ClientSecret = _params.ClientSecret
  62. };
  63. return await Task.FromResult(authParams);
  64. }
  65. }
  66. }