Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_tracks_to_playlist() fails when uris has length 1. #199

Open
jonathan-g opened this issue May 16, 2023 · 1 comment
Open

add_tracks_to_playlist() fails when uris has length 1. #199

jonathan-g opened this issue May 16, 2023 · 1 comment

Comments

@jonathan-g
Copy link

Trying to add tracks to a playlist, I get an error:

auth <- get_spotify_authorization_code(scope = c("playlist-modify-public"))

# This fails
add_tracks_to_playlist(playlist_id, track, 0, authorization = auth)
#> Request failed [400]. Retrying in 1 seconds...
#> Request failed [400]. Retrying in 1 seconds...
#> Error in add_tracks_to_playlist(playlist_id, track, 0, authorization = auth): Bad Request (HTTP 400).

# This succeeds
add_tracks_to_playlist(playlist_id, c(track, track), 0, authorization = auth)
#> $snapshot_id
#> [1] "<snapshot id>"

To get more information about the problem, I look at

library(tidyverse)
library(httr)
base_url <- "https://api.spotify.com/v1/playlists"
url <- str_glue("{base_url}/{playlist_id}/tracks")
params <- list(position = 0, uris = track)
res <- POST(url, body = params, config(token = auth), encode = "json")
res
#>   Response [https://api.spotify.com/v1/playlists/7uRhAIgNZNwsPaYfCaQJRW/tracks]
#>   Date: 2023-05-16 00:32
#>   Status: 400
#>   Content-Type: application/json; charset=utf-8
#>   Size: 79 B
#> {
#>   "error" : {
#>     "status" : 400,
#>     "message" : "Error parsing JSON."
#>   }

I think the issue is that the POST body is incorrectly encoded into JSON if there is only one element in the character vector uris.

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.3 (2023-03-15 ucrt)
#>  os       Windows 10 x64 (build 19045)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United States.utf8
#>  ctype    English_United States.utf8
#>  tz       America/Chicago
#>  date     2023-05-15
#>  pandoc   2.19.2 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version     date (UTC) lib source
#>  assertthat    0.2.1       2019-03-21 [1] CRAN (R 4.2.1)
#>  cli           3.6.1       2023-03-23 [1] CRAN (R 4.2.3)
#>  curl          5.0.0       2023-01-12 [1] CRAN (R 4.2.2)
#>  digest        0.6.31      2022-12-11 [1] CRAN (R 4.2.2)
#>  dplyr         1.1.2       2023-04-20 [1] CRAN (R 4.2.3)
#>  evaluate      0.20        2023-01-17 [1] CRAN (R 4.2.2)
#>  fansi         1.0.4       2023-01-22 [1] CRAN (R 4.2.2)
#>  fastmap       1.1.1       2023-02-24 [1] CRAN (R 4.2.2)
#>  fs            1.6.1       2023-02-06 [1] CRAN (R 4.2.2)
#>  generics      0.1.3       2022-07-05 [1] CRAN (R 4.2.1)
#>  glue          1.6.2       2022-02-24 [1] CRAN (R 4.2.1)
#>  hms           1.1.3       2023-03-21 [1] CRAN (R 4.2.2)
#>  htmltools     0.5.5       2023-03-23 [1] CRAN (R 4.2.3)
#>  httr          1.4.5       2023-02-24 [1] CRAN (R 4.2.2)
#>  janitor       2.2.0       2023-02-02 [1] CRAN (R 4.2.2)
#>  jsonlite      1.8.4       2022-12-06 [1] CRAN (R 4.2.2)
#>  knitr         1.42        2023-01-25 [1] CRAN (R 4.2.2)
#>  lifecycle     1.0.3       2022-10-07 [1] CRAN (R 4.2.2)
#>  lubridate     1.9.2       2023-02-10 [1] CRAN (R 4.2.2)
#>  magrittr      2.0.3       2022-03-30 [1] CRAN (R 4.2.1)
#>  pillar        1.9.0       2023-03-22 [1] CRAN (R 4.2.3)
#>  pkgconfig     2.0.3       2019-09-22 [1] CRAN (R 4.2.1)
#>  purrr         1.0.1       2023-01-10 [1] CRAN (R 4.2.2)
#>  R.cache       0.16.0      2022-07-21 [1] CRAN (R 4.2.1)
#>  R.methodsS3   1.8.2       2022-06-13 [1] CRAN (R 4.2.0)
#>  R.oo          1.25.0      2022-06-12 [1] CRAN (R 4.2.0)
#>  R.utils       2.12.2      2022-11-11 [1] CRAN (R 4.2.2)
#>  R6            2.5.1       2021-08-19 [1] CRAN (R 4.2.1)
#>  readr         2.1.4       2023-02-10 [1] CRAN (R 4.2.2)
#>  reprex        2.0.2       2022-08-17 [1] CRAN (R 4.2.1)
#>  rlang         1.1.0       2023-03-14 [1] CRAN (R 4.2.2)
#>  rmarkdown     2.20.1.9000 2023-04-24 [1] Github (jonathan-g/rmarkdown@a2d68e0)
#>  rstudioapi    0.14.0-9000 2022-08-28 [1] Github (rstudio/rstudioapi@72e63b1)
#>  rvest         1.0.3       2022-08-19 [1] CRAN (R 4.2.1)
#>  sessioninfo   1.2.2       2021-12-06 [1] CRAN (R 4.2.1)
#>  snakecase     0.11.0      2019-05-25 [1] CRAN (R 4.2.1)
#>  spotifyr    * 2.2.4       2022-12-15 [1] CRAN (R 4.2.2)
#>  stringi       1.7.12      2023-01-11 [1] CRAN (R 4.2.2)
#>  stringr       1.5.0       2022-12-02 [1] CRAN (R 4.2.2)
#>  styler        1.9.1       2023-03-04 [1] CRAN (R 4.2.2)
#>  tibble        3.2.1       2023-03-20 [1] CRAN (R 4.2.2)
#>  tidyselect    1.2.0       2022-10-10 [1] CRAN (R 4.2.2)
#>  timechange    0.2.0       2023-01-11 [1] CRAN (R 4.2.2)
#>  tzdb          0.3.0       2022-03-28 [1] CRAN (R 4.2.1)
#>  utf8          1.2.3       2023-01-31 [1] CRAN (R 4.2.2)
#>  vctrs         0.6.2       2023-04-19 [1] CRAN (R 4.2.3)
#>  withr         2.5.0       2022-03-03 [1] CRAN (R 4.2.1)
#>  xfun          0.39        2023-04-20 [1] CRAN (R 4.2.3)
#>  xml2          1.3.3       2021-11-30 [1] CRAN (R 4.2.1)
#>  yaml          2.3.7       2023-01-23 [1] CRAN (R 4.2.2)
#> 
#>  [1] C:/Users/Jonathan/AppData/Local/R/win-library/4.2
#>  [2] C:/Program Files/R/R-4.2.3/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@enricotonini
Copy link

Hi, I've just found exactly the same problem. Since it seems a low-level issue not trivial to be solved, I tentatively decided to create a wrapper of add_tracks_to_playlist(), that, if length(uris) = 1 it adds together with the desired track another track which I would never add to my Spotify library and then immediately removes it. This is a trick that forces the function to add at least two songs in any case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants