forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientCredentialsAuth.cs
More file actions
52 lines (48 loc) · 1.62 KB
/
ClientCredentialsAuth.cs
File metadata and controls
52 lines (48 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using Newtonsoft.Json;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;
namespace SpotifyAPI.Web.Auth
{
public class ClientCredentialsAuth
{
public Scope Scope { get; set; }
public String ClientId { get; set; }
public String ClientSecret { get; set; }
/// <summary>
/// Starts the auth process and
/// </summary>
/// <returns>A new Token</returns>
public Token DoAuth()
{
using (WebClient wc = new WebClient())
{
wc.Proxy = null;
wc.Headers.Add("Authorization",
"Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientId + ":" + ClientSecret)));
NameValueCollection col = new NameValueCollection
{
{"grant_type", "client_credentials"},
{"scope", Scope.GetStringAttribute(" ")}
};
byte[] data;
try
{
data = wc.UploadValues("https://accounts.spotify.com/api/token", "POST", col);
}
catch (WebException e)
{
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
{
data = Encoding.UTF8.GetBytes(reader.ReadToEnd());
}
}
return JsonConvert.DeserializeObject<Token>(Encoding.UTF8.GetString(data));
}
}
}
}