Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa7f9a8

Browse files
author
ClouSale
committed
Get Refresh Token from LWA Authorization Code
1 parent 4386497 commit fa7f9a8

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

lib/SellingPartnerOAuth.php

+38
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,42 @@ public static function getAccessTokenFromRefreshToken($refreshToken, $clientId,
3939

4040
return $bodyAsJson['access_token'];
4141
}
42+
43+
/**
44+
* @param string $lwaAuthorizationCode
45+
* @param string $clientId
46+
* @param string $clientSecret
47+
* @param string $redirectUri
48+
* @return string|null
49+
* @throws GuzzleException
50+
*/
51+
public static function getRefreshTokenFromLwaAuthorizationCode(
52+
string $lwaAuthorizationCode,
53+
string $clientId,
54+
string $clientSecret,
55+
string $redirectUri
56+
): ?string {
57+
$client = new Client();
58+
$params = [
59+
'grant_type' => 'authorization_code',
60+
'client_id' => $clientId,
61+
'client_secret' => $clientSecret,
62+
'code' => $lwaAuthorizationCode,
63+
'redirect_uri' => $redirectUri,
64+
];
65+
$options = array_merge([
66+
RequestOptions::HEADERS => ['Accept' => 'application/json'],
67+
RequestOptions::HTTP_ERRORS => false,
68+
'curl' => [
69+
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2,
70+
],
71+
], $params ? [RequestOptions::FORM_PARAMS => $params] : []);
72+
73+
$response = $client->request('POST', 'https://api.amazon.com/auth/o2/token', $options);
74+
75+
$body = $response->getBody()->getContents();
76+
$bodyAsJson = json_decode($body, true);
77+
78+
return $bodyAsJson['refresh_token'];
79+
}
4280
}

0 commit comments

Comments
 (0)