Skip to content

Instantly share code, notes, and snippets.

@bugproof
Last active February 6, 2023 00:52
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bugproof/667709f83f0ed27c337f62bbd34ce9bd to your computer and use it in GitHub Desktop.
Save bugproof/667709f83f0ed27c337f62bbd34ce9bd to your computer and use it in GitHub Desktop.
Reverse engineered spotify realtime player notifications https://user-images.githubusercontent.com/3116731/60773452-3ace8700-a106-11e9-9092-782aab26626a.png Works only with discord and official spotify access token (capture with chrome or firefox DevTools)
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Net.WebSockets;
using Websocket.Client;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
namespace SpotifyPlayerState
{
class SpotifyResponse
{
public Dictionary<string, object> Headers { get; set; }
public string Type { get; set; }
public string Uri { get; set; }
}
class Program
{
static async Task Main(string[] args)
{
// go to discord web and capture https://discordapp.com/api/v6/users/@me/connections/spotify/XXX/access-token
// or go to open.spotify.com and capture the token there
var accessToken = "ACCESS TOKEN FROM OFFICIAL CLIENT OR DISCORD";
var url = new Uri($"wss://dealer.spotify.com/?access_token={accessToken}");
using var client = new WebsocketClient(url)
{
ReconnectTimeoutMs = 30000
};
client.ReconnectionHappened.Subscribe(type =>
Console.WriteLine($"Reconnection happened, type: {type}"));
client.MessageReceived.Subscribe(async msg =>
{
if (msg.MessageType != WebSocketMessageType.Text) return;
Console.WriteLine($"Message received {msg.Text}");
var parsed = JsonConvert.DeserializeObject<SpotifyResponse>(msg.Text);
if (parsed.Type == "message" && parsed.Headers.TryGetValue("Spotify-Connection-Id", out var spotifyConnectionId))
{
using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var requestUri = $"https://api.spotify.com/v1/me/notifications/player?connection_id={spotifyConnectionId}";
var r = await httpClient.PutAsync(requestUri, null);
if (r.IsSuccessStatusCode)
{
Console.WriteLine("Enabled player notifications for this connection");
}
else
{
Console.WriteLine("eh" + await r.Content.ReadAsStringAsync());
}
}
});
await client.Start();
while (true)
{
await Task.Delay(30000);
await client.Send("{\"type\":\"ping\"}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment