question

postiljon avatar image
1 Like"
postiljon asked

Straightforward way of programmatically getting an OAuth Application Token token with PHP

Hi, We recently developed an API application for another selling site (Delcampe) and are currently looking at integrating the eBay API for our store. But I find the API thoroughly confusing! I've already created Application keys (sandbox and production), and a RuName and I can retrieve user tokens, and I can make some test API calls, but all via the webinterface (click, click). Given that the tokens are only valid for a couple of hours (except the refresh token), I need a easy way to **programmatically get a new token.** Because most things I want to do with the API needs a fresh token every time (we plan on uploading some inventory items daily). So I keep looking for the straightforward simple piece of PHP code (with curl options) to systematically retrieve a new valid OAuth token to use? Where is it. (Yes, I've seen the eBay SDK on github, but that seems overly complex). Something like (pseudocode): appid=12345 certid=56789 RuName=bla build_curl_call(appid,certid,RuName) execute curl() { return $token; }
token renew
10 |600

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

postiljon avatar image
1 Like"
postiljon answered
I figured it out. Mostly thanks to https://medium.com/@abeflansburg/ebay-rest-api-oauth2-plain-english-edition-1d102a9f719c for explaining things in a clear way. So for future reference: <?php $authCode = $_GET['code']; $clientID = 'WillemPa-Posthist-PRD-12345-12345'; $clientSecret = 'PRD-12345-1234512345-12345'; $ruName = 'Willem_Pasterka-bla-bla'; $url = ' https://api.ebay.com/identity/v1/oauth2/token'; $headers = [ 'Content-Type: application/x-www-form-urlencoded', 'Authorization: Basic '.base64_encode($clientID.":".$clientSecret) ]; $body = http_build_query([ 'grant_type' => 'authorization_code', 'code' => $authCode, 'redirect_uri' => $ruName ]); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $body, CURLOPT_HTTPHEADER => $headers )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response."\n"; }
10 |600

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

keepcool-hvac avatar image
0 Likes"
keepcool-hvac answered
Please tell me from where to get the 'code' with line 2 : $authCode = $_GET['code']
10 |600

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.