Skip to content

Commit d9b54c6

Browse files
authored
Merge pull request hybridauth#840 from sjerdo/feature/spotify-provider
Add Spotify OAuth2 provider
2 parents 4faff28 + 750ea83 commit d9b54c6

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

docs/providers.md

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Steam | Hybrid | [X] | [X] |
4444
Discord | OAuth2 | [X] | [X] | | |
4545
TwitchTV | OAuth2 | [X] | [X] | | |
4646
Authentiq | OAuth2 | [X] | [X] | | |
47+
Spotify | OAuth2 | [X] | [X] | | |
4748

4849
{% include callout.html content="Some providers such as Google and Yahoo may use multiple protocols for their APIs and as naming convention we append the protocol's name to the adapter's (Often the case with OpenID adapters as those might be subject to removal by providers in near future due to deprecation of the OpenID protocol)." type="default" %}
4950

src/Provider/Spotify.php

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/*!
3+
* Hybridauth
4+
* https://hybridauth.github.io | https://github.com/hybridauth/hybridauth
5+
* (c) 2017 Hybridauth authors | https://hybridauth.github.io/license.html
6+
*/
7+
8+
namespace Hybridauth\Provider;
9+
10+
use Hybridauth\Adapter\OAuth2;
11+
use Hybridauth\Exception\UnexpectedApiResponseException;
12+
use Hybridauth\Data;
13+
use Hybridauth\User;
14+
15+
/**
16+
* Spotify OAuth2 provider adapter.
17+
*/
18+
class Spotify extends OAuth2
19+
{
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public $scope = 'user-read-email';
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public $apiBaseUrl = 'https://api.spotify.com/v1/';
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public $authorizeUrl = 'https://accounts.spotify.com/authorize';
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
protected $accessTokenUrl = 'https://accounts.spotify.com/api/token';
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function getUserProfile()
45+
{
46+
$response = $this->apiRequest('me');
47+
48+
$data = new Data\Collection($response);
49+
50+
if (!$data->exists('id')) {
51+
throw new UnexpectedApiResponseException('Provider API returned an unexpected response.');
52+
}
53+
54+
$userProfile = new User\Profile();
55+
56+
$userProfile->identifier = $data->get('id');
57+
$userProfile->displayName = $data->get('display_name');
58+
$userProfile->email = $data->get('email');
59+
$userProfile->emailVerified = $data->get('email');
60+
$userProfile->profileURL = $data->filter('external_urls')->get('spotify');
61+
$userProfile->photoURL = $data->filter('images')->get('url');
62+
$userProfile->country = $data->get('country');
63+
64+
if ($data->exists('birthdate')) {
65+
$this->fetchBirthday($userProfile, $data->get('birthdate'));
66+
}
67+
68+
return $userProfile;
69+
}
70+
71+
/**
72+
* Fetch use birthday
73+
*/
74+
protected function fetchBirthday($userProfile, $birthday)
75+
{
76+
$result = (new Data\Parser())->parseBirthday($birthday, '-');
77+
78+
$userProfile->birthDay = (int)$result[0];
79+
$userProfile->birthMonth = (int)$result[1];
80+
$userProfile->birthYear = (int)$result[2];
81+
82+
return $userProfile;
83+
}
84+
85+
}

0 commit comments

Comments
 (0)