# Load Packages
library(httr)
library(jsonlite)
library(tidyverse)Assignment 6
Assignment 6: Learning a New API
Explanation of Service and Use Cases
The Spotify Web API allows users to access data about albums, artists, and tracks that are on Spotify. The purpose of this script is to use Spotify’s API to retrieve data related to music tracks, artists, and albums using a GET request. The following steps demonstrate how to use the Client Credentials Flow to access Spotify’s API and retrieve useful music data.
For instance, you could use Spotify’s API to get all the track details, like name, album, duration, and popularity, corresponding with a specific artist.
Requesting Access: Client Credentials Flow
Authorization is the process of gaining permissions to Spotify data. Spotify uses the OAuth 2.0 framework, which enables third-party applications to obtain limited access to an HTTP service.
In this assignment, I use the “Client Credentials Flow” authentication process, which only allows access to public data. I used this method to protect personal user information.
This process works by sending the app’s Client ID and Client Secret to Spotify’s accounts service, which returns an access token. The token can then be used in requests to the Spotify Web API.
Preliminary Steps
- Go to the Spotify for Developers Dashboard website (https://developer.spotify.com/).
- Create an app to get your Client ID and Client Secret.
- Use these credentials to authenticate and request data through Spotify’s API.
Set Up Credentials
# Set Client ID and Client Secret
client_id <- "<YOUR_CLIENT_ID_HERE>"
client_secret <- "<YOUR_CLIENT_SECRET_HERE>"Endpoint
# Spotify token endpoint
spotify_token_url <- "https://accounts.spotify.com/api/token"
# Request an access token
# uses POST to retrieve access token, as required by Spotify
spotify_authentication <- POST(
url = spotify_token_url,
authenticate(client_id, client_secret),
body = list(grant_type = "client_credentials"),
encode = "form")
# Extract access token
access_token <- content(spotify_authentication)$access_tokenDemonstration of Features: Making a Call to the API
In this case, I want to use the API to find 20 tracks by Taylor Swift, along with their popularity and duration.
# Example: Find tracks by Taylor Swift
# "limit" is max number of items in response
# "q" is search query term
# "type" is kind of data to retrieve tracks, artists, etc)
artist <- "Taylor Swift"
search_type <- "track"
limit <- 20
# Define endpoint and query parameters
spotify_search_endpoint <- paste0(
"https://api.spotify.com/v1/search?",
"q=", URLencode(artist), #URL encode accounts for spaces or characters in artist name
"&type=", search_type,
"&limit=", limit)GET Request
# GET Request
spotify_response <- GET(
url = spotify_search_endpoint,
add_headers(Authorization = paste("Bearer", access_token)))
# Bearer is part of authentication via Client Credentials Flow
# Checking Status Code for Success
# 200 indicates successful request
spotify_response$status_codeParsing Results as JSON
spotify_api_data <-
spotify_response %>%
content(as = "text",
encoding = "UTF-8") %>%
fromJSON()Viewing All Results
This will return all track information. Notably, each track is a list of information rather than just a single value, which causes a list column format. In the example below where I extract the data, I retrieve fields without this issue for simplicity.
spotify_api_data$tracks$items
%>% view()Extracting Certain Information into Data Frame
# Extract track information
spotify_df <-
data.frame(
track_name = spotify_api_data$tracks$items$name,
popularity = spotify_api_data$tracks$items$popularity,
duration_ms = spotify_api_data$tracks$items$duration_ms)Creating CSV
write_csv(spotify_df, "spotify_taylor_tracks.csv")Conclusion
The Spotify API is a great tool for analyzing music and artist data on the platform. Using the Client Credentials Flow, as demonstrated here, allows public access to the data without sacrificing personal information.