-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImgur.php
66 lines (52 loc) · 2.03 KB
/
Imgur.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Class Imgur
*
* @created 28.07.2019
* @author smiley <smiley@chillerlan.net>
* @copyright 2019 smiley
* @license MIT
*/
declare(strict_types=1);
namespace chillerlan\OAuth\Providers;
use chillerlan\OAuth\Core\{AccessToken, AuthenticatedUser, CSRFToken, OAuth2Provider, TokenRefresh, UserInfo};
use function sprintf, time;
/**
* Imgur OAuth2
*
* Note: imgur sends an "expires_in" of 315360000 (10 years!) for access tokens,
* but states in the docs that tokens expire after one month.
*
* @link https://apidocs.imgur.com/
*/
class Imgur extends OAuth2Provider implements CSRFToken, TokenRefresh, UserInfo{
public const IDENTIFIER = 'IMGUR';
protected string $authorizationURL = 'https://api.imgur.com/oauth2/authorize';
protected string $accessTokenURL = 'https://api.imgur.com/oauth2/token';
protected string $apiURL = 'https://api.imgur.com';
protected string|null $userRevokeURL = 'https://imgur.com/account/settings/apps';
protected string|null $apiDocs = 'https://apidocs.imgur.com';
protected string|null $applicationURL = 'https://api.imgur.com/oauth2/addclient';
public function getAccessToken(string $code, string|null $state = null):AccessToken{
$this->checkState($state);
$body = $this->getAccessTokenRequestBodyParams($code);
$response = $this->sendAccessTokenRequest($this->accessTokenURL, $body);
$token = $this->parseTokenResponse($response);
// set the expiry to a sane period to allow auto-refreshing
$token->expires = (time() + 2592000); // 30 days
$this->storage->storeAccessToken($token, $this->name);
return $token;
}
/** @codeCoverageIgnore */
public function me():AuthenticatedUser{
$json = $this->getMeResponseData('/3/account/me');
$userdata = [
'data' => $json,
'avatar' => $json['data']['avatar'],
'handle' => $json['data']['url'],
'id' => $json['data']['id'],
'url' => sprintf('https://imgur.com/user/%s', $json['data']['url']),
];
return new AuthenticatedUser($userdata);
}
}