尝试获取eBay用户访问令牌时出现“无效请求”。没有给出具体的错误号

时间:2017-02-10 07:23:19

标签: php oauth-2.0 token ebay

在尝试获取用户访问令牌时,这是我得到的极其模糊的回应:

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

这是我的代码:

$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 );

如果它甚至没有暗示我的请求有什么问题,我怎么能调试呢? 我一步一步地浏览了这个页面: https://developer.ebay.com/devzone/rest/ebay-rest/content/oauth-gen-user-token.html#Updating

2 个答案:

答案 0 :(得分:0)

似乎您在Json中编码您的身体,而授权服务器期望POST字段格式的身体参数。 标题还应包含键/值对,但您只发送值列表

请尝试使用以下代码:

$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 );

答案 1 :(得分:0)

授权标头应具有以下格式:

'Authorization' => sprintf('Basic <%s>',base64_encode(sprintf('%s:%s', $client_id, $client_secret)))