1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pinterest service. |
4
|
|
|
* |
5
|
|
|
* @author Pedro Amorim <[email protected]> |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
7
|
|
|
* |
8
|
|
|
* @see https://developers.pinterest.com/docs/api/overview/ |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace OAuth\OAuth2\Service; |
12
|
|
|
|
13
|
|
|
use OAuth\Common\Consumer\CredentialsInterface; |
14
|
|
|
use OAuth\Common\Http\Client\ClientInterface; |
15
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
16
|
|
|
use OAuth\Common\Http\Uri\Uri; |
17
|
|
|
use OAuth\Common\Http\Uri\UriInterface; |
18
|
|
|
use OAuth\Common\Storage\TokenStorageInterface; |
19
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Pinterest service. |
23
|
|
|
* |
24
|
|
|
* @author Pedro Amorim <[email protected]> |
25
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
26
|
|
|
* |
27
|
|
|
* @see https://developers.pinterest.com/docs/api/overview/ |
28
|
|
|
*/ |
29
|
|
|
class Pinterest extends AbstractService |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Defined scopes - More scopes are listed here: |
33
|
|
|
* https://developers.pinterest.com/docs/api/overview/. |
34
|
|
|
*/ |
35
|
|
|
const SCOPE_READ_PUBLIC = 'read_public'; // read a user’s Pins, boards and likes |
36
|
|
|
const SCOPE_WRITE_PUBLIC = 'write_public'; // write Pins, boards, likes |
37
|
|
|
const SCOPE_READ_RELATIONSHIPS = 'read_relationships'; // read a user’s follows (boards, users, interests) |
38
|
|
|
const SCOPE_WRITE_RELATIONSHIPS = 'write_relationships'; // follow boards, users and interests |
39
|
|
|
|
40
|
|
|
public function __construct( |
41
|
|
|
CredentialsInterface $credentials, |
42
|
|
|
ClientInterface $httpClient, |
43
|
|
|
TokenStorageInterface $storage, |
44
|
|
|
$scopes = [], |
45
|
|
|
?UriInterface $baseApiUri = null |
46
|
|
|
) { |
47
|
|
|
parent::__construct( |
48
|
|
|
$credentials, |
49
|
|
|
$httpClient, |
50
|
|
|
$storage, |
51
|
|
|
$scopes, |
52
|
|
|
$baseApiUri, |
53
|
|
|
true |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
if (null === $baseApiUri) { |
57
|
|
|
$this->baseApiUri = new Uri('https://api.pinterest.com/'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function getAuthorizationEndpoint() |
65
|
|
|
{ |
66
|
|
|
return new Uri('https://api.pinterest.com/oauth/'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function getAccessTokenEndpoint() |
73
|
|
|
{ |
74
|
|
|
return new Uri('https://api.pinterest.com/v1/oauth/token'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
protected function getAuthorizationMethod() |
81
|
|
|
{ |
82
|
|
|
return static::AUTHORIZATION_METHOD_HEADER_BEARER; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
protected function parseAccessTokenResponse($responseBody) |
89
|
|
|
{ |
90
|
|
|
$data = json_decode($responseBody, true); |
91
|
|
|
|
92
|
|
|
if (null === $data || !is_array($data)) { |
93
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
94
|
|
|
} elseif (isset($data['error'])) { |
95
|
|
|
throw new TokenResponseException( |
96
|
|
|
'Error in retrieving token: "' . $data['error'] . '"' |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$token = new StdOAuth2Token(); |
101
|
|
|
$token->setAccessToken($data['access_token']); |
102
|
|
|
|
103
|
|
|
if (isset($data['expires_in'])) { |
104
|
|
|
$token->setLifeTime($data['expires_in']); |
105
|
|
|
unset($data['expires_in']); |
106
|
|
|
} |
107
|
|
|
// I hope one day Pinterest add a refresh token :) |
108
|
|
|
if (isset($data['refresh_token'])) { |
109
|
|
|
$token->setRefreshToken($data['refresh_token']); |
110
|
|
|
unset($data['refresh_token']); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
unset($data['access_token']); |
114
|
|
|
|
115
|
|
|
$token->setExtraParams($data); |
116
|
|
|
|
117
|
|
|
return $token; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|