skip to Main Content

This is the extremely vague response I’m getting, while trying to get a user access token:

{
"error":"invalid_request",
"error_description":"request is invalid",
"error_uri":null
}

Here is my code:

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

);

$apiURL = "https://api.sandbox.ebay.com/identity/v1/oauth2/token";
$urlParams = array (
        "grant_type" => "authorization_code",
        "code" => $auth_code,
        "redirect_uri" => "xxx-xxx-xxx-SBX-ccd-xxx"
);
$data_json = json_encode($urlParams);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 1 );

curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data_json );

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

print_r ( $resp );

How can I debug this when it doesn’t even hint at what’s wrong with my request?
I’ve been through this page step-by-step:
https://developer.ebay.com/devzone/rest/ebay-rest/content/oauth-gen-user-token.html#Updating

2

Answers


  1. It seems that you encode your body in Json while the authorization server expects body parameters in POST field format.
    The headers should also contains key/value pairs but you are sending only a list of values

    Could you please try with the following code:

    $headers = array (
            'Authorization' => sprintf('Basic %s',base64_encode(sprintf('%s:%s', $client_id, $client_secret))),
            'Content-Type'  => 'application/x-www-form-urlencoded'
    );
    
    $apiURL = "https://api.sandbox.ebay.com/identity/v1/oauth2/token";
    $urlParams = array (
            "grant_type" => "authorization_code",
            "code" => $auth_code,
            "redirect_uri" => "xxx-xxx-xxx-SBX-ccd-xxx"
    );
    
    $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, 1 );
    
    curl_setopt($ch, CURLOPT_URL, $apiURL);
    curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $urlParams );
    
    $resp = curl_exec ( $ch );
    curl_close ( $ch );
    
    print_r ( $resp );
    
    Login or Signup to reply.
  2. Authorization header should have a following format:

    'Authorization' => sprintf('Basic <%s>',base64_encode(sprintf('%s:%s', $client_id, $client_secret)))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search