Skip to content

Instantly share code, notes, and snippets.

@morrislaptop
Last active August 29, 2015 14:15
Show Gist options
  • Save morrislaptop/2603944cabfbf0faf11f to your computer and use it in GitHub Desktop.
Save morrislaptop/2603944cabfbf0faf11f to your computer and use it in GitHub Desktop.
<?php
namespace FestivalPlaylists\Oauth;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\User;
class SpotifyProvider extends AbstractProvider implements ProviderInterface
{
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://accounts.spotify.com/authorize', $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://accounts.spotify.com/api/token';
}
/**
* {@inheritdoc}
*/
public function getAccessToken($code)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
'headers' => ['Authorization' => 'Basic ' . base64_encode($this->clientId . ':' . $this->clientSecret)],
'body' => $this->getTokenFields($code),
]);
return $this->parseAccessToken($response->getBody());
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return array_add(
parent::getTokenFields($code), 'grant_type', 'authorization_code'
);
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get('https://api.spotify.com/v1/me', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function formatScopes(array $scopes)
{
return implode(' ', $scopes);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => $user['id'],
'nickname' => $user['display_name'],
'name' => $user['display_name'],
'avatar' => !empty($user['images']) ? $user['images'][0]['url'] : null,
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment