Hello, I have created an API in ASP.NET Framework 4.5, and an html page using ajax communicating with the API. My problem is that I don't know how to communicate with Spotify to get the login page and display it to the user, knowing that the html page is independent (not in the solution)
C# code that allows to retrieve a token :
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://accounts.spotify.com/api/authorize");
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Accept = "application/json";
var request = ("grant_type=client_credentials&client_id=" + ClientId + "&client_secret=" + ClientSecret+ "&scope=playlist-read-private");
byte[] reqBytes = Encoding.ASCII.GetBytes(request);
webRequest.ContentLength = reqBytes.Length;
Stream strm = webRequest.GetRequestStream();
strm.Write(reqBytes, 0, reqBytes.Length);
strm.Close();
try
{
HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
String Result = "";
using (Stream respStr = resp.GetResponseStream())
{
using (StreamReader rdr = new StreamReader(respStr, Encoding.UTF8))
{
//retourne un string
Result = rdr.ReadToEnd();
rdr.Close();
JsonValue Json = JsonValue.Parse(Result);
_instance._accessToken = Json["access_token"];
_instance._expiresIn = Json["expires_in"];
_instance._tokenType = Json["token_type"];
}
}
}
JQuery AJAX :
$("#connection").click(function()
{
var Data = { Name: "GetUserPlaylist" };
$.ajax({
url: 'https://localhost:44376/SpotifyDrag_Drop/Index',
type: 'POST',
contentType: 'application/json; charset=UTF-8',
crossDomain: true,
dataType: 'JSON',
data: JSON.stringify(Data),
success: function(response)
{
console.log("conneciton sucess");
viewPlayList(response);
},
error: function(xhr)
{
console.log("Error: " + xhr.statusText);
}
});
});
Plan
Premium
Country
France
Device
PC
Operating System
Windows 10
Okay I change some paramerts :
var plainTextBytes = System.Convert.FromBase64String(ClientId);
string base64 = System.Text.Encoding.UTF8.GetString(plainTextBytes);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://accounts.spotify.com/authorize?" +
"client_id=" + ClientId + "&response_type=code&redirect_uri=http://spotifydragdrop&scope=playlist-read-private");
webRequest.Method = "GET";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Accept = "*/*";
Now i receive and html page :
| Result | "\n<!DOCTYPE html>\n<html id=\"app\" lang=\"fr\" dir=\"ltr\" ng-csp ng-strict-di>\n<head>\n <meta charset=\"utf-8\">\n <title ng-bind=\"('loginTitle' | localize) + ' - Spotify'\">Spotify</title>\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\">\n <base href=\"/\">\n <link rel=\"icon\" href=\"https://accounts.scdn.co/sso/images/favicon.ace4d8543bbb017893402a1e9d1ac1fa.ico\">\n <link href=\"https://accounts.scdn.co/sso/css/index.7f5ebce98590aa1e70ff.css\" media=\"screen\" rel=\"stylesheet\">\n\n <script defer src=\"https://accounts.scdn.co/sso/js/index.7f5ebce98590aa1e70ff.js\" sp-bootstrap></script>\n <meta ng-non-bindable sp-bootstrap-data='{\"phoneFeatureEnabled\":false,\"previewEnabled\":false,\"user\":false,\"tpaState\":\"AQD+OgdYnOEkdSlhAICFXJmGmNPccw0clGNjRXSfDoYkukertPlUhCluiz+fHJZJ6iglgGqPnQ7US0ZZvlMmKOUiA71ZfNTUUDtE5sYVS0adYlFPT3FcVFv4R4vbvJDuPD4dEC/o22uy/iRYAIMZRSCNFpwzeY7eyOld0Mc8KamS7pcmrMXcV44N/6b4p4Sh8fW/dIDNloji2f1VdLS6KtZc1rezDC/2AKngdTYCWwdQlh3e4+6kf5ZphA4EkDWqMVnUxIgJnb9yh2kid19Qt8umTlPDRtXhimQwlZrP7LD866Ls+cNwXG/nA/ck8oBokvrB2jD1bqRXZNZ1m2DbVYtOtDqXZAejBc+A5NtgxyFWpq+Vld411rvYTqbjOehDCuhWy7h0mwkJ13icBc6XrnjodhDw4Kc5GBXMnzVvdCaw\",\"BON\":[\"0\",\"0\",-1482864988]}'>\n</head>\n<body ng-controller=\"LoginController\">\n <div ng-include=\"template\"></div>\n</body>\n</html>\n" | string |
By the way, I have to know that I am in a singleton class so I return a token object with this method, so I don't see how to return the html page ?
With an await ?