问题描述
Wink API当前在版本2上.
Wink API is currently on version 2.
我的问题:如何通过PHP使用Wink API V2创建一个简单的"Hello World"?
注释:
- Wink使用PubNub进行订阅(设备具有事件)
- 使用OAuth2标准
- 网站/登录名通常是"hokey" :(登录时&将出现错误:身份验证失败!")
- 在此处登录: https://developer.wink.com &使用Google帐户(或其他任何方式)
- 然后将URL更改为此: https://developer.wink.com/clients
- 有时您必须做几次!!
- Wink uses PubNub for subscriptions (devices have an event)
- Uses OAuth2 standard
- Website/Login is often "hokey": (& will error when you login: "Authentication failed!")
- Login here: https://developer.wink.com & use Google account (or whatever)
- Then change URL to this: https://developer.wink.com/clients
- Sometimes you have to do this a couple times!!
- Wink表示要使用授权码授予类型"
- Dox&示例: https://developer.byu.edu/docs/consume-api/use-api/choose-grant-type
相关链接:
- Wink API: https://winkapiv2.docs.apiary.io/#
- 与Stackoverflow相关的问题:
- Wink API: https://winkapiv2.docs.apiary.io/#
- Stackoverflow related questions:
- How to use Wink API V2 from a non-web app
- Issues with Pubnub + Wink Hub and sensors
- Wink API Subscriptions Stop Sending Overnight
推荐答案
关于此的信息非常有限,因此我将回答我自己的问题,以希望对其他人有所帮助. (花了很长时间,因为那里没有任何好的信息.)本示例具有一个用户界面(Wink需要登录).我希望有人可以发布非用户界面版本(用于后台脚本等).
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).
这将为您提供原始的json输出,供您随意处理.这个单一的php页面将首先加载,带您进入Wink的登录名(如果不明显,则需要使用设备帐户),登录后,它将带代码返回至同一页面,要求输入令牌,然后使用该令牌获取设备资源.
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.
在您的http/php服务器上创建////[YourServer]/wink_helloworld.php.
Create: //[YourServer]/wink_helloworld.php on your http/php server.
wink_helloworld.php:
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>"; } ?>
这篇关于PHP中的Wink API v2 Hello World的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!