This tutorial discusses Spotify API. People might want to use Spotify API to learn more about their favorite songs and artists. Spotify data can be used to analyze trends in public preferences, new emerging artists, all-time favorite albums, and much more.
Walkthrough the steps for setting up the API:
1. Install the spotifyr package and other necessary packages.
library(spotifyr)
Warning: package 'spotifyr' was built under R version 4.3.2
library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.3.2
Warning: package 'ggplot2' was built under R version 4.3.2
Warning: package 'tibble' was built under R version 4.3.2
Warning: package 'tidyr' was built under R version 4.3.2
Warning: package 'readr' was built under R version 4.3.2
Warning: package 'purrr' was built under R version 4.3.2
Warning: package 'dplyr' was built under R version 4.3.2
Warning: package 'stringr' was built under R version 4.3.2
Warning: package 'forcats' was built under R version 4.3.2
Warning: package 'lubridate' was built under R version 4.3.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.3 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.0
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(jsonlite)
Warning: package 'jsonlite' was built under R version 4.3.2
Attaching package: 'jsonlite'
The following object is masked from 'package:purrr':
flatten
library(magrittr)
Warning: package 'magrittr' was built under R version 4.3.2
Attaching package: 'magrittr'
The following object is masked from 'package:purrr':
set_names
The following object is masked from 'package:tidyr':
extract
library(httpuv)
Warning: package 'httpuv' was built under R version 4.3.2
library(knitr)
Warning: package 'knitr' was built under R version 4.3.2
3. Log into the dashboard and accept Spotify’s terms.
4. Click the blue “Create app” button and fill out the fields.
5. Once your app has been created, go to “Settings” button to get your client ID and client secret.
id <-"XXXX"secret <-"XXXX"Sys.setenv(SPOTIFY_CLIENT_ID = id) Sys.setenv(SPOTIFY_CLIENT_SECRET = secret)
6. Retrieve a generic access token providing not-unique platform data.
access_token <-get_spotify_access_token()
7. Set an authorization code for private data access.
auth <-get_spotify_authorization_code(client_id =Sys.getenv("SPOTIFY_CLIENT_ID"), client_secret =Sys.getenv("SPOTIFY_CLIENT_SECRET"), scope =scopes()[c(7,8,14,15)]) # Below are all the scopes that Spotify can perform. I only chose 7, 8, 14 & 15.scopes()