automatic token refresh, initial stage
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
require('isomorphic-fetch');
|
||||
|
||||
var ObjectID = require ('mongodb').ObjectID;
|
||||
const config = require('../config');
|
||||
var request = require("request");
|
||||
var token = require('./token');
|
||||
|
||||
|
||||
var getBuildStatus = function(skillID){
|
||||
@@ -18,45 +19,102 @@ var getBuildStatus = function(skillID){
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
generateInteractionModel: function(skill){
|
||||
try{
|
||||
let result = {};
|
||||
let allIntents = [];
|
||||
let defaultIntents = [{
|
||||
name: "AMAZON.CancelIntent",
|
||||
samples: []
|
||||
},
|
||||
{
|
||||
name: "AMAZON.HelpIntent",
|
||||
samples: []
|
||||
},
|
||||
{
|
||||
name: "AMAZON.StopIntent",
|
||||
samples: []
|
||||
}];
|
||||
|
||||
/*
|
||||
defaultIntents.map(intent=>{
|
||||
allIntents.push(intent);
|
||||
});
|
||||
*/
|
||||
|
||||
skill.intents.map(intent=>{
|
||||
allIntents.push({name: intent.intentName, samples: intent.questions});
|
||||
});
|
||||
|
||||
result.interactionModel = {};
|
||||
|
||||
result.interactionModel.languageModel = {
|
||||
invocationName: skill.invocationName,
|
||||
intents: allIntents
|
||||
};
|
||||
|
||||
return JSON.stringify(result);
|
||||
}catch(e){
|
||||
console.log("error generate : " + e);
|
||||
}
|
||||
}
|
||||
var refreshToken = function(db){
|
||||
return new Promise((resolve, reject)=>{
|
||||
var options = { method: 'POST',
|
||||
url: 'https://api.amazon.com/auth/o2/token',
|
||||
headers:
|
||||
{ 'cache-control': 'no-cache',
|
||||
'content-type': 'application/x-www-form-urlencoded' },
|
||||
form:
|
||||
{ grant_type: 'refresh_token',
|
||||
refresh_token: config.REFRESH_TOKEN,
|
||||
client_id: config.CLIENT_ID,
|
||||
client_secret: config.CLIENT_SECRET } };
|
||||
|
||||
request(options, function (error, response, body) {
|
||||
if (error) reject(error);
|
||||
parsedResponse = JSON.parse(body);
|
||||
if (parsedResponse.refresh_token){
|
||||
try{
|
||||
token.updateTokens(parsedResponse.refresh_token, parsedResponse.access_token, parsedResponse.expires_in,db);
|
||||
resolve();
|
||||
}catch(e){
|
||||
reject(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
var generateInteractionModel = function(skill){
|
||||
let result = {};
|
||||
let allIntents = [];
|
||||
let defaultIntents = [{
|
||||
name: "AMAZON.CancelIntent",
|
||||
samples: []
|
||||
},
|
||||
{
|
||||
name: "AMAZON.HelpIntent",
|
||||
samples: []
|
||||
},
|
||||
{
|
||||
name: "AMAZON.StopIntent",
|
||||
samples: []
|
||||
}];
|
||||
|
||||
/*
|
||||
defaultIntents.map(intent=>{
|
||||
allIntents.push(intent);
|
||||
});
|
||||
*/
|
||||
|
||||
skill.intents.map(intent=>{
|
||||
allIntents.push({name: intent.intentName, samples: intent.questions});
|
||||
});
|
||||
|
||||
result.interactionModel = {};
|
||||
|
||||
result.interactionModel.languageModel = {
|
||||
invocationName: skill.invocationName,
|
||||
intents: allIntents
|
||||
};
|
||||
|
||||
return JSON.stringify(result);
|
||||
}
|
||||
|
||||
var uploadSkill = function(skill){
|
||||
return fetch(`https://api.amazonalexa.com/v0/skills/${skill.skillID}/interactionModel/locales/en-US`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: config.TOKEN
|
||||
},
|
||||
body: generateInteractionModel(skill)
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
updateSkill: function(skill, db){
|
||||
console.log("update skill function");
|
||||
|
||||
return new Promise((resolve,reject)=>{
|
||||
if (new Date() / 1000 > config.TOKEN_EXPIRES_IN){
|
||||
refreshToken(db).then(()=>{
|
||||
uploadSkill(skill).then(response=>{
|
||||
resolve(response.status);
|
||||
});
|
||||
}).catch(e=>{
|
||||
reject(e);
|
||||
});
|
||||
}else{
|
||||
uploadSkill(skill).then(response=>{
|
||||
resolve(response.status);
|
||||
}).catch(e=>{
|
||||
reject(e);
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user