Last active
August 29, 2015 14:17
-
-
Save matthijsotterloo/da9da7a90214cfbf9ab0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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