我一直在用这个ebay-sdk-php从 David Sadler 生成对 Ebay API 的交易调用,但首先我必须创建 OAuthUserToken。
我使用了 gettoken.php 示例并创建了以下代码:
$service = new \DTS\eBaySDK\OAuth\Services\OAuthService([
'credentials' => config('ebay.'.config('ebay.mode').'.credentials'),
'ruName' => config('ebay.'.config('ebay.mode').'.ruName'),
'sandbox' => true
]);
$token = session('????'); //here I have to retrieve the authorization callback information.
/**
* Create the request object.
*/
$request = new \DTS\eBaySDK\OAuth\Types\GetUserTokenRestRequest();
$request->code = $token;
/**
* Send the request.
*/
$response = $service->getUserToken($request);
出于某种原因,我无法为 UserOauth token 生成重定向。我认为代码:
$service = new\DTS\eBaySDK\OAuth\Services\OAuthService([
...自动生成了一个指向 eBay Grant Area 的重定向,但事实并非如此。
有人知道怎么解决吗?我想知道如何授予用户访问权限,然后执行调用(例如 getEbayTime
)。
最佳答案
您可以使用 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()
重定向用户。请注意,您不能在 header 调用之前显示任何文本/html。
header("Location: $url");
之后,当用户从 ebay 网站返回时,您的 token 应存储在 $_GET["code"]
中。
$token = $_GET["code"];
因此,您可以发送请求并使用该示例取回 OAuth token 。
$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 token 将位于 $response->access_token
变量中。 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
关于php - 如何生成 eBay OAuth 用户 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56827786/