示例#1
0
文件: functions.php 项目: bycEEE/env
/**
 * 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;
}
示例#2
0
 public function register(Application $app)
 {
     $app['spotify.api'] = $app->share(function () use($app) {
         $api = new ThirdPartApi();
         if (!($auth_token = $app['predis']->get('spotify:auth'))) {
             $session = new \SpotifyWebAPI\Session(Credentials::CLIENT_ID, Credentials::CLIENT_SECRET, '');
             $session->requestCredentialsToken(array());
             $auth_token = $session->getAccessToken();
             $app['predis']->set('spotify:auth', $auth_token);
             $app['predis']->expire('spotify:auth', $session->getExpires() - 60);
         }
         $api->setAccessToken($auth_token);
         return new RuntimeCacheDecorator(new SpotifyApi($api));
     });
 }
示例#3
0
<?php

error_reporting(E_ALL);
ini_set('display_errors', 'On');
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);
<?php

require './vendor/autoload.php';
require './src/functions.php';
require_once './src/workflows.php';
$w = new Workflows('com.vdesabou.spotify.mini.player');
//
// 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;
try {
    $session = new SpotifyWebAPI\Session($oauth_client_id, $oauth_client_secret, $oauth_redirect_uri);
    if (!empty($_GET['code'])) {
        // Request a access token using the code from Spotify
        $ret = $session->requestAccessToken($_GET['code']);
        if ($ret == true) {
            $api = new SpotifyWebAPI\SpotifyWebAPI();
            // Set the code on the API wrapper
            $api->setAccessToken($session->getAccessToken());
            $user = $api->me();
            $ret = updateSetting($w, 'oauth_access_token', $session->getAccessToken());
            if ($ret == false) {
                echo "There was an error when updating settings";
                exec("kill -9 \$(ps -efx | grep \"php -S localhost:15298\"  | grep -v grep | awk '{print \$2}')");
                return;
            }
            $ret = updateSetting($w, 'oauth_expires', time());
            if ($ret == false) {
示例#5
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');
$scopes = array('playlist-read-private', 'user-read-private', 'playlist-modify-private', 'playlist-modify-public');
$authorizeUrl = $session->getAuthorizeUrl(array('scope' => $scopes));
header('Location: ' . $authorizeUrl);
die;
示例#6
0
<?php

require './vendor/autoload.php';
require './src/functions.php';
require_once './src/workflows.php';
$w = new Workflows('com.vdesabou.spotify.mini.player');
//
// 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;
try {
    $session = new SpotifyWebAPI\Session($oauth_client_id, $oauth_client_secret, $oauth_redirect_uri);
    // Get the authorization URL and send the user there
    header('Location: ' . $session->getAuthorizeUrl(array('scope' => array('user-library-read', 'user-read-email', 'user-read-private', 'user-library-modify', 'user-follow-modify', 'user-follow-read', 'playlist-read-private', 'playlist-modify-public', 'playlist-modify-private', 'playlist-read-collaborative'), 'show_dialog' => true)));
} catch (SpotifyWebAPI\SpotifyWebAPIException $e) {
    echo "There was an error during the authentication flow (exception " . $e . ")";
    displayNotificationWithArtwork("Web server killed", './images/warning.png', 'Error!');
    exec("kill -9 \$(ps -efx | grep \"php -S localhost:15298\"  | grep -v grep | awk '{print \$2}')");
    return;
}
示例#7
0
<?php

error_reporting(-1);
ini_set('display_errors', 1);
require 'vendor/autoload.php';
Dotenv::load(__DIR__);
$session = new SpotifyWebAPI\Session(getenv('SPOTIFY_CLIENT_ID'), getenv('SPOTIFY_CLIENT_SECRET'), getenv('SPOTIFY_REDIRECT_URI'));
$api = new SpotifyWebAPI\SpotifyWebAPI();
if (isset($_GET['code'])) {
    $session->requestToken($_GET['code']);
    $api->setAccessToken($session->getAccessToken());
    print_r($api->me());
} else {
    header('Location: ' . $session->getAuthorizeUrl(array('scope' => array('user-read-email', 'user-library-modify'))));
}
示例#8
0
<?php

error_reporting(-1);
ini_set('display_errors', 1);
require 'vendor/autoload.php';
Dotenv::load(__DIR__);
$session = new SpotifyWebAPI\Session(getenv('SPOTIFY_CLIENT_ID'), getenv('SPOTIFY_CLIENT_SECRET'), getenv('SPOTIFY_REDIRECT_URI'));
$api = new SpotifyWebAPI\SpotifyWebAPI();
if (isset($_GET['code'])) {
    $session->requestAccessToken($_GET['code']);
    $api->setAccessToken($session->getAccessToken());
    print_r($api->me());
} else {
    $scopes = array('scope' => array('user-read-email', 'user-library-modify'));
    header('Location: ' . $session->getAuthorizeUrl($scopes));
}
示例#9
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";
}
示例#10
0
 public function testSetRedirectUri()
 {
     $session = new SpotifyWebAPI\Session($this->clientID, $this->clientSecret, $this->redirectURI);
     $expected = $this->redirectURI;
     $session->setRedirectUri($expected);
     $this->assertEquals($expected, $session->getRedirectUri());
 }