cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Application Token

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!

Message 1 of 12
latest reply
11 REPLIES 11

Re: Application Token

@partstechdeals 

 

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.

 

 

ShipScript has been an eBay Community volunteer since 2003, specializing in HTML, CSS, Scripts, Photos, Active Content, Technical Solutions, and online Seller Tools.
Message 2 of 12
latest reply

Re: Application Token

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

Message 3 of 12
latest reply

Re: Application Token

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

 

 

 

ShipScript has been an eBay Community volunteer since 2003, specializing in HTML, CSS, Scripts, Photos, Active Content, Technical Solutions, and online Seller Tools.
Message 4 of 12
latest reply

Re: Application Token

No i am not familiar with php this is why i am using Powershell !

 

Where in your script should i place those lines ?

Message 5 of 12
latest reply

Re: Application Token

The single black line directly below the red code marks where you should insert the test code in the module called   "getBasicToken.php",

 

ShipScript has been an eBay Community volunteer since 2003, specializing in HTML, CSS, Scripts, Photos, Active Content, Technical Solutions, and online Seller Tools.
Message 6 of 12
latest reply

Re: Application Token

It return this: {"error":"server_error","error_description":"server encountered an unexpected condition that prevented it from fulfilling the request"}

Message 7 of 12
latest reply

Re: Application Token

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 = [

 

 

ShipScript has been an eBay Community volunteer since 2003, specializing in HTML, CSS, Scripts, Photos, Active Content, Technical Solutions, and online Seller Tools.
Message 8 of 12
latest reply

Re: Application Token

Yes it's returning my credentials.

Message 9 of 12
latest reply

Re: Application Token

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

 

ShipScript has been an eBay Community volunteer since 2003, specializing in HTML, CSS, Scripts, Photos, Active Content, Technical Solutions, and online Seller Tools.
Message 10 of 12
latest reply

Re: Application Token

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 !

 

Message 11 of 12
latest reply

Re: Application Token

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!

 

Message 12 of 12
latest reply