Completed
Push — master ( 6ad9e3...7a68b3 )
by Dmitry
01:36
created

OAuth2Client::advertiserGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 29
rs 9.8333
cc 2
nc 2
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Promopult\TikTokMarketingApi;
6
7
final class OAuth2Client
8
{
9
    /**
10
     * @var \Psr\Http\Client\ClientInterface
11
     */
12
    private $httpClient;
13
14
    /**
15
     * OAuth2Client constructor.
16
     *
17
     * @param \Psr\Http\Client\ClientInterface $httpClient
18
     */
19
    public function __construct(
20
        \Psr\Http\Client\ClientInterface $httpClient
21
    ) {
22
        $this->httpClient = $httpClient;
23
    }
24
25
    /**
26
     * Getting a list of advertiser accounts.
27
     *
28
     * @param string $accessToken   Authorized Access Token
29
     * @param string $appId         The App id applied by the developer
30
     * @param string $secret        The private key of the developer's application
31
     * @param string $apiBaseUrl
32
     *
33
     * @return array
34
     *
35
     * @throws \Psr\Http\Client\ClientExceptionInterface
36
     *
37
     * @see https://ads.tiktok.com/marketing_api/docs?id=100579
38
     */
39
    public function advertiserGet(
40
        string $accessToken,
41
        string $appId,
42
        string $secret,
43
        string $apiBaseUrl = CredentialsInterface::API_BASE_URL
44
    ): array {
45
        $query = http_build_query([
46
            'access_token' => $accessToken,
47
            'app_id' => $appId,
48
            'secret' => $secret
49
        ]);
50
51
        $request = new \GuzzleHttp\Psr7\Request(
52
            'GET',
53
            $apiBaseUrl . '/open_api/v1.2/oauth2/advertiser/get/?' . $query,
54
            [
55
                'Accept' => 'application/json'
56
            ]
57
        );
58
59
        $response = $this->httpClient->sendRequest($request);
60
61
        $parsedBody = json_decode($response->getBody()->getContents(), true);
62
63
        if (empty($parsedBody)) {
64
            throw new \Promopult\TikTokMarketingApi\Exception\UnexpectedApiResponse($request, $response);
65
        }
66
67
        return $parsedBody;
68
    }
69
70
    /**
71
     * Get Long-term Access Token
72
     *
73
     * @param string $appId
74
     * @param string $authCode
75
     * @param string $secret
76
     * @param string $apiBaseUrl
77
     *
78
     * @return array
79
     *
80
     * @throws \Psr\Http\Client\ClientExceptionInterface
81
     */
82
    public function getAccessToken(
83
        string $appId,
84
        string $authCode,
85
        string $secret,
86
        string $apiBaseUrl = CredentialsInterface::API_BASE_URL
87
    ): array {
88
        $request = new \GuzzleHttp\Psr7\Request(
89
            'POST',
90
            $apiBaseUrl . '/open_api/v1.2/oauth2/access_token/',
91
            [
92
                'Content-Type' => 'application/json'
93
            ],
94
            json_encode([
95
                'app_id' => $appId,
96
                'auth_code' => $authCode,
97
                'secret' => $secret
98
            ])
99
        );
100
101
        $response = $this->httpClient->sendRequest($request);
102
103
        $accessToken = json_decode($response->getBody()->getContents(), true);
104
105
        if (empty($accessToken)) {
106
            throw new \Promopult\TikTokMarketingApi\Exception\UnexpectedApiResponse($request, $response);
107
        }
108
109
        return $accessToken;
110
    }
111
112
    /**
113
     * Creates Authorization URL
114
     *
115
     * @param string $appId         The application ID.
116
     * @param string $redirectUri   The callback address, which is set and defined by you.
117
     * @param int[] $scope          The scope of permissions. See https://ads.tiktok.com/marketing_api/docs?id=100648
118
     * @param string $state         A user-defined parameter which can be used to transfer user-defined information,
119
     *                              and is returned with the authorization code. Common usage includes, using the
120
     *                              advertiser account to distinguish the advertiser corresponding to the authorization
121
     *                              code during callback. Other usages can be set as you wish.
122
     * @param string $apiBaseUrl    API base URL, ex. https://ads.tiktok.com
123
     *
124
     * @return string
125
     *
126
     * @see https://ads.tiktok.com/marketing_api/docs?id=100648
127
     * @see
128
     */
129
    public static function createAuthorizationUrl(
130
        string $appId,
131
        string $redirectUri,
132
        string $state,
133
        ?array $scope = null,
134
        string $apiBaseUrl = CredentialsInterface::API_BASE_URL
135
    ): string {
136
        $queryParams = [
137
            'app_id' => $appId,
138
            'state' => $state,
139
            'redirect_uri' => $redirectUri,
140
        ];
141
142
        if ($scope) {
143
            $queryParams['scope'] = '[' . implode(',', $scope) . ']';
144
        }
145
146
        return $apiBaseUrl . '/marketing_api/auth?' . http_build_query($queryParams);
147
    }
148
}
149