| 1 | package WebService::Spotify::Util; | ||
| 2 | use WebService::Spotify::OAuth2; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | sub prompt_for_user_token { | ||
| 7 | my ($username, $scope) = @_; | ||
| 8 | |||
| 9 | my $sp_oauth = WebService::Spotify::OAuth2->new( | ||
| 10 | client_id => $ENV{SPOTIFY_CLIENT_ID} || die('Expected env var SPOTIFY_CLIENT_ID'), | ||
| 11 | client_secret => $ENV{SPOTIFY_CLIENT_SECRET} || die('Expected env var SPOTIFY_CLIENT_SECRET'), | ||
| 12 | redirect_uri => $ENV{SPOTIFY_REDIRECT_URI} || die('Expected env var SPOTIFY_REDIRECT_URI'), | ||
| 13 | cache_path => $username, | ||
| 14 | trace => 0, | ||
| 15 | ); | ||
| 16 | $sp_oauth->scope($scope) if $scope; | ||
| 17 | |||
| 18 | my $token_info = $sp_oauth->get_cached_token; | ||
| 19 | |||
| 20 | if (!$token_info) { | ||
| 21 | my $auth_url = $sp_oauth->get_authorize_url; | ||
| 22 | |||
| 23 | printf "\nPlease navigate here:\n%s\n", $auth_url; | ||
| 24 | |||
| 25 | print "\nEnter the URL you were redirected to: "; | ||
| 26 | my $response = <STDIN>; | ||
| 27 | |||
| 28 | my $code = $sp_oauth->parse_response_code($response); | ||
| 29 | $token_info = $sp_oauth->get_access_token($code); | ||
| 30 | } | ||
| 31 | |||
| 32 | return $token_info ? $token_info->{access_token} : undef; | ||
| 33 | } | ||
| 34 | |||
| 35 | 1; |