Вы можете использовать функцию redirectUrlForUser()
для генерации URL для перенаправления.
$url = $service->redirectUrlForUser([
'state' => '<state>',
'scope' => [
'https://api.ebay.com/oauth/api_scope/sell.account',
'https://api.ebay.com/oauth/api_scope/sell.inventory'
]
]);
Затем позвоните, например, header()
для перенаправления пользователя. Обратите внимание, что вы не можете отобразить любой текст / HTML до вызова заголовка.
header("Location: $url");
После этого ваш токен должен быть сохранен в $_GET["code"]
, когда пользователь вернется с сайта ebay.
$token = $_GET["code"];
Итак, вы можете отправить свой запрос и использовать этот пример для возврата токена OAuth.
$request = new \DTS\eBaySDK\OAuth\Types\GetUserTokenRestRequest();
$request->code = $token;
$response = $service->getUserToken($request);
// Output the result of calling the service operation.
printf("\nStatus Code: %s\n\n", $response->getStatusCode());
if ($response->getStatusCode() !== 200) {
// Display information that an error has happened
printf(
"%s: %s\n\n",
$response->error,
$response->error_description
);
} else {
// Use the token to make calls to ebay services or store it.
printf(
"%s\n%s\n%s\n%s\n\n",
$response->access_token,
$response->token_type,
$response->expires_in,
$response->refresh_token
);
}
Ваш токен OAuth будет в переменной $response->access_token
. Токен недолговечный, поэтому вам нужно время от времени обновлять его, если вы хотите его использовать. Для этого используйте $response->refresh_token
и позвоните $service->refreshUserToken()
.
$response = $service->refreshUserToken(new Types\RefreshUserTokenRestRequest([
'refresh_token' => '<REFRESH TOKEN>',
'scope' => [
'https://api.ebay.com/oauth/api_scope/sell.account',
'https://api.ebay.com/oauth/api_scope/sell.inventory'
]
]));
// Handle it the same way as above, when you got the first OAuth token