Skip to content

Commit

Permalink
Merge pull request #6 from rweyant/set-credentials
Browse files Browse the repository at this point in the history
Set credentials
  • Loading branch information
rweyant committed Dec 18, 2017
2 parents 3a7b65d + ad4a198 commit 5b60e5f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 85 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -1,7 +1,7 @@
Package: spotifyr
Type: Package
Title: A wrapper for the Spotify API in R
Version: 0.2.0
Version: 0.2.0.9000
Date: 2017-10-31
Authors@R: person("Robert", "Weyant", email = "robert.weyant@gmail.com", role = c("aut", "cre"))
Description: spotifyr offers access to all aspects of Spotify's API throuh R.
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Expand Up @@ -19,6 +19,7 @@ export(get_categories)
export(get_category)
export(get_category_playlists)
export(get_current_user_profile)
export(get_currently_playing_track)
export(get_featured_playlists)
export(get_new_releases)
export(get_playlist)
Expand All @@ -32,6 +33,7 @@ export(get_tracks)
export(get_tracks_audio_features)
export(get_user_devices)
export(get_user_followed_artists)
export(get_user_playback)
export(get_user_playlists)
export(get_user_profile)
export(get_user_recently_played)
Expand Down
87 changes: 4 additions & 83 deletions R/auth.R
Expand Up @@ -5,11 +5,11 @@
#'
#' @param client_id the client ID given from Spotify
#' @param client_secret the client secret ID given from Spotify
set_credentials <- function(client_id, client_secret, client_redirect_uri){
set_credentials <- function(client_id, client_secret){
# TODO: change to environment variables
assign('client_id', client_id, envir = .GlobalEnv)
assign('client_secret', client_secret, envir=.GlobalEnv)
assign('client_redirect_uri', client_redirect_uri, envir=.GlobalEnv)
Sys.setenv('SPOTIFY_CLIENT' = client_id)
Sys.setenv('SPOTIFY_SECRET' = client_secret)
# assign('client_redirect_uri', client_redirect_uri, envir=.GlobalEnv)
}


Expand Down Expand Up @@ -75,82 +75,3 @@ user_auth <- function(cache = TRUE) {
}


#' Get user code for Authorization Code user code
#' Lauches Selenium Webbrowser to handle process
#' This function looks for client_id and client_secret in the global environment
# get_user_code <- function(){
#
# response <- GET(url=authorize_url,
# query=list(client_id=client_id,
# response_type='code',
# scope=all_scopes,
# redirect_uri='http://www.bertplot.com/visualization/'))
#
# unlink(system.file("bin", package = "RSelenium"), recursive = T)
# checkForServer()
# startServer(log = FALSE, invisible = FALSE)
#
# webd <- remoteDriver()
# Sys.sleep(3)
# # webd <- remoteDriver(remoteServerAddr = "localhost",browserName='chrome',port=4455)
# x <- webd$open()
# x <- webd$navigate(response$url)
#
# ## Need to wait for next page
# ## Probably a better way to do this.
# while(str_detect(webd$getCurrentUrl()[[1]],'accounts.spotify.com')) Sys.sleep(2)
#
# user_code <- str_split(webd$getCurrentUrl()[[1]],pattern='code=')[[1]][2]
#
# # Close everything down
# webd$closeWindow()
# webd$closeServer()
#
# user_code
# }

#' Using the user_code, generates tokens for user code
#'
#' @param user_code user_code from get_user_code() function
# get_user_token <- function(user_code){
#
# response <- POST('https://accounts.spotify.com/api/token',
# accept_json(),
# authenticate(client_id,client_secret),
# body=list(grant_type='authorization_code',
# code=user_code,
# redirect_uri=client_redirect_uri),
# encode='form')
#
# content <- get_response_content(response)
#
# # Make accessible globally
# assign('access_token', content$access_token,envir = .GlobalEnv)
# assign('refresh_token', content$refresh_token,envir = .GlobalEnv)
#
# content
# }

#' Refresh your tokens
#'
#' @param token default = NULL. If no token is given, it will look for refresh_token in global environment
# refresh_user_token <- function(token=NULL){
#
# if(is.null(token) && !exists('refresh_token')) stop("Need to provide refresh token")
# if(is.null(token) && exists('refresh_token')) token <- refresh_token
#
# response <- POST('https://accounts.spotify.com/api/token',
# accept_json(),
# authenticate(client_id,client_secret),
# body=list(grant_type='refresh_token',
# refresh_token=token,
# redirect_uri='http://www.bertplot.com/visualization/'),
# encode='form')
#
# content <- get_response_content(response)
#
# # Make accessible globally
# assign('access_token',content$access_token,envir = .GlobalEnv)
#
# content
# }
5 changes: 5 additions & 0 deletions R/player.R
Expand Up @@ -20,6 +20,8 @@ get_user_devices <- function(){
#'
#' @references \href{https://developer.spotify.com/web-api/get-information-about-the-users-current-playback/}{API documentation}
#'
#' @export
#'
#' @examples
#' set_tokens()
#' user_auth()
Expand All @@ -35,6 +37,8 @@ get_user_playback <- function(){
#'
#' @references \href{https://developer.spotify.com/web-api/get-the-users-currently-playing-track/}{API documentation}
#'
#' @export
#'
#' @examples
#' set_tokens()
#' user_auth()
Expand Down Expand Up @@ -126,6 +130,7 @@ skip_user_playback_next <- function(device_id = NULL){
response <- POST(url = glue('{PLAYER_URL}/next'),
config(token = user_token))
get_response_content(response)
invisible()
}

#' Skip User’s Playback To Previous Track
Expand Down
2 changes: 1 addition & 1 deletion man/set_credentials.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5b60e5f

Please sign in to comment.