-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpotifyController.cs
137 lines (106 loc) · 4.79 KB
/
SpotifyController.cs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace SpotifyTokenSwap.Controllers
{
public class SpotifyController : Controller
{
string AUTH_HEADER = "Basic " + Convert.ToBase64String(
Encoding.UTF8.GetBytes(
ConfigurationManager.AppSettings["clientId"] + ":" +
ConfigurationManager.AppSettings["clientSecret"]));
[HttpPost]
public ActionResult swap()
{
try
{
Stream reqBodyStream = Request.InputStream;
reqBodyStream.Seek(0, System.IO.SeekOrigin.Begin);
string reqBody = new StreamReader(reqBodyStream).ReadToEnd();
if (String.IsNullOrWhiteSpace(reqBody))
{
return new HttpStatusCodeResult(550, "unsufficent or deficent request parameters");
}
Dictionary<string,string> kv = reqBody.Split(' ')
.Select(x => x.Split('='))
.Where(x => x.Length == 2)
.ToDictionary(x => x.First(), x => x.Last());
var postData = "grant_type=authorization_code";
postData += "&redirect_uri=" + ConfigurationManager.AppSettings["callbackUri"];
postData += "&code=" + kv["code"];
var data = Encoding.ASCII.GetBytes(postData);
WebRequest webRequest = WebRequest.Create("https://accounts.spotify.com/api/token");
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Headers.Set("Authorization", AUTH_HEADER);
webRequest.ContentLength = data.Length;
using (var resBodyStream = webRequest.GetRequestStream())
{
resBodyStream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
if (response.StatusCode != HttpStatusCode.OK)
{
return new HttpStatusCodeResult(550, "Error came from spotify");
}
string resBody = new StreamReader(response.GetResponseStream()).ReadToEnd();
return Content(resBody, "application/json");
}
catch (Exception ex)
{
return new HttpStatusCodeResult(550, "Internal Error");
}
}
[HttpPost]
public ActionResult refresh()
{
try
{
Stream reqBodyStream = Request.InputStream;
reqBodyStream.Seek(0, System.IO.SeekOrigin.Begin);
string reqBody = new StreamReader(reqBodyStream).ReadToEnd();
if (String.IsNullOrWhiteSpace(reqBody))
{
return new HttpStatusCodeResult(550, "unsufficent or deficent request parameters");
}
Dictionary<string, string> kv = reqBody.Split(' ')
.Select(x => x.Split('='))
.Where(x => x.Length == 2)
.ToDictionary(x => x.First(), x => x.Last());
var postData = "grant_type=refresh_token";
postData += "&refresh_token=" + kv["refresh_token"];
var data = Encoding.ASCII.GetBytes(postData);
WebRequest webRequest = WebRequest.Create("https://accounts.spotify.com/api/token");
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Headers.Set("Authorization", AUTH_HEADER);
webRequest.ContentLength = data.Length;
using (var resBodyStream = webRequest.GetRequestStream())
{
resBodyStream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
if (response.StatusCode != HttpStatusCode.OK)
{
return new HttpStatusCodeResult(550, "Error came from spotify");
}
string resBody = new StreamReader(response.GetResponseStream()).ReadToEnd();
return Content(resBody, "application/json");
}
catch (Exception ex)
{
return new HttpStatusCodeResult(550, "Internal Error");
}
}
public ActionResult test()
{
return Content("Test");
}
}
}