Skip to content

Commit

Permalink
get Tiktok user from the official user details endpoint (#994)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergey Lebedev <sergey.lebedev@corp.mail.ru>
  • Loading branch information
lebedevsergey and Sergey Lebedev authored Mar 21, 2023
1 parent c26e600 commit 484a9f4
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ public function user()
$token = Arr::get($response, 'data.access_token');

$this->user = $this->mapUserToObject(
$this->getUserByToken([
'access_token' => $token,
'open_id' => Arr::get($response, 'data.open_id'),
])
$this->getUserByToken($token)
);

return $this->user->setToken($token)
Expand Down Expand Up @@ -89,20 +86,17 @@ protected function getTokenFields($code)
}

/**
* Get TikTok user by token.
*
* @param array $data
*
* @return mixed
* @inheritdoc
*/
protected function getUserByToken($data)
protected function getUserByToken($token)
{
// Note: The TikTok api does not have an endpoint to get a user by the access
// token only. Open id is also required therefore:
// $data['access_token'] = $token, $data['open_id'] = $open_id

$response = $this->getHttpClient()->get(
'https://open-api.tiktok.com/oauth/userinfo?'.http_build_query($data)
'https://open.tiktokapis.com/v2/user/info/?fields=open_id,union_id,display_name,avatar_large_url',
[
'headers' => [
'Authorization' => 'Bearer '.$token,
],
]
);

return json_decode((string) $response->getBody(), true);
Expand All @@ -119,7 +113,7 @@ protected function mapUserToObject($user)
'id' => $user['open_id'],
'union_id' => $user['union_id'],
'name' => $user['display_name'],
'avatar' => $user['avatar_larger'],
'avatar' => $user['avatar_large_url'],
]);
}
}

0 comments on commit 484a9f4

Please sign in to comment.