Skip to content

Commit

Permalink
Migrate from Zalo Login API v3 to V4 (#924)
Browse files Browse the repository at this point in the history
Co-authored-by: atymic <[email protected]>
  • Loading branch information
binhuq and atymic authored Oct 21, 2022
1 parent bd9311f commit 39305ff
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,29 @@ class Provider extends AbstractProvider
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://oauth.zaloapp.com/v3/auth', $state);
return $this->buildAuthUrlFromBase('https://oauth.zaloapp.com/v4/permission', $state);
}

/**
* {@inheritdoc}
*/
protected function getCodeFields($state = null)
{
$fields = [
'app_id' => $this->clientId,
'redirect_uri' => $this->redirectUrl,
'state' => $state,
];
$fields = parent::getCodeFields($state);

return array_merge($fields, $this->parameters);
$fields['app_id'] = $this->clientId;

return $fields;
}

/**
* {@inheritdoc}
*/
public function getAccessTokenResponse($code)
{
$response = $this->getHttpClient()->get($this->getTokenUrl(), [
RequestOptions::HEADERS => ['Accept' => 'application/json'],
RequestOptions::QUERY => $this->getTokenFields($code),
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
RequestOptions::HEADERS => ['secret_key' => $this->clientSecret],
RequestOptions::FORM_PARAMS => $this->getTokenFields($code),
]);

return json_decode((string) $response->getBody(), true);
Expand All @@ -55,15 +53,18 @@ public function getAccessTokenResponse($code)
*/
protected function getTokenUrl()
{
return 'https://oauth.zaloapp.com/v3/access_token';
return 'https://oauth.zaloapp.com/v4/access_token';
}

/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get('https://graph.zalo.me/v2.0/me?access_token='.$token.'&fields=id,birthday,name,gender,picture');
$response = $this->getHttpClient()->get('https://graph.zalo.me/v2.0/me', [
RequestOptions::HEADERS => ['access_token' => $token],
RequestOptions::QUERY => ['fields' => 'id,error,message,name,picture'],
]);

return json_decode((string) $response->getBody(), true);
}
Expand All @@ -76,7 +77,7 @@ protected function mapUserToObject(array $user)
return (new User())->setRaw($user)->map([
'id' => $user['id'],
'nickname' => null,
'name' => $user['name'],
'name' => $user['name'] ?? null,
'avatar' => preg_replace('/^http:/i', 'https:', $user['picture']['data']['url']),
]);
}
Expand All @@ -86,11 +87,10 @@ protected function mapUserToObject(array $user)
*/
protected function getTokenFields($code)
{
return [
'app_id' => $this->clientId,
'app_secret' => $this->clientSecret,
'code' => $code,
'redirect_uri' => $this->redirectUrl,
];
$fields = parent::getTokenFields($code);

$fields['app_id'] = $this->clientId;

return $fields;
}
}

0 comments on commit 39305ff

Please sign in to comment.