3

I have my code as shown below. I am trying to authenticate users with eBay to be able to get access token. On redirecting users back to my redirection URL to exchange code with access token, I get the error:

HTTP/1.1 100 Continue HTTP/1.1 400 Bad Request Content-Length: 109 Cneonction: close Date: Thu, 02 Mar 2017 06:47:51 GMT RlogId: t6ldssk%28ciudbq%60anng%7Fu2h%3F%3Cwk%7Difvqn*2%3D%3F%3E505-15a8dc659e6-0xf3 Set-Cookie: ebay=%5Esbf%3D%23%5E;Domain=.ebay.com;Path=/ X-EBAY-C-REQUEST-ID: ri=v22kQg9yaBLq,rci=2FDiyansIYnkONQC X-EBAY-C-VERSION: 1.0.0 X-EBAY-REQUEST-ID: 15a8dc658f8.a0968eb.5f6ef.fff87b7e![] Content-Type: application/json Connection: keep-alive {"error":"invalid_request","error_description":"'Authorization' is missing the the header:","error_uri":null}

What is wrong with my POST request?

$auth_code = $_GET['code'];
$client_id = "Client ID";
$client_secret = "Client Secret";
$redirect_uri = "RuName";
$headers = array (
    'Content-Type'  => 'application/x-www-form-urlencoded',
    'Authorization' => sprintf('Basic %s',base64_encode(sprintf('%s:%s', $client_id, $client_secret)))
);
$apiURL = "https://api.sandbox.ebay.com/identity/v1/oauth2/token";
$urlParams = array (
        "grant_type" => "authorization_code",
        "code" => urlencode($auth_code),
        "redirect_uri" => $redirect_uri
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Should be removed on production
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, true );

curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers = array ('Authorization' => sprintf('Basic %s',base64_encode(sprintf('%s:%s', $client_id, $client_secret))),'Content-Type'  => 'application/x-www-form-urlencoded') );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $urlParams );

$resp = curl_exec ( $ch );
curl_close ( $ch );

print_r ( $resp );
dennisdup
  • 33
  • 1
  • 3
  • Could you please provide me full oauth connecting part to ebay. I did all i could but could not go further any further. I really hope i can user your assist. I would really appreciate it. Thanks. – Tekraj Shrestha Sep 18 '18 at 06:34

1 Answers1

6

There are two small issues with your code. The first is that CURLOPT_HTTPHEADER requires an array of strings and not an associative array.

$headers = array (
    'Content-Type: application/x-www-form-urlencoded',
    'Authorization: '.sprintf('Basic %s',base64_encode(sprintf('%s:%s', $client_id, $client_secret)))
);

The second is to do with how PHP handles the CURLOPT_POSTFIELDS option. As noted in the documentation.

Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.

Since the eBay API requires the body to be application/x-www-form-urlencoded you will need to pass in a string instead. You can use your array to build this string by passing it to http_build_query.

$urlParams = http_build_query(array (
        "grant_type" => "authorization_code",
        "code" => $auth_code,
        "redirect_uri" => $redirect_uri
));

I also recommend Postman to test making API requests to eBay. One of it's features is that it will also create the PHP curl code for you. The code below has been adapted from the code that Postman created.

<?php

$clientID = '<YOUR EBAY APP ID>';
$clientSecret = '<YOUR EBAY CERT ID>';
$ruName = '<YOUR EBAY RUNAME>';
$authCode = '<AUTH CODE>';

$url = 'https://api.sandbox.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";
}
  • You have really assisted me. Seems there is much about Curl for me to learn. It worked!! Thanks a bunch David. – dennisdup Mar 04 '17 at 16:53
  • Today, I have got a great tutorial of eBay API but I am gettig "{"error":"invalid_grant","error_description":"the provided authorization grant code is invalid or was issued to another client"} ". What am I doing wrong ? – shubham jain May 26 '17 at 10:06
  • I got the **$authCode** from here, Please check this image - https://ibb.co/dWxPNa , – shubham jain May 26 '17 at 10:12
  • @shubhamjain You are getting the wrong token. eBay provides an older style called "Auth 'n' Auth" and a newer OAuth version. From your screenshot it appears that you have generated an "Auth 'n' Auth" token. This token won't work with the new RESTFul APIs. You need to select the "OAuth (new security)" option at the top of the page, before generating the token. –  May 26 '17 at 14:15
  • @DavidT.Sadler as per your comments I have generated the token using OAuth but still getting the issue. I have reused the code you have posted in your answer. Any suggestion? – Shahzeb Khan Nov 01 '17 at 09:32
  • @shubhamjain did you resolve your issue? can you please post your solution? – Shahzeb Khan Nov 01 '17 at 10:43
  • there are some very useful infos in this thread: https://github.com/davidtsadler/ebay-sdk-php/issues/212 – michabbb Sep 02 '18 at 10:40
  • @shubhamjain This **stackoverflow** very well answered question can really help: https://stackoverflow.com/questions/44603838/ebay-oauth-token-and-refresh-tokens – Avraham Zhurba Dec 26 '18 at 18:08
  • Hello Mr. @DavidT.Sadler , seeking your attention. Please help me in finding error. I have implemented your code. Trying to fetch token via insomnia as per your suggestion. Bu, it throws error. Here is my question: https://stackoverflow.com/questions/69157596/ebay-api-error-authorization-is-throwing-the-following-error-invalid-grant I have used your suggested code and trying to authorize user via insomnia. But, It's throwing 2 different error for different Authorization value. Please take a look the following screenshot. prntscr.com/1rxmqyv prntscr.com/1rxmedg prntscr.com/1rxmdxz – tanjiya Sep 13 '21 at 08:59