Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
6002CEM_ObadaNasr/Services/SpotifyService.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
38 lines (26 sloc)
1.18 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
using System.Text.Json; | |
using app.Models; | |
namespace app.Services; | |
public class SpotifyService : ISpotifyService { | |
private string accessToken; | |
public SpotifyService() { | |
} | |
public async Task<bool> Initialize(string authCode) { | |
var bytes = Encoding.UTF8.GetBytes($"{Constants.SpotifyClientId}:{Constants.SpotifyClientSecret}"); | |
var authHeader = Convert.ToBase64String(bytes); | |
var client = new HttpClient(); | |
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", authHeader); | |
var content = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>() { | |
new("code", authCode), | |
new("redirect_uri", Constants.RedirectUrl), | |
new("grant_type", "authorization_code") | |
}); | |
var response = await client.PostAsync("https://accounts.spotify.com/api/token", content); | |
var json = await response.Content.ReadAsStringAsync(); | |
//deserializing token | |
var result = JsonSerializer.Deserialize<AuthResult>(json); | |
accessToken = result.AccessToken; | |
return response.IsSuccessStatusCode; | |
} | |
} | |