Skip to content

Commit

Permalink
Migrate from Patreon API v1 to v2. (#920)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Michot <[email protected]>
Co-authored-by: atymic <[email protected]>
  • Loading branch information
3 people authored Oct 21, 2022
1 parent 3bc9675 commit 8311da1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
51 changes: 23 additions & 28 deletions Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ class Provider extends AbstractProvider
/**
* {@inheritdoc}
*/
protected $scopes = ['users', 'pledges-to-me', 'my-campaign'];
protected $scopes = [
'campaigns',
'identity',
'identity[email]',
];

/**
* {@inherticdoc}.
Expand All @@ -26,10 +30,7 @@ class Provider extends AbstractProvider
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(
'https://www.patreon.com/oauth2/authorize',
$state
);
return $this->buildAuthUrlFromBase('https://www.patreon.com/oauth2/authorize', $state);
}

/**
Expand All @@ -45,15 +46,15 @@ protected function getTokenUrl()
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get(
'https://api.patreon.com/oauth2/api/current_user',
[
RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
],
]
);
$response = $this->getHttpClient()->get('https://api.patreon.com/api/oauth2/v2/identity', [
RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
],
RequestOptions::QUERY => [
'fields[user]' => 'email,full_name,image_url,vanity',
],
]);

return json_decode((string) $response->getBody(), true);
}
Expand All @@ -63,21 +64,15 @@ protected function getUserByToken($token)
*/
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => $user['data']['id'],
'nickname' => Arr::get($user['data']['attributes'], 'vanity', $user['data']['attributes']['full_name']),
'name' => $user['data']['attributes']['full_name'], 'email' => $user['data']['attributes']['email'],
'avatar' => $user['data']['attributes']['image_url'],
]);
}
$userData = $user['data'];
$userAttributes = $userData['attributes'];

/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return array_merge(parent::getTokenFields($code), [
'grant_type' => 'authorization_code',
return (new User())->setRaw($user)->map([
'id' => $userData['id'],
'nickname' => Arr::get($userAttributes, 'vanity', $userAttributes['full_name']),
'name' => $userAttributes['full_name'],
'email' => $userAttributes['email'],
'avatar' => $userAttributes['image_url'],
]);
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"docs": "https://socialiteproviders.com/patreon"
},
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.0",
"ext-json": "*",
"socialiteproviders/manager": "~4.0"
"socialiteproviders/manager": "^4.2"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit 8311da1

Please sign in to comment.