Announcements

Help Wizard

Step 1

NEXT STEP

C# - /me/player/play "PREMIUM_REQUIRED" - Logged in with Token

C# - /me/player/play "PREMIUM_REQUIRED" - Logged in with Token

Plan

Family/Premium

Country

Germany

Device

PC

Operating System

Windows 10

 

My Question or Issue

 

Hi. I try to build a litte C# Script to play a Song with the Spotify-API.

I am logged in as a member of an family premium account and i have a login-token to call the play-function.

First i try to login with my credentials. It works:

using (HttpClient client = new HttpClient())
{
var requestBody = new Dictionary<string, string>
{
{ "grant_type", "client_credentials" },
{ "client_id", ClientId },
{ "client_secret", ClientSecret }
};
HttpResponseMessage response = await client.PostAsync("https://accounts.spotify.com/api/token", new FormUrlEncodedContent(requestBody));
...

I get a response and my token to use:
response StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
x-request-id: bbed5d37-fd21-4c0a-9d50-069361731a7a
Date: Fri, 07 Mar 2025 13:10:13 GMT
Set-Cookie: __Host-device_id=AQBpWurC_uDbieVOarRxOjkXr6c7PleheCyA0QK24uM8TfykOwTQMQfFekTKyjC44evsbl_npBDZLq9zyCiukPHYoMmQr0YFvSw;Version=1;Path=/;Max-Age=2147483647;Secure;HttpOnly;SameSite=Lax
Set-Cookie: sp_tr=false;Version=1;Domain=accounts.spotify.com;Path=/;Secure;SameSite=Lax
sp-trace-id: 974fa9d9291344e6
x-envoy-upstream-service-time: 6
server: envoy
strict-transport-security: max-age=31536000
x-content-type-options: nosniff
vary: Accept-Encoding
Via: HTTP/2 edgeproxy
Via: 1.1 google
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
Content-Length: 198
Content-Type: application/json
}


Next i try to make a search with my token and it works fine:

using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", AccessToken);

string encodedQuery = Uri.EscapeDataString(query);
var response = await client.GetAsync($"{ApiBaseUrl}/search?q={encodedQuery}&type=track&limit=5");
...

search response StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
x-robots-tag: noindex, nofollow
access-control-allow-origin: *
access-control-allow-headers: Accept, App-Platform, Authorization, Content-Type, Origin, Retry-After, Spotify-App-Version, X-Cloud-Trace-Context, client-token, content-access-token
access-control-allow-methods: GET, POST, OPTIONS, PUT, DELETE, PATCH
access-control-allow-credentials: true
access-control-max-age: 604800
strict-transport-security: max-age=31536000
x-content-type-options: nosniff
vary: Accept-Encoding
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
Cache-Control: public, max-age=0
Date: Fri, 07 Mar 2025 13:11:20 GMT
ETag: "MC-IjQ5YjJmZGJlZjZhNTMwZWVkNWY3ZWUwOGI2MjQ0NTlmIg=="
Server: envoy
Via: HTTP/2 edgeproxy
Via: 1.1 google
Content-Length: 16987
Content-Type: application/json; charset=utf-8
}


Then i try to play an existing song and get an 403-error:

using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", AccessToken);

var requestData = new
{
uris = new[] { trackUri }
};

var json = System.Text.Json.JsonSerializer.Serialize(requestData);
var content = new StringContent(json, Encoding.UTF8, "application/json");

var response = await client.PutAsync($"{ApiBaseUrl}/me/player/play", content);
...

play response StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
x-robots-tag: noindex, nofollow
access-control-allow-origin: *
access-control-allow-headers: Accept, App-Platform, Authorization, Content-Type, Origin, Retry-After, Spotify-App-Version, X-Cloud-Trace-Context, client-token, content-access-token
access-control-allow-methods: GET, POST, OPTIONS, PUT, DELETE, PATCH
access-control-allow-credentials: true
access-control-max-age: 604800
strict-transport-security: max-age=31536000
x-content-type-options: nosniff
vary: Accept-Encoding
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
Cache-Control: max-age=0, private
Date: Fri, 07 Mar 2025 13:14:59 GMT
Server: envoy
Via: HTTP/2 edgeproxy
Via: 1.1 google
Content-Length: 134
Content-Type: application/json; charset=utf-8
}


Why do i get this error? I am logged in with a token from a family-premium user.

Thanks for your help.

Andreas

Reply
2 Replies

I posted this on friday and was marked as possible Spam till today. I hope somebody can help.

closed. i found my copy&paste problem

Suggested posts

Let's introduce ourselves!

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…

ModeratorStaff / Moderator/ 4 years ago  in Social & Random