1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\OAuth1\Service; |
4
|
|
|
|
5
|
|
|
use OAuth\Common\Consumer\CredentialsInterface; |
6
|
|
|
use OAuth\Common\Exception\Exception; |
7
|
|
|
use OAuth\Common\Http\Client\ClientInterface; |
8
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
9
|
|
|
use OAuth\Common\Http\Uri\Uri; |
10
|
|
|
use OAuth\Common\Http\Uri\UriInterface; |
11
|
|
|
use OAuth\Common\Storage\TokenStorageInterface; |
12
|
|
|
use OAuth\OAuth1\Signature\SignatureInterface; |
13
|
|
|
use OAuth\OAuth1\Token\StdOAuth1Token; |
14
|
|
|
|
15
|
|
|
class Twitter extends AbstractService |
16
|
|
|
{ |
17
|
|
|
const ENDPOINT_AUTHENTICATE = 'https://api.twitter.com/oauth/authenticate'; |
18
|
|
|
const ENDPOINT_AUTHORIZE = 'https://api.twitter.com/oauth/authorize'; |
19
|
|
|
|
20
|
|
|
protected $authorizationEndpoint = self::ENDPOINT_AUTHENTICATE; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
CredentialsInterface $credentials, |
24
|
|
|
ClientInterface $httpClient, |
25
|
|
|
TokenStorageInterface $storage, |
26
|
|
|
SignatureInterface $signature, |
27
|
|
|
?UriInterface $baseApiUri = null |
28
|
|
|
) { |
29
|
|
|
parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri); |
30
|
|
|
|
31
|
|
|
if (null === $baseApiUri) { |
32
|
|
|
$this->baseApiUri = new Uri('https://api.twitter.com/1.1/'); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function getRequestTokenEndpoint() |
40
|
|
|
{ |
41
|
|
|
return new Uri('https://api.twitter.com/oauth/request_token'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function getAuthorizationEndpoint() |
48
|
|
|
{ |
49
|
|
|
if ($this->authorizationEndpoint != self::ENDPOINT_AUTHENTICATE |
50
|
|
|
&& $this->authorizationEndpoint != self::ENDPOINT_AUTHORIZE) { |
51
|
|
|
$this->authorizationEndpoint = self::ENDPOINT_AUTHENTICATE; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return new Uri($this->authorizationEndpoint); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param mixed $endpoint |
59
|
|
|
*/ |
60
|
|
|
public function setAuthorizationEndpoint($endpoint): void |
61
|
|
|
{ |
62
|
|
|
if ($endpoint != self::ENDPOINT_AUTHENTICATE && $endpoint != self::ENDPOINT_AUTHORIZE) { |
63
|
|
|
throw new Exception( |
64
|
|
|
sprintf("'%s' is not a correct Twitter authorization endpoint.", $endpoint) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
$this->authorizationEndpoint = $endpoint; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function getAccessTokenEndpoint() |
74
|
|
|
{ |
75
|
|
|
return new Uri('https://api.twitter.com/oauth/access_token'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
protected function parseRequestTokenResponse($responseBody) |
82
|
|
|
{ |
83
|
|
|
parse_str($responseBody, $data); |
84
|
|
|
|
85
|
|
|
if (null === $data || !is_array($data)) { |
86
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
87
|
|
|
} elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') { |
88
|
|
|
throw new TokenResponseException('Error in retrieving token.'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->parseAccessTokenResponse($responseBody); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
protected function parseAccessTokenResponse($responseBody) |
98
|
|
|
{ |
99
|
|
|
parse_str($responseBody, $data); |
100
|
|
|
|
101
|
|
|
if (null === $data || !is_array($data)) { |
102
|
|
|
throw new TokenResponseException('Unable to parse response: ' . $responseBody); |
103
|
|
|
} elseif (isset($data['error'])) { |
104
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
105
|
|
|
} elseif (!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])) { |
106
|
|
|
throw new TokenResponseException('Invalid response. OAuth Token data not set: ' . $responseBody); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$token = new StdOAuth1Token(); |
110
|
|
|
|
111
|
|
|
$token->setRequestToken($data['oauth_token']); |
112
|
|
|
$token->setRequestTokenSecret($data['oauth_token_secret']); |
113
|
|
|
$token->setAccessToken($data['oauth_token']); |
114
|
|
|
$token->setAccessTokenSecret($data['oauth_token_secret']); |
115
|
|
|
|
116
|
|
|
$token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES); |
117
|
|
|
unset($data['oauth_token'], $data['oauth_token_secret']); |
118
|
|
|
$token->setExtraParams($data); |
119
|
|
|
|
120
|
|
|
return $token; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|