Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Get Refresh Token from LWA Authorization Code
Browse files Browse the repository at this point in the history
  • Loading branch information
ClouSale committed Feb 1, 2021
1 parent 4386497 commit fa7f9a8
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/SellingPartnerOAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,42 @@ public static function getAccessTokenFromRefreshToken($refreshToken, $clientId,

return $bodyAsJson['access_token'];
}

/**
* @param string $lwaAuthorizationCode
* @param string $clientId
* @param string $clientSecret
* @param string $redirectUri
* @return string|null
* @throws GuzzleException
*/
public static function getRefreshTokenFromLwaAuthorizationCode(
string $lwaAuthorizationCode,
string $clientId,
string $clientSecret,
string $redirectUri
): ?string {
$client = new Client();
$params = [
'grant_type' => 'authorization_code',
'client_id' => $clientId,
'client_secret' => $clientSecret,
'code' => $lwaAuthorizationCode,
'redirect_uri' => $redirectUri,
];
$options = array_merge([
RequestOptions::HEADERS => ['Accept' => 'application/json'],
RequestOptions::HTTP_ERRORS => false,
'curl' => [
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2,
],
], $params ? [RequestOptions::FORM_PARAMS => $params] : []);

$response = $client->request('POST', 'https://api.amazon.com/auth/o2/token', $options);

$body = $response->getBody()->getContents();
$bodyAsJson = json_decode($body, true);

return $bodyAsJson['refresh_token'];
}
}

0 comments on commit fa7f9a8

Please sign in to comment.