使用Guzzle访问Wink API(无法使其工作)

时间:2015-01-22 03:27:48

标签: api curl guzzle

提前感谢您的期待以及您可以给我的任何帮助/指示。

我是编程(和api&s)的新手,我正在尝试与Wink api集成。文档:http://docs.wink.apiary.io/

使用他们的示例我能够完成oauth2身份验证,然后查询设备列表等,但无法使用Guzzle(400错误)我试图使用CommerceGuys Guzzle Oauth2插件。 https://github.com/commerceguys/guzzle-oauth2-plugin

以下是我可以开始工作的示例代码:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://winkapi.quirky.com/oauth2/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n    \"client_id\": \"consumer_key_goes_here\",\n    \"client_secret\": \"consumer_secret_goes_here\",\n    \"username\": \"user@example.com\",\n    \"password\": \"password_goes_here\",\n    \"grant_type\": \"password\"\n}");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$response = curl_exec($ch);
curl_close($ch);

var_dump($response);

我已经尝试了很多方法来使用Guzzle来实现这一目标,但我总是得到400(错误请求)错误。我正在尝试使用的代码目前正在进行测试之中:

use Guzzle\Http\Client;
use CommerceGuys\Guzzle\Plugin\Oauth2\Oauth2Plugin;
use CommerceGuys\Guzzle\Plugin\Oauth2\GrantType\PasswordCredentials;
use CommerceGuys\Guzzle\Plugin\Oauth2\GrantType\RefreshToken;

Route::get('wink', function(){

$oauth2Client = new Client('https://winkapi.quirky.com/oauth2/token');
          $config = array(
              'client_id' => 'xxxxxxxxxx',
              'client_secret' => 'xxxxxxxxxxxxxxx',
              'username' => 'kevin@xxxxxxxxxxs.com',
              'password' => 'xxxxxxxx',
              'grant_type' => 'password'
          );
          $grantType = new PasswordCredentials($oauth2Client, $config);
          $refreshTokenGrantType = new RefreshToken($oauth2Client, $config);
          $oauth2Plugin = new Oauth2Plugin($grantType, $refreshTokenGrantType);

          $client = new Client('https://winkapi.quirky.com');
          $client->addSubscriber($oauth2Plugin);
          $response = $client->get('/users/me/wink_devices')->send();

          var_dump($response); 
});

任何帮助都将不胜感激!!

0 个答案:

没有答案