Skip to content

Commit 49a7adc

Browse files
committed
feat: add pinterest support to laravel socialite provider
1 parent 22b76c8 commit 49a7adc

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

src/LaravelOAuthServiceProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\ServiceProvider;
66
use Mckenziearts\LaravelOAuth\Providers\DribbbleProvider;
77
use Mckenziearts\LaravelOAuth\Providers\InstagramProvider;
8+
use Mckenziearts\LaravelOAuth\Providers\PinterestProvider;
89

910
class LaravelOAuthServiceProvider extends ServiceProvider
1011
{
@@ -33,6 +34,7 @@ public function boot()
3334
// Boot all new OAuth 2 Provider added to Socialite
3435
$this->bootInstagramSocialite();
3536
$this->bootDribbbleSocialite();
37+
$this->bootPinterestSocialite();
3638
}
3739

3840
/**
@@ -101,4 +103,17 @@ function ($app) use ($socialite) {
101103
}
102104
);
103105
}
106+
107+
private function bootPinterestSocialite()
108+
{
109+
$socialite = $this->app->make(\Laravel\Socialite\Contracts\Factory::class);
110+
$socialite->extend(
111+
'pinterest',
112+
function ($app) use ($socialite) {
113+
$config = $app['config']['services.pinterest'];
114+
115+
return $socialite->buildProvider(PinterestProvider::class, $config);
116+
}
117+
);
118+
}
104119
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Mckenziearts\LaravelOAuth\Providers;
4+
5+
use Laravel\Socialite\Two\User;
6+
use Laravel\Socialite\Two\AbstractProvider;
7+
use Laravel\Socialite\Two\ProviderInterface;
8+
9+
class PinterestProvider extends AbstractProvider implements ProviderInterface
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
protected $scopes = ['read_public'];
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
protected function getAuthUrl($state)
20+
{
21+
return $this->buildAuthUrlFromBase(
22+
'https://api.pinterest.com/oauth/',
23+
$state
24+
);
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
protected function getTokenUrl()
31+
{
32+
return 'https://api.pinterest.com/v1/oauth/token';
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
protected function getUserByToken($token)
39+
{
40+
$response = $this->getHttpClient()->get(
41+
'https://api.pinterest.com/v1/me/',
42+
[
43+
'headers' => [
44+
'Authorization' => 'Bearer '.$token,
45+
],
46+
]
47+
);
48+
$contents = $response->getBody()->getContents();
49+
return json_decode($contents, true);
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
protected function mapUserToObject(array $user)
56+
{
57+
preg_match('#https://www.pinterest.com/(.+?)/#', $user['data']['url'], $matches);
58+
$nickname = $matches[1];
59+
return (new User())->setRaw($user)->map(
60+
[
61+
'id' => $user['data']['id'],
62+
'nickname' => $nickname,
63+
'name' => $user['data']['first_name'].' '.$user['data']['last_name'],
64+
]
65+
);
66+
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
protected function getTokenFields($code)
72+
{
73+
return array_merge(
74+
parent::getTokenFields($code),
75+
[
76+
'grant_type' => 'authorization_code',
77+
]
78+
);
79+
}
80+
}

0 commit comments

Comments
 (0)