Skip to content

Commit

Permalink
[n/a] fix minor issues, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Romain Monteil committed Nov 10, 2018
1 parent c45d49f commit 753d7cd
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 48 deletions.
36 changes: 36 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [Github](https://github.com/ker0x/oauth2-spotify).

## Pull Requests

- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Coding Standards Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the README and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow SemVer. Randomly breaking public APIs is not an option.

- **Create topic branches** - Don't ask us to pull from your master branch.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.

- **Ensure tests pass!** - Please run the tests (see below) before submitting your pull request, and make sure they pass. We won't accept a patch until all tests pass.

- **Ensure no coding standards violations** - Please run PHP Coding Standards Fixer before submitting your pull request. A violation will cause the build to fail, so please make sure there are no violations. We can't accept a patch if the build fails.

## Testing

The following tests must pass for a build to be considered successful. If contributing, please ensure these pass before submitting a pull request.

``` bash
$ composer csfix
$ composer test
```

**Happy coding**!
106 changes: 72 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,57 +53,95 @@ $provider = new Kerox\OAuth2\Client\Provider\Spotify([

if (!isset($_GET['code'])) {
// If we don't have an authorization code then get one
$authUrl = $provider->getAuthorizationUrl();
$authUrl = $provider->getAuthorizationUrl([
'scope' => [
Kerox\OAuth2\Client\Provider\Spotify::SCOPE_USER_READ_BIRTHDATE,
Kerox\OAuth2\Client\Provider\Spotify::SCOPE_USER_READ_EMAIL,
]
]);

$_SESSION['oauth2state'] = $provider->getState();

header('Location: '.$authUrl);
header('Location: ' . $authUrl);
exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

unset($_SESSION['oauth2state']);
exit('Invalid state');

} else {
echo 'Invalid state.';
exit;

// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
}

// Optional: Now you have a token you can look up a users profile data
try {
// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);

// We got an access token, let's now get the user's details
/** @var \Kerox\OAuth2\Client\Provider\SpotifyResourceOwner $owner */
$user = $provider->getResourceOwner($token);
// Optional: Now you have a token you can look up a users profile data
try {

// Use these details to create a new profile
printf('Hello %s!', $user->getDisplayName());
// We got an access token, let's now get the user's details
/** @var \Kerox\OAuth2\Client\Provider\SpotifyResourceOwner $owner */
$user = $provider->getResourceOwner($token);

} catch (Exception $e) {
// Use these details to create a new profile
printf('Hello %s!', $user->getDisplayName());

echo '<pre>';
var_dump($user);
# object(Kerox\OAuth2\Client\Provider\SpotifyResourceOwner)#10 (1) { ...
echo '</pre>';

// Failed to get user details
exit('Damned...');
}
} catch (Exception $e) {

// Use this to interact with an API on the users behalf
echo $token->getToken();
// Failed to get user details
exit('Damned...');
}

echo '<pre>';
// Use this to interact with an API on the users behalf
var_dump($token->getToken());
# string(217) "CAADAppfn3msBAI7tZBLWg...

// The time (in epoch time) when an access token will expire
var_dump($token->getExpires());
# int(1436825866)
echo '</pre>';
```

### Managing Scopes
### Authorization Scopes

When creating your Spotify authorization URL, you can specify scopes your application may authorize.
The following scopes are available as described in the [official documentation](https://developer.spotify.com/documentation/general/guides/scopes/):

```php
$options = [
'scope' => [
Kerox\OAuth2\Client\Provider\Spotify::SCOPE_USER_READ_BIRTHDATE,
Kerox\OAuth2\Client\Provider\Spotify::SCOPE_USER_READ_EMAIL,
]
];

$authorizationUrl = $provider->getAuthorizationUrl($options);
```
* SCOPE_APP_REMOTE_CONTROL
* SCOPE_PLAYLIST_MODIFY_PRIVATE
* SCOPE_PLAYLIST_MODIFY_PUBLIC
* SCOPE_PLAYLIST_READ_COLLABORATIVE
* SCOPE_PLAYLIST_READ_PRIVATE
* SCOPE_STREAMING
* SCOPE_USER_READ_PRIVATE
* SCOPE_USER_READ_BIRTHDATE
* SCOPE_USER_READ_EMAIL
* SCOPE_USER_TOP_READ
* SCOPE_USER_READ_RECENTLY_PLAYED
* SCOPE_USER_LIBRARY_MODIFY
* SCOPE_USER_LIBRARY_READ
* SCOPE_USER_READ_CURRENTLY_PLAYING
* SCOPE_USER_READ_PLAYBACK_STATE
* SCOPE_USER_MODIFY_PLAYBACK_STATE
* SCOPE_USER_FOLLOW_MODIFY
* SCOPE_USER_FOLLOW_READ

## Contributing

Please see [CONTRIBUTING](https://github.com/ker0x/oauth2-spotify/blob/master/CONTRIBUTING.md) for details.

## Credits

- [Romain Monteil](https://github.com/ker0x)

## License

The MIT License (MIT). Please see [License File](https://github.com/ker0x/oauth2-spotify/blob/master/LICENSE) for more information.
5 changes: 0 additions & 5 deletions src/Provider/Spotify.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public function __construct(array $options = [], array $collaborators = [])
/**
* Returns the base URL for authorizing a client.
*
* Eg. https://oauth.service.com/authorize
*
* @return string
*/
public function getBaseAuthorizationUrl(): string
Expand All @@ -69,8 +67,6 @@ public function getBaseAuthorizationUrl(): string
/**
* Returns the base URL for requesting an access token.
*
* Eg. https://oauth.service.com/token
*
* @param array $params
*
* @return string
Expand Down Expand Up @@ -108,7 +104,6 @@ protected function getDefaultScopes(): array
/**
* Checks a provider response for errors.
*
*
* @param ResponseInterface $response
* @param array|string $data Parsed response data
*
Expand Down
18 changes: 9 additions & 9 deletions src/Provider/SpotifyResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public function __construct(array $response)
/**
* @return null|string
*/
public function getBirthDate(): string
public function getBirthDate(): ?string
{
return $this->data['birthdate'];
return $this->data['birthdate'] ?? null;
}

/**
* @return null|string
*/
public function getCountry(): string
public function getCountry(): ?string
{
return $this->data['country'];
return $this->data['country'] ?? null;
}

/**
Expand All @@ -50,9 +50,9 @@ public function getDisplayName(): string
/**
* @return null|string
*/
public function getEmail(): string
public function getEmail(): ?string
{
return $this->data['email'];
return $this->data['email'] ?? null;
}

/**
Expand Down Expand Up @@ -80,7 +80,7 @@ public function getHref(): string
}

/**
* @return mixed
* @return string
*/
public function getId(): string
{
Expand All @@ -98,9 +98,9 @@ public function getImages(): array
/**
* @return null|string
*/
public function getProduct(): string
public function getProduct(): ?string
{
return $this->data['product'];
return $this->data['product'] ?? null;
}

/**
Expand Down

0 comments on commit 753d7cd

Please sign in to comment.