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

时间:2015-11-16 22:48:44

标签: php http guzzle box

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

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

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

任何人都可以帮助我在这里做错了吗?

1 个答案:

答案 0 :(得分:1)

我只是通过将所有参数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')
]);