-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTumblr.php
82 lines (66 loc) · 2.52 KB
/
Tumblr.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* Class Tumblr
*
* @created 22.10.2017
* @author Smiley <smiley@chillerlan.net>
* @copyright 2017 Smiley
* @license MIT
*
* @noinspection PhpUnused
*/
declare(strict_types=1);
namespace chillerlan\OAuth\Providers;
use chillerlan\HTTP\Utils\MessageUtil;
use chillerlan\OAuth\Core\{AccessToken, AuthenticatedUser, OAuth1Provider, UserInfo};
use function sprintf;
/**
* Tumblr OAuth1
*
* @link https://www.tumblr.com/docs/en/api/v2#oauth1-authorization
*/
class Tumblr extends OAuth1Provider implements UserInfo{
public const IDENTIFIER = 'TUMBLR';
protected string $requestTokenURL = 'https://www.tumblr.com/oauth/request_token';
protected string $authorizationURL = 'https://www.tumblr.com/oauth/authorize';
protected string $accessTokenURL = 'https://www.tumblr.com/oauth/access_token';
protected string $apiURL = 'https://api.tumblr.com';
protected string|null $userRevokeURL = 'https://www.tumblr.com/settings/apps';
protected string|null $apiDocs = 'https://www.tumblr.com/docs/en/api/v2';
protected string|null $applicationURL = 'https://www.tumblr.com/oauth/apps';
/** @codeCoverageIgnore */
public function me():AuthenticatedUser{
$json = $this->getMeResponseData('/v2/user/info');
$userdata = [
'data' => $json,
'handle' => $json['response']['user']['name'],
'url' => sprintf('https://www.tumblr.com/%s', $json['response']['user']['name']),
];
return new AuthenticatedUser($userdata);
}
/**
* Exchange the current token for an OAuth2 token - this will invalidate the OAuth1 token.
*
* @link https://www.tumblr.com/docs/en/api/v2#v2oauth2exchange---oauth1-to-oauth2-token-exchange
*
* @throws \chillerlan\OAuth\Providers\ProviderException
*/
public function exchangeForOAuth2Token():AccessToken{
$response = $this->request(path: '/v2/oauth2/exchange', method: 'POST');
$status = $response->getStatusCode();
$json = MessageUtil::decodeJSON($response);
if($status === 200){
$token = $this->createAccessToken();
$token->accessToken = $json->access_token;
$token->refreshToken = $json->refresh_token;
$token->expires = $json->expires_in;
$token->extraParams = ['scope' => $json->scope, 'token_type' => $json->token_type];
$this->storage->storeAccessToken($token, $this->name);
return $token;
}
if(isset($json->meta, $json->meta->msg)){
throw new ProviderException($json->meta->msg);
}
throw new ProviderException(sprintf('token exchange error HTTP/%s', $status));
}
}