How do I fix my localhost for Shiny app OAuth2.0 Authentication?

Viewed 37

I am attempting to create a program which uses a user's Spotify data. I've conducted the following steps as per the documentation:

  1. Set up application
  2. Registered redirect urls on application dashboard
  3. Obtained Client ID and secret.

The code I'm trying to use to get authentication is below:

client_id <- "<CLIENT_ID>"
redirect_url <- "http://localhost:8888/callback/"

link <- glue::glue('https://accounts.spotify.com/authorize?client_id={client_id}&response_type=code&redirect_uri={redirect_url}&scope=user-top-read playlist-modify-public playlist-modify-private user-read-private user-library-read user-library-modify')


browseURL(link,
          browser = getOption("browser"),
          encodeIfNeeded = FALSE)

I was able to get it to show an authorization page once, I tried to approve the application and received a localhost connection error (Connection Refused). This error now happens upon running the code (no authorization page generated).

I've gone through all the steps to fix this issue (Flushing DNS, Disabling Firewall, different redirect urls, resetting my router), but nothing seems to work.

Does anyone have any suggestions on what I might be doing wrong?

1 Answers

I think the proper way of doing OAuth 2.0 authentication is via the httr::oauth2.0_* family. They do not show an example for Spotify, but it should be rather straight forward to set the "dance" up with this framework.

Type demo("oauth2-github") (or refer to the Code repo on GitHub) for an example using Oauth 2.0 for GitHub and adapt the code for Spotify. Be aware that httr provides a convenience function (oauth_endpoints) for some providers (but not Spotify). Hence, you have to provide the necessary config (mainly the proper URLs) using oauth_endpoint (Note the missing s).

If you have particular questions, come back with some code and I am sure we cna help.

Related