Need help taking example and moving it to Guzzle

109 views
Skip to first unread message

Kevin Hiser

unread,
Jan 21, 2015, 3:08:11 PM1/21/15
to guz...@googlegroups.com
Thanks in advance for looking and any help/direction you can give me.

I am new to programming (and api's) and am trying to integrate with the Wink api.  Documentation: http://docs.wink.apiary.io/

Using their examples I was able to accomplish oauth2 authentication but can not get that to work with Guzzle (400 error) I am trying to use the CommerceGuys Guzzle Oauth2 plugin. https://github.com/commerceguys/guzzle-oauth2-plugin

Here is the example code that I can get to work:
<?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);

I have tried many ways to use Guzzle to accomplish this but I always get a 400 (Bad Request) error.  My code that I am trying to use is currently in my route for testing purposes:
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' => 'ke...@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); 
});

Any help would be appreciated!!

Reply all
Reply to author
Forward
0 new messages