skip to Main Content

I am trying to authorize eBay user for exchanging token. Unfortunately, it’s not working and returning 2type of error for different aspect. The errors are: ‘invalid grant’ and ‘invalid client’
My code response is 200. But, in postman, it’s showing the following response for 2 different Authorization criteria

Error for .. Authorization: Basic ‘my code’

{
  "error": "invalid_grant",
  "error_description": "the provided authorization grant code is invalid or was issued to another client"
}

Error for .. Authorization: Bearer’my code’

{
  "error": "invalid_client",
  "error_description": "client authentication failed"
}

My Code:

    <?php

    $clientID     = 'client_id';
    $clientSecret = 'client_secret';
    $authCode     = 'authorization_code';

    $url = 'https://api.sandbox.ebay.com/identity/v1/oauth2/token';
    $redirectUrlName = 'redirect_uri';

    $body = http_build_query([
        'grant_type'  => 'authorization_code',
        'code'        => $authCode,
        'redirect_uri'=> $redirectUrlName
    ]);

    $headers = [
        'Cache-Control: no-cache',
        'Accept       : application/json',
        'Content-Type : application/x-www-form-urlencoded',
        'Authorization: Basic '.base64_encode($clientID.':'.$clientSecret)
    ];

    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL            => $url,
        CURLOPT_RETURNTRANSFER => true,
        // CURLOPT_SSL_VERIFYPEER => false,
        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";
    }

    ?>

Same thing worked for me, when I was trying for client credentials.

2

Answers


  1. Chosen as BEST ANSWER

    The code was okay, except 2things.

    Authorization code: This will come from eBay as a Response. We need to fetch the code.

    $headers will have to write as following:

    $headers = [
    'Cache-Control: no-cache',
    'Accept: application/json',
    'Content-Type: application/x-www-form-urlencoded',
    'Authorization: Basic '.base64_encode($clientID.':'.$clientSecret)
    ];
    

    There Will have no space between Cache-Control and Colon(:). That mean, there will have no space at key and colon. Applicable for all key-value pair.


  2. The body section seems to be incorrectly being passed:

        'grant_type'  => 'authorization_code'&,
        'code'        => $authCode&,
        'redirect_uri'=> $redirectUrlName
    

    In a working code for my case, here is how the body is passed:

    CURLOPT_POSTFIELDS => 'grant_type=authorization_code&code=<code value>&redirect_uri=<uri for my test>',
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search