I'm trying to extract data from my spotify account using R.
When running the script i'm getting the following error:
Error in token$sign(req$method, req$url): attempt to apply non-function
This is the script i'm trying to run:
install.packages("spotifyr")
install.packages("ggjoy")
install.packages("httpuv")
install.packages("tidyverse")
install.packages("ggridges")
library(devtools)
library(spotifyr)
library(knitr)
library(tidyverse)
library(ggridges)
library(lubridate)
library(httpuv)
##Redirect URL http://localhost:1410/
id <- ''
secret <- ''
Sys.setenv(SPOTIFY_CLIENT_ID = id)
Sys.setenv(SPOTIFY_CLIENT_SECRET = secret)
access_token <- get_spotify_access_token()
get_my_recently_played(limit = 50) %>%
mutate(artist.name = map_chr(track.artists, function(x) x$name[1]),
played_at = as_datetime(played_at)) %>%
select(track.name, artist.name, track.album.name, played_at, track.duration_ms) %>%
kable()
yes
0
Could anyone help to nudge me into the right direction for solving this error. Thanks in advance!
Note: the following solution is a hack, and is only a workaround. This probably shouldn't be used in any sort of production code. You should either get the spotifyr package maintainer to update the package, or create your own cloned package as described here:
It looks to me that the spotifyr::scopes()
function, which get_spotify_authorization_code()
relies on, points to this page: https://developer.spotify.com/documentation/general/guides/scopes/. This url comes back with a 404 Error, and looks like it no longer exists.
I believe it should be pointing here instead: https://developer.spotify.com/documentation/general/guides/authorization/scopes/
You could technically overwrite the scopes function in the spotifyr
namespace, using assignInNamespace
, with a new function including the proper url. The below works for me when I test it:
library(spotifyr)
library(httr)
library(knitr)
library(tidyverse)
library(lubridate)
Sys.setenv(SPOTIFY_CLIENT_ID = 'abc')
Sys.setenv(SPOTIFY_CLIENT_SECRET = 'xyz')
scopes <- function() {
xml2::read_html("https://developer.spotify.com/documentation/general/guides/authorization/scopes/") %>%
rvest::html_elements("code") %>% rvest::html_text() %>%
unique()
}
assignInNamespace("scopes", scopes, ns = "spotifyr")
df <- get_my_recently_played(limit = 5) %>%
mutate(artist.name = map_chr(track.artists, function(x) x$name[1]),
played_at = as_datetime(played_at)) %>%
select(track.name, artist.name, track.album.name, played_at, track.duration_ms)
Result:
kable(df)
|track.name |artist.name |track.album.name |played_at | track.duration_ms|
|:-----------------------------------------------|:-------------|:---------------------------------------------|:-------------------|-----------------:|
|Livin It Up (with Post Malone & A$AP Rocky) |Young Thug |Punk |2021-10-20 12:34:17 | 210906|
|Too Easy |Gunna |Too Easy |2021-10-20 12:28:54 | 138586|
|Pissed Me Off |Lil Durk |Pissed Me Off |2021-10-20 12:26:08 | 123076|
|family ties (with Kendrick Lamar) |Baby Keem |family ties (with Kendrick Lamar) |2021-10-20 12:24:05 | 252069|
|Ocean Eyes - Astronomyy Remix |Billie Eilish |Ocean Eyes (The Remixes) |2021-10-20 12:18:48 | 296266|