尝试使用 R 提取 Spotify 数据时出错

Error while trying to extract Spotify data using R

我正在尝试使用 R 从我的 spotify 帐户中提取数据。

当 运行 运行脚本时,出现以下错误: Error in token$sign(req$method, req$url): attempt to apply non-function

这是我正在尝试的脚本 运行:

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

任何人都可以帮助我找到解决此错误的正确方向。 提前致谢!

注意:以下解决方案是 hack,只是一种解决方法。这可能不应该在任何类型的生产代码中使用。您应该让 spotifyr 包维护者更新包,或者按照 here:

中的描述创建您自己的克隆包

在我看来,get_spotify_authorization_code() 所依赖的 spotifyr::scopes() 函数指向此页面:https://developer.spotify.com/documentation/general/guides/scopes/。 url 返回 404 错误,看起来它已不存在。

我认为它应该指向这里:https://developer.spotify.com/documentation/general/guides/authorization/scopes/

从技术上讲,您可以使用 assignInNamespace 覆盖 spotifyr 命名空间中的作用域函数,并使用一个包含正确 url 的新函数。当我测试它时,以下对我有用:

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)

结果:

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|