eBay REST API:在交换访问令牌的授权代码时的invalid_request

时间:2016-12-11 22:33:18

标签: rest ebay ebay-api

我正在尝试通过新的REST API连接到eBay API。

我正在使用一个非常简单的脚本来测试流程,而我正在使用Guzzle。

我关注的指南是example responses

但是,当需要使用访问令牌交换授权代码时,我收到以下响应:

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

我真的不知道该尝试做什么。

这是我正在使用的代码:

<?php

...

if (isset($_GET['code'])) {
    $client = new \GuzzleHttp\Client();

    $authorization = base64_encode($appId . ':' . $certId);
    $code = urlencode($_GET['code']);
    $body = 'grant_type=authorization_code&code=' . $code . '&redirect_uri=' . $ruName;

    $options = [
        \GuzzleHttp\RequestOptions::HEADERS => [
            'Content-Type' => 'application/x-www-form-urlencoded',
            'Authorization' => 'Basic ' . $authorization,
        ],
        \GuzzleHttp\RequestOptions::BODY => $body,
        \GuzzleHttp\RequestOptions::DEBUG => true,
    ];

    try {
        $response = $client->post('https://api.sandbox.ebay.com/identity/v1/oauth2/token', $options);
        die(dump($response->getBody()->__toString(), $response));
    } catch (\Exception $e) {
        die(dump($e,$_GET, $authorization, $body, $options));
    }
}

// Start the authentication redirecting the user to the eBay's sign-in page
$get_request_token_url = 'https://signin.sandbox.ebay.com/authorize'
    . '?client_id=' . $appId
    . '&redirect_uri=' . $ruName
    . '&response_type=code'
    // Scope for User
    . '&scope=' . urlencode(
        'https://api.ebay.com/oauth/api_scope '.
        'https://api.ebay.com/oauth/api_scope/sell.account.readonly '.
        'https://api.ebay.com/oauth/api_scope/sell.account '
    );
header('Location: ' . $get_request_token_url);

由于文档不明确,我还尝试直接在查询字符串中设置eBay返回的准时/临时code(所有其他参数应该在{{1}中) })。我也尝试将它们都发送到body并将它们附加到body但似乎没有任何效果......

我真的不知道还有什么可以尝试。

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

我使用了The League of Extraordinary Packages OAuth 2.0 Client,但为Ebay兼容性修补了一下:

易趣提供商类:

namespace Library\Ras\OAuth2\Client\Provider;

use League\OAuth2\Client\Provider\GenericProvider;

/**
 * Class Ebay
 * @package Library\Ras\OAuth2\Client\Provider
 */
class EbayProvider extends GenericProvider
{

    protected function getAccessTokenOptions(array $params)
    {
        $options = [
            'headers' => [
                'Accept' => 'application/json',
                'Content-Type' => 'application/x-www-form-urlencoded',
                'Authorization' => sprintf(
                    'Basic %s',
                    base64_encode(sprintf('%s:%s', $params['client_id'], $params['client_secret']))
                ),
            ],
        ];

        unset($params['client_id'], $params['client_secret']);

        if ($this->getAccessTokenMethod() === self::METHOD_POST) {
            $options['body'] = $this->getAccessTokenBody($params);
        }

        return $options;
    }
}

如果您使用的是Guzzle&lt; 6.0,然后需要自定义请求工厂:

namespace Library\Ras\OAuth2\Tool;

use GuzzleHttp\Message\MessageFactory;
use GuzzleHttp\Message\Request;
use League\OAuth2\Client\Tool\RequestFactory as BaseRequestFactory;

/**
 * Class RequestFactory
 * @package Library\Ras\OAuth2\Tool
 */
class RequestFactory extends BaseRequestFactory
{

    /**
     * Creates a request using a simplified array of options.
     *
     * @param  null|string $method
     * @param  null|string $uri
     * @param  array $options
     *
     * @return Request
     */
    public function getRequestWithOptions($method, $uri, array $options = [])
    {
        $factory = new MessageFactory();
        return $factory->createRequest($method, $uri, $options);
    }
}

最后:

require __DIR__ . '/../vendor/autoload.php';

$client = new \Library\Ras\OAuth2\Client\Provider\EbayProvider([
    'clientId' => '<clientId>',
    'clientSecret' => '<clientSecret>',
    'redirectUri' => '<RUName>',
    'urlAuthorize' => 'https://signin.ebay.com/authorize',
    'urlAccessToken' => 'https://api.ebay.com/identity/v1/oauth2/token',
    'urlResourceOwnerDetails' => '',
    'scopeSeparator' => ' ',
    'scopes' => [
        'https =>//api.ebay.com/oauth/api_scope',
        'https =>//api.ebay.com/oauth/api_scope/buy.order.readonly',
        'https =>//api.ebay.com/oauth/api_scope/buy.order',
    ],
]);

// Only if your Guzzle version is < 6.0
$client->setRequestFactory(new \Library\Ras\OAuth2\Tool\RequestFactory());

if (array_key_exists('code', $_GET)) {
    $applicationToken = $_GET['code'];
    $accessToken = $client->getAccessToken('authorization_code', [
        'code' => $applicationToken,
    ]);
    echo 'User access token: ' . $accessToken;
} else {
    $url = $client->getAuthorizationUrl();
    header('Location: ' . $url);
}