【问题标题】:Error while trying to extract Spotify data using R尝试使用 R 提取 Spotify 数据时出错
【发布时间】:2021-10-20 16:23:56
【问题描述】:

我正在尝试使用 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

谁能帮助我找到正确的方向来解决这个错误。 提前致谢!

【问题讨论】:

  • 请在reproducible exampleminimal reproducible example 中输入样本。 (1) 错误究竟发生在何处/何时? (2)get_my_recently_played(limit = 50)长什么样子?如果是大对象,请使用dputdput(head()) 提供。
  • 我没有深入挖掘,但我倾向于认为 spotifyr 包坏了。例如,get_spotify_authorization_code() 所依赖的 scopes() 函数指向此页面:https://developer.spotify.com/documentation/general/guides/scopes/,返回 404 错误,并且看起来不再存在。

标签: r spotify


【解决方案1】:

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

在我看来,get_spotify_authorization_code() 所依赖的 spotifyr::scopes() 函数指向此页面:https://developer.spotify.com/documentation/general/guides/scopes/。此网址返回 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|

【讨论】:

    猜你喜欢
    • 2019-10-06
    • 1970-01-01
    • 2020-05-08
    • 2013-08-28
    • 2020-09-11
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多