I don’t know how, one of the biggest ecommerce platforms, has sparse, poor and bad documentation. I have seen api’s built by small tech companies and they are a hell of a lot better than this one.
I’m struggling creating a new access token for the media api (I need the product videos) So, after wasting a lot of time I just finally get the “code” via url and its “new” OAuth2. With this I get the access token and with this I can be able to get the info of a video by the videoID parameter of getItem endpoint.
My problem is this. I need to update programmatically my woocommerce website. I desing a script to get the items, save to a cvs file and import via wp all import, but I need to get the videos urls and I can get only the “code” param open a new window and needs the human intervention. If I use the access token to get the video for 1 product and if I use the same token to a new product then ebay returns me an error like: “The access token is invalid or has already use it” (say something like this)
How can I get the access token or improve this process to avoid human intervention??? or maybe one every 18 months that it’s suppose to live the refresh token.
This is my dummy code, this is only for testing purpose:
<?php
$oauth_clientIdfile = 'AAAAAAAAAAAAAA'; // file containing appID = clientID
$client_id = $oauth_clientIdfile;
$oauth_secretIdfile = 'BBBBBBBBB'; // file containing certID = clientSecret
$client_secret = $oauth_secretIdfile;
$redirect_uri = 'https://www.example.com/callback.php';
$response_type = 'code';
$scope = urlencode('https://api.ebay.com/oauth/api_scope https://api.ebay.com/oauth/api_scope/sell.inventory');
if (isset($_GET['code'])) {
// Step 2: Handle the callback and exchange the authorization code for an access token
$auth_code = $_GET['code'];
$token_url = "https://api.ebay.com/identity/v1/oauth2/token";
$post_fields = http_build_query([
'grant_type' => 'authorization_code',
'code' => $auth_code,
'redirect_uri' => $redirect_uri
]);
$headers = [
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Basic " . base64_encode("$client_id:$client_secret")
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if ($response === false) {
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
curl_close($ch);
$response_data = json_decode($response, true);
if (isset($response_data['access_token'])) {
$access_token = $response_data['access_token'];
echo "Access Token: " . htmlspecialchars($access_token);
// You can now use the access token to make API calls
// Example API call to getVideo endpoint
$video_id = 'EEEEEEEEEEEEEEEEEEE';
$api_url = "https://apim.ebay.com/commerce/media/v1_beta/video/$video_id"; // getVideo endpoint
$api_headers = [
"Authorization: Bearer $access_token",
"Content-Type: application/json",
// "Content-Language: en-US",
// "Accept: application/json",
// "Accept-Encoding: gzip",
// "Accept-Language: en-US",
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $api_headers);
$api_response = curl_exec($ch);
// echo "<pre>*";
// print_r($api_response);
// echo "*</pre>";
if ($api_response === false) {
die('API Request Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
curl_close($ch);
// echo "API Response: " . htmlspecialchars($api_response);
$response_data = json_decode($api_response, true);
echo "API Response: ";
echo "<pre>*";
print_r($response_data);
echo "*</pre>";
} else {
echo "Error retrieving access token: " . htmlspecialchars($response);
}
} else {
// Step 1: Redirect to eBay's authorization URL
$auth_url = "https://auth.ebay.com/oauth2/authorize?client_id={$client_id}&redirect_uri=" . urlencode($redirect_uri) . "&response_type=code&scope=" . urlencode($scope);
header("Location: $auth_url");
exit;
}
The code above works if I manually run open the page in a browser, but I need to run this process in a cron job.
Is there another way to get token to get the video info???
Thanks a lot for all your help. Your guidance!!