1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Flickr |
4
|
|
|
* |
5
|
|
|
* @created 20.10.2017 |
6
|
|
|
* @author Smiley <[email protected]> |
7
|
|
|
* @copyright 2017 Smiley |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace chillerlan\OAuth\Providers; |
12
|
|
|
|
13
|
|
|
use chillerlan\HTTP\Utils\MessageUtil; |
14
|
|
|
use chillerlan\HTTP\Utils\QueryUtil; |
15
|
|
|
use chillerlan\OAuth\Core\OAuth1Provider; |
16
|
|
|
use chillerlan\OAuth\Core\ProviderException; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\StreamInterface; |
19
|
|
|
use function array_merge; |
20
|
|
|
use function sprintf; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @see https://www.flickr.com/services/api/auth.oauth.html |
24
|
|
|
* @see https://www.flickr.com/services/api/ |
25
|
|
|
*/ |
26
|
|
|
class Flickr extends OAuth1Provider{ |
27
|
|
|
|
28
|
|
|
public const PERM_READ = 'read'; |
29
|
|
|
public const PERM_WRITE = 'write'; |
30
|
|
|
public const PERM_DELETE = 'delete'; |
31
|
|
|
|
32
|
|
|
protected string $requestTokenURL = 'https://www.flickr.com/services/oauth/request_token'; |
33
|
|
|
protected string $authURL = 'https://www.flickr.com/services/oauth/authorize'; |
34
|
|
|
protected string $accessTokenURL = 'https://www.flickr.com/services/oauth/access_token'; |
35
|
|
|
protected string $apiURL = 'https://api.flickr.com/services/rest'; |
36
|
|
|
protected ?string $userRevokeURL = 'https://www.flickr.com/services/auth/list.gne'; |
37
|
|
|
protected ?string $apiDocs = 'https://www.flickr.com/services/api/'; |
38
|
|
|
protected ?string $applicationURL = 'https://www.flickr.com/services/apps/create/'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritDoc |
42
|
|
|
*/ |
43
|
|
|
public function request( |
44
|
|
|
string $path, |
45
|
|
|
array $params = null, |
46
|
|
|
string $method = null, |
47
|
|
|
StreamInterface|array|string $body = null, |
48
|
|
|
array $headers = null |
49
|
|
|
):ResponseInterface{ |
50
|
|
|
|
51
|
|
|
$params = array_merge($params ?? [], [ |
52
|
|
|
'method' => $path, |
53
|
|
|
'format' => 'json', |
54
|
|
|
'nojsoncallback' => true, |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
$request = $this->getRequestAuthorization( |
58
|
|
|
/** @phan-suppress-next-line PhanTypeMismatchArgumentNullable */ |
59
|
|
|
$this->requestFactory->createRequest($method ?? 'POST', QueryUtil::merge($this->apiURL, $params)), |
60
|
|
|
$this->storage->getAccessToken($this->serviceName) |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
return $this->http->sendRequest($request); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
|
|
public function me():ResponseInterface{ |
70
|
|
|
$response = $this->request('flickr.test.login'); |
71
|
|
|
$status = $response->getStatusCode(); |
72
|
|
|
$json = MessageUtil::decodeJSON($response); |
73
|
|
|
|
74
|
|
|
if($status === 200 && isset($json->user)){ |
75
|
|
|
return $response; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if(isset($json->message)){ |
79
|
|
|
throw new ProviderException($json->message); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
throw new ProviderException(sprintf('user info error error HTTP/%s', $status)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|