Example #1
0
/**
 * 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->refreshToken() == 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;
}
Example #2
0
require __DIR__ . '/../vendor/autoload.php';
use Firebase\Firebase;
$fb = Firebase::initialize("https://boiling-fire-2669.firebaseio.com/", "P5Ofkmp3suKfnJWbWOtQimj5SqzC0tuWBdSz9UQh");
$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 {
 public function testUserFollowsPlaylist()
 {
     $options = array('ids' => array('74ASZWbe4lXaubB36ztrGX', '36QJpDe2go2KgaRleHCDTp'));
     $expected = array('ids' => '74ASZWbe4lXaubB36ztrGX,36QJpDe2go2KgaRleHCDTp');
     $headers = array('Authorization' => 'Bearer ' . $this->accessToken);
     $return = array('body' => get_fixture('users-follows-playlist'));
     $stub = $this->setupStub('GET', '/v1/users/mcgurk/playlists/0UZ0Ll4HJHR7yvURYbHJe9/followers/contains', $expected, $headers, $return);
     $api = new SpotifyWebAPI\SpotifyWebAPI($stub);
     $api->setAccessToken($this->accessToken);
     $response = $api->userFollowsPlaylist('mcgurk', '0UZ0Ll4HJHR7yvURYbHJe9', $options);
     $this->assertTrue($response[0]);
 }
Example #4
0
<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require __DIR__ . '/vendor/autoload.php';
$session = new SpotifyWebAPI\Session('2888525482b94ccb86ae7ee9469bab07', '6df8e93ea2ba49c6ae90951fea0e2f9e', 'http://confetti:8888/details.php');
$api = new SpotifyWebAPI\SpotifyWebAPI();
// Request a access token using the code from Spotify
$session->requestAccessToken($_GET['code']);
$accessToken = $session->getAccessToken();
$refreshToken = $session->getRefreshToken();
// Set the access token on the API wrapper
$api->setAccessToken($accessToken);
$session->refreshAccessToken($refreshToken);
// Start using the API!
// $add = $api->addUserPlaylistTracks('1113560298', '7Fyg5tJ0oQdIRxLwOJ2T1g', array(
//     '0sr5LOMv4x0jmTYfe6oOvP',
// ));
if ($accessToken) {
    $data = array('access_token' => $accessToken, 'refresh_token' => $refreshToken);
    echo json_encode($data);
} else {
    echo "Can't authorize";
}