Merge pull request #783 from chme/spotify_collaborative_playlists

Allow reading of collaborative/shared Spotify playlists
This commit is contained in:
Christian Meffert 2019-08-04 12:25:53 +02:00 committed by GitHub
commit 7ee9a47783
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 3 deletions

View File

@ -213,6 +213,7 @@
</div> </div>
<div v-show="spotify.webapi_token_valid"> <div v-show="spotify.webapi_token_valid">
<p><b>Spotify Web API</b>: access authorized for <b>{{ spotify.webapi_user }}</b></p> <p><b>Spotify Web API</b>: access authorized for <b>{{ spotify.webapi_user }}</b></p>
<p v-if="spotify_missing_scope.length > 0" class="help is-danger">Please reauthorize Web API access to grant forked-daapd the following additional access rights: <b>{{ spotify_missing_scope | join }}</b></p>
<a class="button" v-bind:href="spotify.oauth_uri">Reauthorize Web API access</a> <a class="button" v-bind:href="spotify.oauth_uri">Reauthorize Web API access</a>
</div> </div>
</div> </div>

View File

@ -24,6 +24,15 @@ var app = new Vue({
this.loadLastfm(); this.loadLastfm();
}, },
computed: {
spotify_missing_scope () {
if (this.spotify.webapi_token_valid && this.spotify.webapi_granted_scope && this.spotify.webapi_required_scope) {
return this.spotify.webapi_required_scope.split(' ').filter(scope => this.spotify.webapi_granted_scope.indexOf(scope) < 0)
}
return []
}
},
methods: { methods: {
loadConfig: function() { loadConfig: function() {
axios.get('/api/config').then(response => { axios.get('/api/config').then(response => {

View File

@ -850,6 +850,8 @@ jsonapi_reply_spotify(struct httpd_request *hreq)
json_object_object_add(jreply, "webapi_token_valid", json_object_new_boolean(webapi_info.token_valid)); json_object_object_add(jreply, "webapi_token_valid", json_object_new_boolean(webapi_info.token_valid));
safe_json_add_string(jreply, "webapi_user", webapi_info.user); safe_json_add_string(jreply, "webapi_user", webapi_info.user);
safe_json_add_string(jreply, "webapi_country", webapi_info.country); safe_json_add_string(jreply, "webapi_country", webapi_info.country);
safe_json_add_string(jreply, "webapi_granted_scope", webapi_info.granted_scope);
safe_json_add_string(jreply, "webapi_required_scope", webapi_info.required_scope);
spotifywebapi_access_token_get(&webapi_token); spotifywebapi_access_token_get(&webapi_token);
safe_json_add_string(jreply, "webapi_token", webapi_token.token); safe_json_add_string(jreply, "webapi_token", webapi_token.token);

View File

@ -96,6 +96,7 @@ struct spotify_playlist
// Credentials for the web api // Credentials for the web api
static char *spotify_access_token; static char *spotify_access_token;
static char *spotify_refresh_token; static char *spotify_refresh_token;
static char *spotify_granted_scope;
static char *spotify_user_country; static char *spotify_user_country;
static char *spotify_user; static char *spotify_user;
@ -118,8 +119,11 @@ static bool scanning;
// Endpoints and credentials for the web api // Endpoints and credentials for the web api
static const char *spotify_client_id = "0e684a5422384114a8ae7ac020f01789"; static const char *spotify_client_id = "0e684a5422384114a8ae7ac020f01789";
static const char *spotify_client_secret = "232af95f39014c9ba218285a5c11a239"; static const char *spotify_client_secret = "232af95f39014c9ba218285a5c11a239";
static const char *spotify_scope = "playlist-read-private playlist-read-collaborative user-library-read user-read-private";
static const char *spotify_auth_uri = "https://accounts.spotify.com/authorize"; static const char *spotify_auth_uri = "https://accounts.spotify.com/authorize";
static const char *spotify_token_uri = "https://accounts.spotify.com/api/token"; static const char *spotify_token_uri = "https://accounts.spotify.com/api/token";
static const char *spotify_playlist_uri = "https://api.spotify.com/v1/playlists/%s"; static const char *spotify_playlist_uri = "https://api.spotify.com/v1/playlists/%s";
static const char *spotify_track_uri = "https://api.spotify.com/v1/tracks/%s"; static const char *spotify_track_uri = "https://api.spotify.com/v1/tracks/%s";
static const char *spotify_me_uri = "https://api.spotify.com/v1/me"; static const char *spotify_me_uri = "https://api.spotify.com/v1/me";
@ -219,6 +223,13 @@ request_access_tokens(struct keyval *kv, const char **err)
spotify_refresh_token = strdup(tmp); spotify_refresh_token = strdup(tmp);
} }
tmp = jparse_str_from_obj(haystack, "scope");
if (tmp)
{
free(spotify_granted_scope);
spotify_granted_scope = strdup(tmp);
}
expires_in = jparse_int_from_obj(haystack, "expires_in"); expires_in = jparse_int_from_obj(haystack, "expires_in");
if (expires_in == 0) if (expires_in == 0)
expires_in = 3600; expires_in = 3600;
@ -914,7 +925,7 @@ spotifywebapi_oauth_uri_get(const char *redirect_uri)
ret = ( (keyval_add(&kv, "client_id", spotify_client_id) == 0) && ret = ( (keyval_add(&kv, "client_id", spotify_client_id) == 0) &&
(keyval_add(&kv, "response_type", "code") == 0) && (keyval_add(&kv, "response_type", "code") == 0) &&
(keyval_add(&kv, "redirect_uri", redirect_uri) == 0) && (keyval_add(&kv, "redirect_uri", redirect_uri) == 0) &&
(keyval_add(&kv, "scope", "user-read-private playlist-read-private user-library-read") == 0) && (keyval_add(&kv, "scope", spotify_scope) == 0) &&
(keyval_add(&kv, "show_dialog", "false") == 0) ); (keyval_add(&kv, "show_dialog", "false") == 0) );
if (!ret) if (!ret)
{ {
@ -1890,11 +1901,19 @@ spotifywebapi_status_info_get(struct spotifywebapi_status_info *info)
info->token_valid = token_valid(); info->token_valid = token_valid();
if (spotify_user) if (spotify_user)
{ {
memcpy(info->user, spotify_user, (sizeof(info->user) - 1)); strncpy(info->user, spotify_user, (sizeof(info->user) - 1));
} }
if (spotify_user_country) if (spotify_user_country)
{ {
memcpy(info->country, spotify_user_country, (sizeof(info->country) - 1)); strncpy(info->country, spotify_user_country, (sizeof(info->country) - 1));
}
if (spotify_granted_scope)
{
strncpy(info->granted_scope, spotify_granted_scope, (sizeof(info->granted_scope) - 1));
}
if (spotify_scope)
{
strncpy(info->required_scope, spotify_scope, (sizeof(info->required_scope) - 1));
} }
CHECK_ERR(L_SPOTIFY, pthread_mutex_unlock(&token_lck)); CHECK_ERR(L_SPOTIFY, pthread_mutex_unlock(&token_lck));
@ -1936,6 +1955,12 @@ spotifywebapi_deinit()
CHECK_ERR(L_SPOTIFY, pthread_mutex_destroy(&token_lck)); CHECK_ERR(L_SPOTIFY, pthread_mutex_destroy(&token_lck));
spotify_deinit(); spotify_deinit();
free(spotify_access_token);
free(spotify_refresh_token);
free(spotify_granted_scope);
free(spotify_user_country);
free(spotify_user);
} }
struct library_source spotifyscanner = struct library_source spotifyscanner =

View File

@ -31,6 +31,8 @@ struct spotifywebapi_status_info
bool token_valid; bool token_valid;
char user[100]; char user[100];
char country[3]; // ISO 3166-1 alpha-2 country code char country[3]; // ISO 3166-1 alpha-2 country code
char granted_scope[250];
char required_scope[250];
}; };
struct spotifywebapi_access_token struct spotifywebapi_access_token