Skip to content

Instantly share code, notes, and snippets.

@hvt
Forked from matthijsotterloo/code.php
Last active September 18, 2015 12:36
Show Gist options
  • Save hvt/b1ec30ece40e2fdd872c to your computer and use it in GitHub Desktop.
Save hvt/b1ec30ece40e2fdd872c 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(array('input','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',
'https://beta.cloudmoosic.com/music/spotify/login/'
);
$scopes = array(
'user-read-email',
'user-library-modify'
);
$api = new SpotifyWebAPI\SpotifyWebAPI();
// Redirect to Spotify login screen if user is not logged in.
$code = $this->input->get('code');
if ($code !== false) {
$session->requestAccessToken($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