/** * getSpotifyWebAPI function. * * @access public * @param mixed $w * @return void */ function getSpotifyWebAPI($w, $old_api = null) { if (!$w->internet()) { throw new SpotifyWebAPI\SpotifyWebAPIException("No internet connection", 100); } // // Read settings from JSON // $settings = getSettings($w); $oauth_client_id = $settings->oauth_client_id; $oauth_client_secret = $settings->oauth_client_secret; $oauth_redirect_uri = $settings->oauth_redirect_uri; $oauth_access_token = $settings->oauth_access_token; $oauth_expires = $settings->oauth_expires; $oauth_refresh_token = $settings->oauth_refresh_token; if ($old_api == null) { // create a new api object $session = new SpotifyWebAPI\Session($oauth_client_id, $oauth_client_secret, $oauth_redirect_uri); $session->setRefreshToken($oauth_refresh_token); $api = new SpotifyWebAPI\SpotifyWebAPI(); } // Check if refresh token necessary // if token validity < 20 minutes if (time() - $oauth_expires > 2400) { if ($old_api != null) { // when refresh needed: // create a new api object (even if api not null) $session = new SpotifyWebAPI\Session($oauth_client_id, $oauth_client_secret, $oauth_redirect_uri); $session->setRefreshToken($oauth_refresh_token); $api = new SpotifyWebAPI\SpotifyWebAPI(); } if ($session->refreshAccessToken() == true) { $oauth_access_token = $session->getAccessToken(); // Set new token to settings $ret = updateSetting($w, 'oauth_access_token', $oauth_access_token); if ($ret == false) { throw new SpotifyWebAPI\SpotifyWebAPIException("Cannot set oauth_access_token", 100); } $ret = updateSetting($w, 'oauth_expires', time()); if ($ret == false) { throw new SpotifyWebAPI\SpotifyWebAPIException("Cannot set oauth_expires", 100); } $api->setAccessToken($oauth_access_token); } else { throw new SpotifyWebAPI\SpotifyWebAPIException("Token could not be refreshed", 100); } } else { // no need to refresh, the old api is // stil valid if ($old_api != null) { $api = $old_api; } else { // set the access token for the new api $api->setAccessToken($oauth_access_token); } } return $api; }
public function testRefreshAccessToken() { $expected = array('grant_type' => 'refresh_token', 'refresh_token' => $this->refreshToken); $headers = array('Authorization' => 'Basic Yjc3NzI5MmFmMGRlZjIyZjkyNTc5OTFmYzc3MGI1MjA6NmEwNDE5ZjQzZDBhYTkzYjJhZTg4MTQyOWI2YjliYzI='); $return = array('body' => get_fixture('refresh-token')); $stub = $this->setupStub('POST', '/api/token', $expected, $headers, $return); $session = new SpotifyWebAPI\Session($this->clientID, $this->clientSecret, $this->redirectURI, $stub); $session->refreshAccessToken($this->refreshToken); $this->assertNotEmpty($session->getAccessToken()); $this->assertEquals(time() + 3600, $session->getTokenExpiration()); }
$root = $_SERVER['DOCUMENT_ROOT']; $session = new SpotifyWebAPI\Session('5bc1d4f975214ebb9be4698594970a18', 'ee1b5a43af9942b2adcf8f69532ae001', 'http://localhost:8888/confettiapp/server/spotify_auth.php'); $api = new SpotifyWebAPI\SpotifyWebAPI(); if (!isset($_SESSION)) { session_start(); } if (isset($_GET['authid'])) { $authid = $_GET['authid']; $_SESSION["authid"] = $authid; } if (isset($_GET['code'])) { $session->requestAccessToken($_GET['code']); $api->setAccessToken($session->getAccessToken()); $accessToken = $session->getAccessToken(); $refreshToken = $session->getRefreshToken(); $session->refreshAccessToken($refreshToken); $playlists = $api->getUserPlaylists('1113560298', array('limit' => 5)); if (isset($_SESSION["authid"])) { $authid_session = $_SESSION["authid"]; $fb_accesstoken = $fb->set("users/" . $authid_session . "/access_token", $accessToken); $fb_refreshtoken = $fb->set("users/" . $authid_session . "/refresh_token", $refreshToken); $user = $api->me(); $json = json_encode($user, true); $result = json_decode($json, true); $fb_username = $fb->set("users/" . $authid_session . "/sp_user", $result["id"]); echo "<script>window.close();</script>"; } } else { header('Location: ' . $session->getAuthorizeUrl(array('scope' => array('user-follow-modify', 'user-follow-read', 'user-read-email', 'user-read-private', 'playlist-modify-private', 'playlist-modify-public', 'playlist-read-private')))); die; }