0

Wink API is currently on version 2.

My Question: How can you do a simple "Hello World" with the Wink API V2 via PHP?

Notes:

  • Wink uses PubNub for subscriptions (devices have an event)
  • Uses OAuth2 standard
  • Website/Login is often "hokey": (& will error when you login: "Authentication failed!")
  • You will need to request an Application API key in order to use the API. I followed up with an email to get it approved swiftly.
  • Once you are approved, you'll get: Client ID, Client Secret, & URLs to assist
  • API URL: https://api.wink.com/...
  • Email support: support@wink.zendesk.com (Get Application API key, etc)
  • OAuth 2:

Related Links:

Kevin
  • 2,296
  • 21
  • 22
  • 2
    That is not a question. You got all these links and tuts. What about them didn't work? What have you _actually_ tried? – GolezTrol Nov 02 '18 at 23:56
  • 1
    LOL! For over a week, yes. But I'm posting an answer as we speak so I can hopefully save others this much time. I'll rephrase it into a question to appease... – Kevin Nov 02 '18 at 23:58
  • I didn't downvote the question, because you put in the effort to get all those links, so I guessed there would also have been put quite some effort in actually trying them out. But that last bit didn't show in the question (which is why I asked), and I can imagine how that apparent lack of effort was a reason to downvote for someone. Fortunately you got a good answer to counter for it. ;-) Also, be careful [asking for upvotes](https://meta.stackexchange.com/questions/63439/begging-for-votes), it is genrally frowned upon and may have the averse effect. – GolezTrol Nov 04 '18 at 08:38
  • 1
    Haha, thanks for your sincerity & feedback. I deleted my previous comment since it wouldn't let me edit it. So, how about that upvote GolexTrol ;) (That was a joke!!) Honestly, I didn't know if you were a moderator at first or an actual trol, then I saw your name. But I still don't know! Youre cool with me though :) Oh, for the record, when I first created this "question", yes, I spent a TON of time going through ALL the links and couldnt get any of it "all duct taped together" to work. Much time later, with all those links/resources, I got it going. – Kevin Nov 04 '18 at 16:45
  • I'm neither a troll nor a moderator, just a user like yourself. But I get that sometimes when I utter some criticism, so maybe I should change my name. Or stop commenting.... Nah, I won't do either :p – GolezTrol Nov 04 '18 at 18:12

1 Answers1

2

Information regarding this is extremely limited, so I'll answer my own question hoping to help others. (It took a long time since there wasn't any good info out there.) This example has a user interface (Login required by Wink). I'm hoping someone can post a non-user-interface version (for background scripting, etc).

This will give you raw json output, for you to do with as you wish. This single php page will initially load, take you to Wink's login (you need an account with your devices if this wasn't obvious), after logging it, it will take you back to this same page with a code, call for a token, then use that token to get the device resources.

Create: //[YourServer]/wink_helloworld.php on your http/php server.

wink_helloworld.php:

//Make sure to add this exact URL to your Wink Developer Portal! (https://developer.wink.com/clients)
$redirect_uri = "http://[YourServer]/wink_helloworld.php";
// This is from Wink Developer Portal
$client_id = "abcdefg";
$wink_oauth_url = "https://api.wink.com/oauth2/token";
$client_secret = "hijklmnop";
$devices_url = "https://api.wink.com/users/me/wink_devices";
//need to create a state variable, like a session id. should actually be random tho!!
$randomstring="xyzABC123";
$state = base64_encode($randomstring);
/*_____________________________________________________________________________________________________________________________________ */

echo "<h2>Wink Hello World - Show Devices</h2>";

//If we don't have a code, then send user to login page
if($_GET['code'] == null | $_GET['code'] == ""){
    echo "<a href='https://api.wink.com/oauth2/authorize?response_type=code&client_id=".$client_id."&redirect_uri=$redirect_uri&state=".$state."'>Login</a>";
    return;
}
$code = $_GET['code'];
//if we dont have a token, lets get one
if($access_token == null | $access_token == ""){
    $access_token = getAccessToken();
}
// lets get some data from our devices!
getResource($access_token);
/*_____________________________________________________________________________________________________________________________________ */
// Get token
function getAccessToken() {
    global $wink_oauth_url, $code, $client_secret;
    echo "<b>getAccessToken()</b> Using Code: $code<br>";
    $curl = curl_init();    
    curl_setopt($curl, CURLOPT_URL, $wink_oauth_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_HEADER, FALSE);
    curl_setopt($curl, CURLOPT_POST, TRUE);
    curl_setopt($curl, CURLOPT_POSTFIELDS, "{
        \"client_secret\": \"$client_secret\",
        \"grant_type\": \"authorization_code\",
        \"code\": \"$code\"
    }");
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));    
    $response = curl_exec($curl);
    //var_dump($response);
    formatResults($response);       //debug output
    curl_close($curl);
    return json_decode($response)->access_token;
}
/*_____________________________________________________________________________________________________________________________________ */
// Get Resource(s) with our code & token
function getResource($access_token) {
    global $devices_url;
    echo "<b>getResource()</b> Using Token: $access_token<p>";
    $header = array("Authorization: Bearer {$access_token}");
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $devices_url,
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_RETURNTRANSFER => true
    ));
    $response = curl_exec($curl);
    curl_close($curl);  
    formatResults($response);       //debug output
}

/*_____________________________________________________________________________________________________________________________________ */
//debug formatted output functions
function formatResults($json){
    echo "<pre>";
    echo json_encode(json_decode($json), JSON_PRETTY_PRINT);
    echo "</pre>";
}

?>
Kevin
  • 2,296
  • 21
  • 22