使用 Guzzle 生成 Box.com 访问令牌

Generate Box.com Access Token Using Guzzle

我正在努力使用 Box.com API 的授权代码生成访问令牌。我尝试了很多方法,使用 Guzzle 向 Box.com OAuth/token API 发出 post 请求,以使用授权代码生成访问令牌。但是,我还没有成功。

这是我正在尝试的以下代码。

use Guzzle\Http\Client;

$client = new Client();

$request = $client->post('http://www.box.com/api/oauth2/token', [], [
    'grant_type' => 'authorization_code',
    'client_id' => 'mobvhx1foqtclyccza4hdvst11lkdcjb',
    'client_secret' => 'fRpwlcRxM5rumDL2WnjA0F69QaiRZxct',
    'code' => 'CSSBd0tJnYLRTowxg0tS4h2mA7Vow0WS'
]);

$response = $request->send();

这是我遇到的错误:(

Fatal error: Uncaught exception 'Guzzle\Http\Exception\ClientErrorResponseException' with message 'Client error response [status code] 400 [reason phrase] Bad Request [url] http://www.box.com/api/oauth2/token' in vendor/guzzle/guzzle/src/Guzzle/Http/Exception/BadResponseException.php:43 Stack trace: #0 vendor/guzzle/guzzle/src/Guzzle/Http/Message/Request.php(145): Guzzle\Http\Exception\BadResponseException::factory(Object(Guzzle\Http\Message\EntityEnclosingRequest), Object(Guzzle\Http\Message\Response)) #1 [internal function]: Guzzle\Http\Message\Request::onRequestError(Object(Guzzle\Common\Event), 'request.error', Object(Symfony\Component\EventDispatcher\EventDispatcher)) #2 vendor/symfony/event-dispatcher/EventDispatcher.php(158): call_user_func(Array, Object(Guzzle\Common\Event), 'request.error', Object(Symfony\Component\EventDispatcher\EventDispatcher)) #3 vendor/symfony/event in vendor/guzzle/guzzle/src/Guzzle/Http/Exception/BadResponseException.php on line 43

任何人都可以帮助我做错什么吗?

我只是通过对所有参数 URL 进行编码并将请求发送到 HTTPS 来解决这个问题。这是正确的代码,可能有人会觉得有用。

$request = $client->post('https://www.box.com/api/oauth2/token', [], [
    'grant_type' => urlencode('authorization_code'),
    'client_id' => urlencode('mobvhx1foqtclyccza4hdvst11lkdcjb'),
    'client_secret' => urlencode('fRpwlcRxM5rumDL2WnjA0F69QaiRZxct'),
    'code' => urlencode('CSSBd0tJnYLRTowxg0tS4h2mA7Vow0WS')
]);