Last active
May 9, 2022 11:57
-
-
Save bbezerra82/b46e483023f5d15dfb7edb90bf682f66 to your computer and use it in GitHub Desktop.
Account Linking Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
web authorization URI: https://www.amazon.com/ap/oa | |
access token URI: https://api.amazon.com/auth/o2/token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https'); | |
.addRequestInterceptors(function (handlerInput) { | |
console.log(`\n********** REQUEST *********\n${JSON.stringify(handlerInput, null, 4)}`); | |
}) | |
.addResponseInterceptors(function (request, response) { | |
console.log(`\n************* RESPONSE **************\n${JSON.stringify(response, null, 4)}`); | |
}) | |
const { accessToken } = handlerInput.requestEnvelope.session.user; | |
let speakOutput = ''; | |
if (typeof accessToken !== "undefined") { | |
const info = await getUserInfo(accessToken); | |
console.log(`info: ${JSON.stringify(info)}`) | |
const { name } = info; | |
speakOutput = `Welcome ${name}, you can say Hello or Help. Which would you like to try?`; | |
} else { | |
speakOutput = 'Welcome, you can say Hello or Help. Which would you like to try?'; | |
} | |
async function getUserInfo(accessToken) { | |
return new Promise((resolve, reject) => { | |
const options = { | |
"method": "GET", | |
"hostname": "api.amazon.com", | |
"path": "/user/profile", | |
"headers": { | |
"Authorization": `Bearer ${accessToken}` | |
} | |
}; | |
let req = https.request(options, (response) => { | |
let returnData = ''; | |
response.on('data', (chunk) => { | |
returnData += chunk; | |
}); | |
response.on('end', () => { | |
resolve(JSON.parse(returnData)); | |
}); | |
response.on("error", (error) => { | |
reject(error) | |
}) | |
}) | |
req.end(); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment