Skip to content

Instantly share code, notes, and snippets.

@matthijsotterloo
Last active August 29, 2015 14:17
Show Gist options
  • Save matthijsotterloo/da9da7a90214cfbf9ab0 to your computer and use it in GitHub Desktop.
Save matthijsotterloo/da9da7a90214cfbf9ab0 to your computer and use it in GitHub Desktop.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once 'vendor/Request.php';
require_once 'vendor/Session.php';
require_once 'vendor/SpotifyWebAPI.php';
require_once 'vendor/SpotifyWebAPIException.php';
class Spotify extends MY_Controller
{
public function __construct()
{
parent::__construct();
// To use site_url and redirect on this controller.
$this->load->helper('url');
}
public function login()
{
require_once 'vendor/Request.php';
require_once 'vendor/Session.php';
require_once 'vendor/SpotifyWebAPI.php';
require_once 'vendor/SpotifyWebAPIException.php';
$session = new SpotifyWebAPI\Session(
'placeholder',
'placeholder',
'placeholder'
);
$scopes = array(
'user-read-email',
'user-library-modify'
);
$api = new SpotifyWebAPI\SpotifyWebAPI();
// Redirect to Spotify login screen if user is not logged in.
if (isset($_GET['code'])) {
$session->requestAccessToken($_GET['code']);
$accessToken = $session->getAccessToken();
// Set the access token on the API wrapper
$api->setAccessToken($accessToken);
// Request a access token using the code from Spotify
print_r($api->me());
} else {
$authorizeUrl = $session->getAuthorizeUrl(array(
'scope' => $scopes
));
header('Location: ' . $authorizeUrl);
die();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment