1 year ago
#240756
elamelanin
Can I declare an accesstoken outside the Intenthandler in AlexaSkill
I am trying to get data from strava api, I was unable to acquire the activities but am now looking into getting the stats using the endpoint. I've gotten the accesstoken from the intent handler but I am unable to put it in the 'options' header without hardcoding it as it . The access token changes everyday so I need to use the variable but I cant declare the accesstoken globally to where the options header ca reach.
const ListMyActivityDataHandler = {
canHandle(handlerInput) {
return (
Alexa.getRequestType(handlerInput.requestEnvelope) === "IntentRequest" &&
Alexa.getIntentName(handlerInput.requestEnvelope) ===
"ListMyAcitivityDataIntent"
);
},
async handle(handlerInput) {
const accessToken =
handlerInput.requestEnvelope.context.System.user.accessToken;
let outputSpeech = "This is the default message.";
const Activitiesurl =
"https://www.strava.com/api/v3/athletes/93008019/stats?accessToken=" +
accessToken;
await getActivities(Activitiesurl)
.then((response) => {
const data = JSON.parse(response);
outputSpeech = "the data that I want is ";
})
.catch((err) => {
console.log(`ERROR: ${err.message}`);
});
return handlerInput.responseBuilder.speak(outputSpeech).getResponse();
},
};
const options = {
headers: {
Authorization: `Bearer <Accesstoken goes here> `,
},
};
const getActivities = function (url) {
return new Promise((resolve, reject) => {
const client = url.startsWith("https") ? require("https") : require("http");
const request = client.get(url, options, (response) => {
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error("Failed with status code: " + response.statusCode));
}
const body = [];
response.on("data", (chunk) => body.push(chunk));
response.on("end", () => resolve(body.join("")));
});
request.on("error", (err) => reject(err));
});
};
alexa
alexa-skills-kit
alexa-skill
strava
0 Answers
Your Answer