01-18-2024 05:07 PM
I need help to figure out how my Powershell script can get a new application token eveytime i used the Shopping API. For now the only way i found is to generate one each time and paste it inside my Powershell script but it's valid for a couple of hours and i need to run the script many time a day on a schedule.
I have search the web a lot and tried many different things, i even tried with ChatGPT but i can't figure how to construct my request for that inside the script.
Any help will be appreciated.
Thanks!
01-18-2024 06:10 PM
I looked up "powershell" and it looks like a command-line tool that runs command-line scripts on Windows machines. But where does it run - out on a web server? From your description, it sounds like it can auto-run on some schedule.
An application token has a two hour life. It can be renewed when eBay returns an expiration error, or it can be renewed before it times out.
I have a PHP program that I run on my server to update the application token as it expires every two hours. In that situation, I save the token to my server and test the age of the file prior to each API call. If it is within 30 seconds of expiring, I mint and save a new token to file, otherwise, I read the token from file. So far, it has worked for my applications that use the Shopping API.
I presume your shell invokes some routine or program to request data from eBay's API. Perhaps the program requesting Shopping data can call the routine to test and update the token. Or perhaps your auto-run shell can call a server program to deal with the age of the token file and update that file as needed, prior to using the token in scheduled Shopping calls.
By the way, the Shopping API is now deprecated. You will have until next January 2025 to convert your programs to the Browse API, which will use a similar token mechanism.
01-22-2024 11:01 AM
I tried your script but it always throw me an error like this:
PHP Warning: Undefined array key "access_token" in /var/www/prestashop/ebay_oauth/getBasicToken.php on line 63
01-22-2024 11:19 AM
Have you saved your credentials to individual text files on your server and provided the correct path to find them?
The location of that error seems to indicate you are not receiving a valid response to your curl request.
Are you familiar with PHP? If so, place a line of code in "getBasicToken.php" to intercept eBay's response, and call that module directly from your browser. That way you can see what eBay is sending back.
else { echo $response; // test to read response
exit; $token = json_decode($response,true); // true means use keys
01-22-2024 11:31 AM
No i am not familiar with php this is why i am using Powershell !
Where in your script should i place those lines ?
01-22-2024 12:32 PM
The single black line directly below the red code marks where you should insert the test code in the module called "getBasicToken.php",
01-22-2024 01:07 PM
It return this: {"error":"server_error","error_description":"server encountered an unexpected condition that prevented it from fulfilling the request"}
01-22-2024 02:18 PM
I'm afraid it will be hard to troubleshoot for you.
Next, I'm trying to determine if the credentials are being found.
Remove that prior test code and add red test code here to see if credentials are returned to your browser:
function createBasicOauthToken(){ global $oauth_clientIdfile, $oauth_secretIdfile, $oauth_basictokenfile ; $url = 'https://api.ebay.com/identity/v1/oauth2/token'; $clientID = file_get_contents($oauth_clientIdfile); // AppID $clientSecret = file_get_contents($oauth_secretIdfile); // CertID
echo $clientID."\n".$clientSecret;
exit; $headers = [
01-22-2024 03:30 PM
Yes it's returning my credentials.
01-22-2024 08:56 PM
I believe this message is coming from eBay, since the response is JSON format:
{"error":"server_error","error_description":"server encountered an unexpected condition that prevented it from fulfilling the request"}
Since your credentials look right to you, and eBay is not complaining about credentials, I suspect something is not right with the cURL post. My server is Linux and yours is Windows, so that could account for some differences in how the code is handled.
I asked ChatGPT to adjust the cURL code to Windows, and it came back with a powershell suggestion, which I would not know how to plug into PHP. So, without a Windows server, I'm at a loss here. Sorry.
$response = Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body
01-23-2024 12:01 PM
Thanks a lot for your help, i have now fix the problem with my Powershell script.
Is was basically the way i was passing the authorization part in the Headers of my request.
Now i am able to get my token correctly !
09-27-2024 10:53 PM - edited 09-27-2024 10:56 PM
Hi there,
I've been working on a similar issue with the eBay Shopping API and ran into the same problem. In fact, the same issue is in my app, where generating and manually pasting the token is tedious, especially when you need to run the script multiple times a day. What worked for me was automating the token refresh process within my PowerShell script.
To do this, you can create a function that sends a POST request to the token endpoint using your client ID and client secret, similar to how an app might authenticate, to generate a new token. Then, store this token and use it for your API requests. Here’s a simplified example:
```powershell
function Get-NewToken {
$url = "https://api.ebay.com/identity/v1/oauth2/token"
$headers = @{
Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$client_id:$client_secret"))
}
$body = @{
grant_type = "client_credentials"
scope = "https://api.ebay.com/oauth/api_scope"
}
$response = Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body
return $response.access_token
}
$token = Get-NewToken
```
You would then use `$token` for your API calls instead of manually pasting the token each time. This should save you the hassle and allow your script to run as frequently as needed without constant manual input.
If your app or script is making frequent requests, this approach will definitely streamline the process. Hope this helps!