Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 按艺术家获取所有Spotify歌曲的音频属性_R_Api_Spotify_Httr_Libspotify - Fatal编程技术网

R 按艺术家获取所有Spotify歌曲的音频属性

R 按艺术家获取所有Spotify歌曲的音频属性,r,api,spotify,httr,libspotify,R,Api,Spotify,Httr,Libspotify,这是以下问题的后续问题: 我在这里的目标是从Spotify的API中提取多个艺术家,然后按艺术家及其属性检索所有歌曲 这就是我们迄今为止针对前一个问题所做的工作: 检索有关艺术家的信息(不是按歌曲): 如果您从上述代码中知道艺术家ID,则可以选择多个艺术家 URI = paste0('https://api.spotify.com/v1/artists?ids=', Artist$artists$items[[2]]$id,",", '1Mxqyy3pSjf8kZZL4QVxS0') respo

这是以下问题的后续问题:

我在这里的目标是从Spotify的API中提取多个艺术家,然后按艺术家及其属性检索所有歌曲

这就是我们迄今为止针对前一个问题所做的工作:

检索有关艺术家的信息(不是按歌曲):

如果您从上述代码中知道艺术家ID,则可以选择多个艺术家

URI = paste0('https://api.spotify.com/v1/artists?ids=', Artist$artists$items[[2]]$id,",", '1Mxqyy3pSjf8kZZL4QVxS0')
response2 = GET(url = URI, add_headers(Authorization = HeaderValue))
Artists = content(response2)
如何提取多个艺术家的歌曲及其属性

这是音频功能的链接:

这是我的尝试:

artistID = '1Mxqyy3pSjf8kZZL4QVxS0'
HeaderValue = paste0('Bearer ', mytoken)
URI = paste0('https://api.spotify.com/v1/audio-features', artistID)
response2 = GET(url = URI, add_headers(Authorization = HeaderValue))
Artist = content(response2)
Artist
答复:

raw(0)

这是一个很好的参考,但即使在打开“httr”库之后,我也无法设置凭据

set_credentials(client_id=CLIENTID,client_secret=CLIENTSECRET)
错误:找不到函数“设置\u凭据”

任何帮助都很好,谢谢

以API凭证开头的部分修订:

clientID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'

response = POST(
  'https://accounts.spotify.com/api/token',
  accept_json(),
  authenticate(clientID, secret),
  body = list(grant_type = 'client_credentials'),
  encode = 'form',
  verbose()
 )

 mytoken = content(response)$access_token

我对R中的HTTP请求不是特别熟悉,但是如果下面的行依赖于字符串连接

URI = paste0('https://api.spotify.com/v1/audio-features', artistID)
第一个参数后面需要一个反斜杠,以便第二个参数正确地连接URI


此外,尽管。我建议你做的是为你感兴趣的艺术家抓取前10首左右的曲目,并使用端点获取所有曲目的音频功能。这会给你一个相当好的艺术家声音的表现。如果需要较小的表示,可以对特征进行平均,但要注意平均值可能会降低数据的准确性。

getFeatures@Hack-R如果你想到什么,因为我知道你能解决前面的问题。谢谢这可能意味着库没有正确安装和加载,我对此进行了测试,依赖项没有递归安装。在一个新的R会话中,这应该可以工作
install.packages(c('httr','jsonlite','RSelenium','RCurl','XML','stringr','stringi','plyr',dep=TRUE);devtools::install_github('rweyant/spotifyr')
这是我执行此操作时出现的错误:assign中出现错误(“client_redirect_uri”,client_redirect_uri,envir=.GlobalEnv):缺少参数“client_redirect_uri”,没有默认值。我在原始问题中尝试了此操作,结果为“raw(0)”。你知道如何在赛道上做到这一点吗。仍然无法获取音频属性。谢谢你的回馈。你能提供一个样本令牌吗,它可以是一个随机数,但我如何将其应用于此?我得到一个错误,上面写着:“令牌$sign:$operator中的错误对于原子向量是无效的”同样,当我按照您的方式应用它时,它表示:结构中的错误(list(method=method,url=url,headers=keep_last(headers)):缺少参数“token”,没有defaulthttr::config(token=token)请用您的Oauth授权代码更改此项…此处的此令牌指的是您在使用Spotify API进行身份验证后获得的o-auth令牌。谢谢,我刚刚编辑了此问题,以获得原始数据集中的示例。如果我在“使用API凭据修订开始部分”之后运行所有操作:然后你的脚本和我的API凭据都不起作用了。你能修改你的答案吗?我对这个很陌生,这就是为什么我要问这个问题。谢谢你的反馈。请在上面的代码中替换为add_headers(Authorization=HeaderValue)而不是httr::config(token=token)
getFeatures<-function(spotify_ID,token){
  req <- httr::GET(paste0("https://api.spotify.com/v1/audio-features/",spotify_ID), add_headers(Authorization = HeaderValue))
  json1<-httr::content(req)
  dados=data.frame(id=json1$id,
                   danceability=json1$danceability,
                   energy=json1$energy,
                   key=json1$key,
                   loudness=json1$loudness,
                   mode=json1$mode,
                   speechiness=json1$speechiness,
                   acousticness=json1$acousticness,
                   instrumentalness=json1$instrumentalness,
                   liveness=json1$liveness,
                   valence=json1$valence,
                   tempo=json1$tempo,
                   duration_ms=json1$duration_ms,
                   time_signature=json1$time_signature,
                   uri=json1$uri,
                   analysis_url=json1$analysis_url,stringsAsFactors = F)
  return(dados)
}

KanyeFatherStretch <- getFeatures("4KW1lqgSr8TKrvBII0Brf8")

Try this if it helps 
getFeatures<-function(spotify_ID,token){
  req <- httr::GET(paste0("https://api.spotify.com/v1/audio-features/",spotify_ID), add_headers(Authorization = HeaderValue))
  json1<-httr::content(req)
  dados=data.frame(id=json1$id,
                   danceability=json1$danceability,
                   energy=json1$energy,
                   key=json1$key,
                   loudness=json1$loudness,
                   mode=json1$mode,
                   speechiness=json1$speechiness,
                   acousticness=json1$acousticness,
                   instrumentalness=json1$instrumentalness,
                   liveness=json1$liveness,
                   valence=json1$valence,
                   tempo=json1$tempo,
                   duration_ms=json1$duration_ms,
                   time_signature=json1$time_signature,
                   uri=json1$uri,
                   analysis_url=json1$analysis_url,stringsAsFactors = F)
  return(dados)
}

KanyeFatherStretch <- getFeatures("4KW1lqgSr8TKrvBII0Brf8")

Try this if it helps