alexa-samples / alexa-smarthome

Resources for Alexa Smart Home developers.
https://alexa.design/smarthome
Other
682 stars 336 forks source link

implementing ChangeReport #99

Closed madgeni closed 5 years ago

madgeni commented 5 years ago

Hi - wanting to implement ChangeReport. Currently my users make their requests via the lambda handing the requests to my cloud service, and i send the responses back to the lambda. For calls to the Event Gateway, where the data flow has changed (device telling my cloud service a change has occurred) - can my service call the event gateway, or does the lambda have to do it?

madgeni commented 5 years ago

update - had confirmation that this is called from my cloud service - think the wording was confusing in the documentation (my excuse :) ) - however - when i test this (with my valid accesstoken) - i get a 401: "INVALID_ACCESS_TOKEN_EXCEPTION" any ideas? Is this because AcceptGrant hasn't been enabled yet?

madgeni commented 5 years ago

RTFM :)

sskorol commented 5 years ago

@madgeni, I'm struggling with the same thing right now... could you please explain how did you get AcceptGrant code which is required for getting LWA tokens?.. I found in docs that this code is available when you link your account... I've checked Authorization request logs in CloudWatch, copy/pasted it to sample script along with client id / secret from skill's Permissions tab... trying to run this sample and getting:

DEBUG:root:LWA response status: 400
DEBUG:root:LWA response body  : {"error_description":"The request has an invalid grant parameter : code","error":"invalid_grant"}
DEBUG:root:Error calling LWA!

I've also tried to run it from curl, axios and browser rest client... still getting 400 with unsupported_grant_type message... wondering if you can point me out what I'm doing wrong...

madgeni commented 5 years ago

Ok - so here's some code snippets:

switch (event.directive.header.namespace) { case 'Alexa.Authorization':

const usrToken = event.directive.payload.grantee.token; const token = 'Bearer ' + usrToken; const grantCode = event.directive.payload.grant.code;

const requestBody = {
  grant_type: 'authorization_code',
  code: grantCode,
  client_id: '',
  client_secret: '',
};
const formData = querystring.stringify(requestBody);

axios.request({
  url: '/auth/o2/token/',
  method: 'post',
  headers: {
    'content-type': 'application/x-www-form-urlencoded',
  },
  baseURL: 'https://api.amazon.com',
  data: formData,
}).then((result) => {
  const accessToken = result.data.access_token;
  const refreshToken = result.data.refresh_token;

});

These are your tokens for calling alexa events, so you need to store it per user - then retrieve that and call the alexa event call: (Ignore the switch to requestify - it's awaiting refactoring :) ) // I check the region in the handler - const lambdaRegion = context.invokedFunctionArn then set url appropriately ` requestify.request(url, { method: 'POST', headers: { 'content-type': 'application/json;charset=UTF-8', 'Authorization': token, }, body: postData, datatype: 'json', }).then(function(reply) { console.log(reply); const statusCode = reply.getCode(); });

requestify.request(url, { method: 'POST', headers: { 'content-type': 'application/json;charset=UTF-8', 'Authorization': token, }, body: postData, datatype: 'json', }).then(function(reply) { console.log(reply); const statusCode = reply.getCode(); }); `

sskorol commented 5 years ago

When I tried to run tokens' retrieval code directly within Lambda while Auth request processing, my issue has gone. @madgeni thanks a lot for your help!

madgeni commented 5 years ago

Happy to help :)