PouleR / spotify-artists-api

Spotify Artists API
4 stars 0 forks source link

Documentation for setup #3

Open bholtbholt opened 2 years ago

bholtbholt commented 2 years ago

Hello! I've got the API request successfully working because it returns a response with empty data. The trouble is, I can’t seem to get it to return the data I'm looking for.

I think I haven’t successfully authenticated the account because we don’t have a token, but don’t know where to get the token from. So far, I’ve tried using the cookie from the web login, and I’ve set up an API app with a ClientId and DeviceId. Neither appear to be working, but there’s no error, just an empty array.

It’s possible the artist or track ids aren’t valid, but again, it returns an empty array. We’ve tried using artist + track Id combos from the Spotify console as well as combos copied from the Artist dashboard.

I'm hoping to get some clearer documentation or guidance to get this working. Thanks in advance!

PouleR commented 2 years ago

Hi,

Do you have access to Spotify 4 Artists when you log in on https://artists.spotify.com ? That's a prerequisite before you want to use the API.

bholtbholt commented 2 years ago

Thanks for the response! Yep, I've got access to Spotify 4 Artists and have tried the Artist + Track Ids that I have access to, though I'm not 100% confident they're the correct Ids. I've taken it from the URL:

PouleR commented 2 years ago

Thanks for sharing!

Since you do have access to S4A, can you see if there's data returned when you log in on the Spotify CMS? Since I can't reproduce it as I don't have access to that artist details.

bholtbholt commented 2 years ago

Is this the Spotify CMS? cms.spotify.com? Maybe code here, it token in the script?

Screen Shot 2022-05-24 at 2 09 49 PM
$spotifyApi->setAccessToken('token');

In the README.md example, this token is main piece I think I'm missing. I'm not sure what the token is or how to get it.

Appreciate your responses and help!

PouleR commented 2 years ago

I meant the artists.spotify.com with Spotify CMS, excuse me for the confusion.

After reading the README, I noticed there's indeed some information missing on how to obtain an accessToken. Which makes your question more clear for me.

Can you please use the updated README to give it a try?

bholtbholt commented 2 years ago

I think we're getting closer. I'm getting an error like:

~/Spotify-API$ php main.php 3LDW8l1U9CKmwfCOTXPXi9 1jngqCcYYXb6wdGeAcpzyA

Fatal error: Uncaught PouleR\SpotifyLogin\Exception\SpotifyLoginException: Login request: Response error: BAD_REQUEST in /home/runner/Spotify-API/vendor/pouler/spotify-login/src/SpotifyLoginClient.php:61
Stack trace:
#0 /home/runner/Spotify-API/vendor/pouler/spotify-login/src/SpotifyLogin.php(115): PouleR\SpotifyLogin\SpotifyLoginClient->loginRequest(Object(Spotify\Login5\V3\LoginRequest))
#1 /home/runner/Spotify-API/main.php(24): PouleR\SpotifyLogin\SpotifyLogin->login('xxxx...', 'xxxxx')
#2 {main}
  thrown in /home/runner/Spotify-API/vendor/pouler/spotify-login/src/SpotifyLoginClient.php on line 61

(I've obscured the email password in the response, but it's there)

And my script, following the README.md instructions looks like:

<?php declare(strict_types=1);

// validate artistId and trackId
$artistId = &$argv[1];
$trackId = &$argv[2];
if (is_null($artistId) || is_null($trackId)) {
  exit("You must include an ArtistId and TrackId like\n\n  > php main.php 6gmzMcAvRiQNYg6 11dFghVlKsNCbNl\n\n");
}

require 'vendor/autoload.php';

// Load PHP Scripts
$httpClient = new \Symfony\Component\HttpClient\CurlHttpClient();
$apiClient = new \PouleR\SpotifyArtistsAPI\SpotifyArtistsAPIClient($httpClient);
$artistsApi = new \PouleR\SpotifyArtistsAPI\SpotifyArtistsAPI($apiClient);

$loginClient = new \PouleR\SpotifyLogin\SpotifyLoginClient($httpClient);
$spotifyLogin = new \PouleR\SpotifyLogin\SpotifyLogin($loginClient);

// Log in to the OAuth App
$spotifyLogin->setClientId(getenv('clientId'));
$spotifyLogin->setDeviceId(getenv('deviceId'));
// Log in and get the access token
$accessToken = $spotifyLogin->login(getenv('email'),getenv('password'));
// Use this token for the artists API
$artistsApi->setAccessToken($accessToken);

// Get Data
$upcoming = $spotifyApi->getUpcomingReleases($artistId);
print("Upcoming Data:\n");
print_r($upcoming);

// Realtime Data
$realtime = $spotifyApi->getRealTimeStatistics($artistId, $trackId);
print("\nRealtime Data:\n");
print_r($realtime);

I'm using ENV variables because it's a shared script, but all the inputs are working as expected -- though I'm not 100% confident they're the right inputs.

  1. Are ClientId and DeviceId from developers.spotify.com? deviceId doesn't match to anything, so I thought it might be Client Secret. (see screenshot below)
  2. Email + Password are my regular Spotify email + password combination, right? As if I was logging in manually?
Screen Shot 2022-05-25 at 9 36 20 AM
PouleR commented 2 years ago

Hi,

The ClientID and DeviceID are taken from sniffing the network traffic on the Spotify4Artists app. I found out that the deviceId can be left blank, but the ClientId should be taken from there. Since I can't share mine you need to retrieve this ClientID yourself. It's indeed not compatible with the ClientID from developers.spotify.com since it's not a public API resource I'm using in this repository.

So with a bit of reverse engineering of the app (or maybe website of artists.spotify.com?) you can find the correct ID to make sure you can use the API.

bholtbholt commented 2 years ago

Hi,

Did some sleuthing on the network but am still having trouble. To confirm, is this the Spotify4Artists app you mean?

I've been looking through the network on artists.spotify.com, but haven't found a working ID yet either. Is there a particular key or label you found it under? I've been looking through cookies and query params in both responses and requests. A couple look like "client ids", but it's tough to confirm. Attached a screenshot for reference.

Screen Shot 2022-06-04 at 8 56 14 PM

Appreciate your help thus far!

PouleR commented 2 years ago

You're correct, it's the Spotify4Arists app; to retrieve the clientId & deviceId I setup an SSL-proxy (by using Charles) on my laptop and made sure this proxy is used on my phone.

When opening the S4A app and logging in; you can trace all network traffic from the app to Spotify that happens on your phone. By intercepting these requests you can find the deviceId. Spotify is using Google Protobuf for these requests and you can view them in Charles SSL proxy.

gianlucariccobono commented 1 year ago

Related: https://github.com/PouleR/spotify-login/issues/4#issuecomment-1646663943