Wink - Access token issue

Hi,
I am trying to get a Wink Token (v2) so I can use it with Home Assistant. However I am having issues with this. I have tried to do a POST request to https://api.wink.com/oauth2/token with the below body:

{
    "client_secret": "xxxxxxxxxxxxxxxxxxxxx",
    "grant_type": "authorization_code",
    "code": "7xY9SSasrosqGwTeYlja"
}

( I got the ‘code’ from https://api.wink.com/oauth2/authorize?response_type=code&client_id=xxxxxxxxxxxx&state=123&redirect_uri=https://pitangui.amazon.com/api/skill/link/M2JG8VGTIBNOSK)

As a result, I get the error: “invalid_grant”. How can I fix this? I’d really appreciate any help.

HA doesn’t allow you to manually use a token. HA handles the complexity of getting this for you, you can follow the documentation here https://www.home-assistant.io/components/wink/

Could you explain how exactly HA does all the authentication. I would also like to learn how this is done.

It is the standard oauth flow. You are presented with a Wink login page. Once logged in, that page will redirect you to the redirect URI you entered in to your Dev account page. That URL is accessible from HA for a brief amount of time and it collects the code from the URL parameters. That code, along with the client ID and cilient secret are then sent to the Wink API token endpoint to get a token and a refresh token which come back in a JSON body.

Thanks for the help. I am trying to work with the Wink APIs myself now. I have used Postman to turn on my Hue lights successfully, and I generated the node.js code below. However the code isn’t working. Why is this so and how can I fix it?

var options = {
    host: 'api.wink.com',
    method: 'PUT',
    path: '/light_bulbs/3616598/desired_state',
    headers: {
        "Content-Type": "application/json",
        "Authorization": "Bearer xxxxxxxxxxxxxxxxxxxxxx"
    }
};


req = https.request(options, res => {
        res.on("data", chunk => {});
        res.on("end", () => {
            console.log('Success');
        });
    });

req.write(JSON.stringify({ desired_state: { powered: true, brightness: 0.15 } }));

req.end();

I am not sure that path is correct? Should be /light_bulb/3616598 with your post body.

I’ve been looking at https://winkapiv2.docs.apiary.io/#reference/device/desired-state-and-last-reading/update-desired-state, and I see that I need to do ‘PUT’ https://api.wink.com/device_type/device_id/desired_state.

I trust very little in that doc, they haven’t updated it in years. Python-wink makes the request to the device-type/device_id

I updated the path, but unfortunately it didn’t fix the problem. What else do you suggest?

Are you getting a response? If so what?

This is the response I get:

{“data”:{“object_type”:“light_bulb”,“object_id”:“3616598”,“uuid”:“d4126d68-265b-4d41-b5c6-ded1fac1b957”,“created_at”:1525995100,“updated_at”:1526434167,“icon_id”:“66”,“icon_code”:“light_bulb-philips_hue”,“desired_state”:{},“last_reading”:{“connection”:true,“connection_updated_at”:1526434168.0145588,“powered”:false,“powered_updated_at”:1526434168.0145588,“brightness”:0.15,“brightness_updated_at”:1526434168.0145588,“desired_powered_updated_at”:1526434058.6853716,“desired_brightness_updated_at”:1526434058.6853716,“connection_changed_at”:1525995101.017664,“powered_changed_at”:1526434058.6222353,“brightness_changed_at”:1525998360.922579,“desired_powered_changed_at”:1526434058.6853716,“desired_brightness_changed_at”:1526434058.6853716},“subscription”:{“pubnub”:{“subscribe_key”:“sub-c-f7bf7f7e-0542-11e3-a5e8-02ee2ddab7fe”,“channel”:“af81cb228a7b04b27be9654b85116ae807c43c02|light_bulb-3616598|user-787233”}},“light_bulb_id”:“3616598”,“name”:“Light”,“locale”:null,“units”:{},“hidden_at”:null,“capabilities”:{“fields”:[{“type”:“boolean”,“field”:“connection”,“mutability”:“read-only”},{“type”:“boolean”,“field”:“powered”,“mutability”:“read-write”},{“type”:“percentage”,“field”:“brightness”,“mutability”:“read-write”}],“excludes_robot_cause”:true},“triggers”:,“manufacturer_device_model”:“philips_philips_hue_dimmable_light”,“manufacturer_device_id”:“00:17:88:b0:2a:d2_2”,“device_manufacturer”:“philips”,“model_name”:“Hue Dimmable Light”,“upc_id”:“479”,“upc_code”:“philips_hue_dimmable_light”,“primary_upc_code”:“philips_hue_dimmable_light”,“gang_id”:null,“hub_id”:“858204”,“local_id”:“2”,“radio_type”:null,“linked_service_id”:“1040982”,“lat_lng”:[null,null],“location”:null,“order”:0},“errors”:,“pagination”:{}}

The “desired_state” object is empty.

Crazy, I have no idea what’s going on. You are correct it’s a put not a post. Based on what I am seeing it all looks good. Compared to python-wink. Maybe try just setting powered and not powered and brightness just to see.

I did notice though that the 1st parameter in the data object is “object_type”. When I sent a Postman request, the first object I saw was “uuid”. Why are there more parameters in this node.js request than Postman?

They do something stange with user-agent that changes what gets sent back.

I am a bit confused. Why is my response different that Postman’s exactly?

Wink sends back different responses with different user agents it isn’t documented I stumbled accross it. I have no idea why.

Is my Wink response similar to what your response is?

Looks to be, you can compare to what’s here https://github.com/python-wink/python-wink/tree/master/src/pywink/test/devices/api_responses

Does my below JSON request look correct?

req.write(JSON.stringify({ desired_state: { powered: true, brightness: 0.15 } }));

Plain JSON:

{
	"desired_state": {
		"powered": false,
		"brightness": 0.15
	}
}

No “” around the false it’s a Boolean should be false

Lol nevermind. Looks okay.