friendship ended with social-app. php is my new best friend
1<?php
2/**
3 * Class Flickr
4 *
5 * @created 20.10.2017
6 * @author Smiley <smiley@chillerlan.net>
7 * @copyright 2017 Smiley
8 * @license MIT
9 *
10 * @noinspection PhpUnused
11 */
12declare(strict_types=1);
13
14namespace chillerlan\OAuth\Providers;
15
16use chillerlan\HTTP\Utils\QueryUtil;
17use chillerlan\OAuth\Core\{AuthenticatedUser, InvalidAccessTokenException, OAuth1Provider, UserInfo};
18use Psr\Http\Message\{ResponseInterface, StreamInterface};
19use function array_merge, sprintf;
20
21/**
22 * Flickr OAuth1
23 *
24 * @link https://www.flickr.com/services/api/auth.oauth.html
25 * @link https://www.flickr.com/services/api/
26 */
27class Flickr extends OAuth1Provider implements UserInfo{
28
29 public const IDENTIFIER = 'FLICKR';
30
31 public const PERM_READ = 'read';
32 public const PERM_WRITE = 'write';
33 public const PERM_DELETE = 'delete';
34
35 protected string $requestTokenURL = 'https://www.flickr.com/services/oauth/request_token';
36 protected string $authorizationURL = 'https://www.flickr.com/services/oauth/authorize';
37 protected string $accessTokenURL = 'https://www.flickr.com/services/oauth/access_token';
38 protected string $apiURL = 'https://api.flickr.com/services/rest';
39 protected string|null $userRevokeURL = 'https://www.flickr.com/services/auth/list.gne';
40 protected string|null $apiDocs = 'https://www.flickr.com/services/api/';
41 protected string|null $applicationURL = 'https://www.flickr.com/services/apps/create/';
42
43 public function request(
44 string $path,
45 array|null $params = null,
46 string|null $method = null,
47 StreamInterface|array|string|null $body = null,
48 array|null $headers = null,
49 string|null $protocolVersion = null,
50 ):ResponseInterface{
51
52 $params = array_merge(($params ?? []), [
53 'method' => $path,
54 'format' => 'json',
55 'nojsoncallback' => true,
56 ]);
57
58 $request = $this->getRequestAuthorization(
59 $this->requestFactory->createRequest(($method ?? 'POST'), QueryUtil::merge($this->apiURL, $params)),
60 );
61
62 return $this->http->sendRequest($request);
63 }
64
65 /**
66 * hi flickr, can i have a 401 on invalid token???
67 *
68 * @inheritDoc
69 * @codeCoverageIgnore
70 */
71 public function me():AuthenticatedUser{
72
73 $json = $this->getMeResponseData($this->apiURL, [
74 'method' => 'flickr.test.login',
75 'format' => 'json',
76 'nojsoncallback' => true,
77 ]);
78
79 if(isset($json['stat'], $json['message']) && $json['stat'] === 'fail'){
80
81 if($json['message'] === 'Invalid auth token'){
82 throw new InvalidAccessTokenException($json['message']);
83 }
84
85 throw new ProviderException($json['message']);
86 }
87
88 $userdata = [
89 'data' => $json['user'],
90 'handle' => $json['user']['username']['_content'],
91 'id' => $json['user']['id'],
92 'url' => sprintf('https://www.flickr.com/people/%s/', $json['user']['path_alias']),
93 ];
94
95 return new AuthenticatedUser($userdata);
96 }
97
98}