Type in your question below and we'll check to see what answers we can find...
Loading article...
Submitting...
If you couldn't find any answers in the previous step then we need to post your question in the community and wait for someone to respond. You'll be notified when that happens.
Simply add some detail to your question and refine the title if needed, choose the relevant category, then post.
Before we can post your question we need you to quickly make an account (or sign in if you already have one).
Don't worry - it's quick and painless! Just click below, and once you're logged in we'll bring you right back here and post your question. We'll remember what you've already typed in so you won't have to do it again.
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 ?
Hi,
I don't think you can do this part on the server side.
You should open the webpage `https://accounts.spotify.com/api/authorize?client_id=ClientId&response_type=code&redirect_uri=http:/...` and let the user confirm or enter his credentials.
You will then receive the code in the query params of the callback uri that you sent to spotify. So in your web app (http://spotifydragdrop/login) you have to listen to the query params:
document.addEventListener('DOMContentLoaded', function (event) {
console.log('loaded');
const urlParams = new URLSearchParams(location.search);
const code = urlParams.get('code') || null;
// now you have to exchange the code for an access token
});
Then you exchange this code for a access token with your .NET backend.
Hope this helps.
Hey there you, Yeah, you! 😁 Welcome - we're glad you joined the Spotify Community! While you here, let's have a fun game and get…