Home
%3CLINGO-SUB%20id%3D%22lingo-sub-318127%22%20slang%3D%22en-US%22%3EAlexa%20%2B%20Azure%20Functions%20%2B%20Microsoft%20Graph%20%3D%20a%20smarter%20assistant!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-318127%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Dec%2005%2C%202018%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EOn%20this%20blog%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2F2018%2F11%2F02%2Fbuild-your-first-alexa-skill-with-alexa-net-and-azure-functions-the-basics%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20we%20have%20recently%20learned%20%3C%2FA%3E%20how%20to%20build%20an%20Alexa%20Skill%20using%20Microsoft%20tools%20and%20platforms%2C%20like%20C%23%2C%20Visual%20Studio%20and%20Azure%20Functions.%20The%20skill%20we%20have%20built%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2Ftag%2Falexa%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20in%20the%20previous%20posts%20%3C%2FA%3E%20was%20available%20to%20every%20user%3A%20it%20didn't%20need%20to%20know%20who%20the%20user%20is%20asking%20the%20questions%20in%20order%20to%20return%20a%20proper%20feedback.%20However%2C%20there%20are%20some%20scenarios%20where%20this%20approach%20isn't%20good%20enoguh.%20Think%2C%20for%20example%2C%20to%20a%20skill%20to%20order%20food%20that%20can%20be%20delivered%20at%20home.%20The%20skill%20must%20know%20who%20is%20placing%20the%20order%2C%20to%20charge%20your%20credit%20card%20with%20the%20total%20amount%3B%20or%20to%20pick%20your%20home%20location%20as%20delivery%20address.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20these%20scenarios%2C%20Alexa%20provides%20a%20feature%20called%20%3CSTRONG%3E%20account%20linking%20%3C%2FSTRONG%3E%20.%20When%20the%20user%20enables%20the%20skill%2C%20he%20will%20be%20requested%20to%20authenticate%20to%20a%203rd%20party%20service%20using%20an%20OAuth%202.0%20flow.%20Once%20the%20linking%20is%20successful%2C%20every%20request%20to%20the%20skill%20will%20be%20authenticated%20against%20the%20service%20and%20Alexa%20will%20send%20you%2C%20in%20the%20JSON%20request%2C%20the%20access%20token%20which%20is%20required%20to%20perform%20further%20operations.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20identify%20skills%20which%20require%20account%20linking%20thanks%20to%20the%20message%20displayed%20under%20the%20%3CSTRONG%3E%20Enable%20%3C%2FSTRONG%3E%20button%20in%20the%20Alexa%20Store.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20800px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67942i24F6EBF2F789A979%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20blog%20post%20we're%20going%20to%20see%20how%20to%20implement%20this%20flow%20by%20adding%20support%20to%20Azure%20Active%20Directory%20authentication%20and%20the%20Microsoft%20Graph%20to%20a%20skill.%20We'll%20welcome%20the%20user%20by%20saying%20his%20name%20and%20we'll%20be%20able%20to%20provide%20information%20about%20his%20OneDrive%20storage.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet's%20start!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22the-account-linking%22%20id%3D%22toc-hId-1477859675%22%20id%3D%22toc-hId-1511799492%22%3EThe%20account%20linking%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20setup%20the%20account%20linking%20you%20need%20to%20select%20your%20skill%20on%20the%20%3CA%20href%3D%22https%3A%2F%2Fdeveloper.amazon.com%2Falexa%2Fconsole%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Alexa%20Developer%20Console%20%3C%2FA%3E%20and%20choose%20%3CSTRONG%3E%20Account%20linking%20%3C%2FSTRONG%3E%20from%20the%20menu%20on%20the%20left%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20322px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67944i0FB44FD74B65B7FF%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20first%20part%20of%20the%20page%20you'll%20be%20able%20to%20customize%20the%20configuration%20of%20the%20account%20linking.%20In%20order%20to%20enable%20it%2C%20you%20have%20to%20turn%20on%20the%20first%20option%20%3CSTRONG%3E%20Do%20you%20allow%20users%20to%20create%20an%20account%20or%20link%20to%20an%20existing%20account%20with%20you%3F%20%3C%2FSTRONG%3E%20.%20This%20will%20enable%20the%20message%20you%20have%20seen%20in%20the%20previous%20image%20in%20the%20Alexa%20Store.%20%3CBR%20%2F%3EThe%20second%20option%20can%20be%20used%20if%20your%20skill%20offers%20some%20features%20which%20can%20be%20leveraged%20also%20without%20linking%20an%20account.%20For%20example%2C%20the%20food%20delivery%20skill%20could%20offer%20a%20command%20to%20know%20the%20restaurants%20around%20you%20which%20can%20be%20freely%20invoked%3B%20however%2C%20as%20soon%20as%20you%20try%20to%20order%20some%20food%2C%20you'll%20be%20asked%20to%20link%20your%20account.%20%3CBR%20%2F%3EIn%20our%20scenario%2C%20we%20leave%20this%20option%20turned%20off.%20Our%20skill%20needs%20to%20use%20the%20Microsoft%20Graph%2C%20so%20we%20aren't%20able%20to%20provide%20any%20feature%20if%20the%20user%20hasn't%20login%20with%20his%20Microsoft%20Account.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThen%20make%20sure%20to%20check%20%3CSTRONG%3E%20Auth%20Code%20Grant%20%3C%2FSTRONG%3E%20as%20authorization%20grant%20type%2C%20since%20it's%20the%20safest%20one.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20rest%20of%20the%20page%20is%20dedicated%20to%20configure%20the%20OAuth%202.0%20flow%2C%20which%20will%20be%20triggered%20when%20the%20user%20enables%20the%20skill%20and%20authenticates%20with%20the%203rd%20party%20service.%20Let's%20take%20a%20step%20back%20and%20take%20a%20look%20at%20how%20the%20OAuth%202.0%20flow%20works%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20626px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67946iF58696B1AEBE1116%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20process%20involves%20three%20steps%3A%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3COL%3E%0A%3COL%3E%0A%3CLI%3EFirst%2C%20the%20client%20application%20(in%20our%20case%2C%20the%20skill)%20sends%20an%20authorization%20request%20to%20the%20specific%20endpoint%20exposed%20by%20the%20service.%20The%20user%20will%20see%20a%20popup%20or%20he%20will%20be%20redirected%20to%20a%20different%20page%20(in%20case%20of%20a%20web%20application)%2C%20where%20he%20will%20be%20asked%20to%20login%20with%20his%20credentials.%20This%20page%20belongs%20to%20the%20owner%20of%20the%20service.%20This%20means%20that%20the%20client%20application%20(in%20our%20case%2C%20neither%20Amazon%20nor%20our%20skill)%20will%20never%20get%20access%20to%20the%20credentials%20of%20the%20user.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3COL%3E%0A%3CLI%3EIf%20the%20authorization%20request%20is%20approved%2C%20the%20application%20can%20use%20the%20received%20grant%20to%20request%20an%20access%20token.%20The%20access%20token%20is%20a%20unique%20identifier%20of%20the%20user%20which%2C%20however%2C%20doesn't%20contain%20any%20information%20that%20can%20be%20used%20to%20identify%20his%20credentials.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3COL%3E%0A%3CLI%3EOnce%20we%20have%20the%20access%20token%2C%20we%20can%20now%20finally%20access%20to%20all%20the%20protected%20resources%20exposed%20by%20the%20service.%20The%20access%20token%20will%20be%20included%20as%20authorization%20header%20of%20every%20request%20and%2C%20if%20it's%20valid%2C%20we%20will%20receive%20the%20resource%20we%20have%20asked.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FOL%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EIf%20we%20take%20a%20look%20now%20at%20the%20section%20to%20configure%20the%20OAuth%202.0%20flow%2C%20we%20will%20see%20how%20the%20various%20fields%20match%20the%20flow%20we%20have%20just%20highlighted%3A%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EAuthorization%20URI%20%3C%2FSTRONG%3E%20%3A%20this%20is%20the%20endpoint%20exposed%20by%20the%20service%20to%20ask%20the%20permission%20to%20start%20using%20it.%20You%20send%20a%20request%20with%20all%20the%20details%20to%20identify%20your%20application%20and%2C%20if%20approved%2C%20you%20will%20receive%20back%20the%20authorization%20code.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EAccess%20Token%20URI%20%3C%2FSTRONG%3E%20%3A%20this%20is%20the%20endpoint%20to%20use%20as%20next%20step.%20After%20we%20have%20been%20authorized%20to%20use%20the%20service%2C%20we%20can%20use%20the%20authorization%20code%20to%20send%20a%20request%20to%20this%20URI%20to%20get%20back%20the%20access%20token.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EClient%20ID%20%3C%2FSTRONG%3E%20is%20the%20unique%20identifier%20of%20our%20application.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EClient%20secret%20%3C%2FSTRONG%3E%20is%20a%20password%20which%20has%20been%20provided%20by%20the%20owner%20of%20the%20service%20to%20authenticate%20our%20application.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EScope%20%3C%2FSTRONG%3E%20is%20a%20set%20of%20values%20which%20which%20identiy%20the%20features%20of%20the%20service%20we%20want%20to%20leverage.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EIn%20our%20scenario%2C%20we%20need%20to%20authenticate%20to%20the%20Microsoft%20Graph%20using%20a%20Microsoft%20Account%20or%20an%20Office%20365%20account.%20Where%20can%20we%20find%20all%20the%20information%20we%20need%20to%20fill%20the%20Account%20Linking%20page%20in%20the%20Amazon%20portal%3F%20%3CBR%20%2F%3EIn%20order%20to%20leverage%20authentication%20with%20a%203d%20party%20service%2C%20we%20need%20to%20register%20our%20application%20against%20it.%20For%20this%20reason%2C%20all%20the%203rd%20party%20services%20which%20provide%20authentication%20(Microsoft%2C%20Google%2C%20Twitter%2C%20GitHub%2C%20etc.)%20offer%20a%20developer%20portal%20that%20can%20be%20used%20to%20register%20an%20application%20and%20to%20obtain%20all%20the%20credentials%20we%20need%20to%20implement%20the%20authentication%20flow.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20one%20offered%20by%20Microsoft%20to%20register%20an%20application%20which%20needs%20to%20support%20Azure%20AD%20authentication%20is%20available%20in%20the%20Azure%20Portal.%20As%20such%2C%20the%20first%20step%20is%20to%20login%20to%20%3CSTRONG%3E%20%3CA%20href%3D%22https%3A%2F%2Fportal.azure.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fportal.azure.com%20%3C%2FA%3E%20%3C%2FSTRONG%3E%20using%20the%20account%20which%20you%20want%20to%20use%20to%20register%20your%20application%20(Microsoft%20Account%20or%20Office%20365).%20%3CBR%20%2F%3EOnce%20you%20are%20logged%20into%20Azure%2C%20open%20the%20%3CSTRONG%3E%20Azure%20Active%20Directory%20%3C%2FSTRONG%3E%20section%20and%20choose%20%3CSTRONG%3E%20App%20registrations%20(Preview)%20%3C%2FSTRONG%3E%20%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet's%20start%20to%20register%20our%20application.%20Click%20on%20the%20%3CSTRONG%3E%20New%20registration%20%3C%2FSTRONG%3E%20button.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20942px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67948iEAC67628903EBB3B%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%20give%20to%20your%20application%20a%20unique%20name.%20In%20my%20case%2C%20I%20chose%20%3CSTRONG%3E%20MyAlexaSkill%20%3C%2FSTRONG%3E%20.%20Then%20you%20must%20specify%20which%20account%20types%20you%20want%20to%20support%3A%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EAccounts%20in%20the%20organizational%20directory%20only%20%3C%2FSTRONG%3E%20%3A%20if%20you're%20logged%20in%20with%20an%20Office%20365%20account%2C%20you%20will%20be%20able%20to%20create%20an%20application%20which%20is%20authorized%20to%20allow%20the%20login%20only%20for%20users%20from%20your%20organization.%20This%20approach%20is%20useful%20when%20you're%20building%20an%20internal%20app%2C%20which%20my%20be%20used%20only%20by%20people%20from%20your%20company.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EAccounts%20in%20any%20organizational%20directory%20%3C%2FSTRONG%3E%20%3A%20this%20option%20will%20enable%20any%20Office%20365%20user%20to%20login%20with%20their%20account%2C%20regardless%20of%20the%20tenant%20they%20belong%20to.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EAccounts%20in%20any%20organizational%20directory%20and%20personal%20Microsoft%20Account%20%3C%2FSTRONG%3E%20%3A%20this%20option%20will%20enable%20not%20only%20Office%20365%20users%20to%20login%2C%20but%20also%20users%20with%20a%20personal%20Microsoft%20Account.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EIn%20our%20scenario%2C%20let's%20enable%20the%20third%20option.%20We%20want%20to%20enable%20every%20user%20to%20use%20the%20skill%20to%20know%20how%20much%20space%20they%20have%20left%20on%20OneDrive%2C%20regardless%20if%20it's%20the%20personal%20space%20or%20the%20business%20one.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIgnore%2C%20for%20the%20moment%2C%20the%20%3CSTRONG%3E%20Redirect%20URI%20%3C%2FSTRONG%3E%20section.%20We're%20going%20to%20fill%20it%20later.%20Press%20%3CSTRONG%3E%20Register%20%3C%2FSTRONG%3E%20at%20the%20end%20of%20the%20section.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20we%20can%20start%20to%20fill%20the%20various%20field%20in%20the%20Alexa%20Developer%20Portal.%20Let's%20see%20where%20to%20retrieve%20the%20various%20parameters%3A%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EAuthorization%20URI%20%3C%2FSTRONG%3E%20and%20%3CSTRONG%3E%20Access%20Token%20URI%20%3C%2FSTRONG%3E%20%3A%20we%20can%20find%20this%20information%20by%20clicking%20on%20the%20%3CSTRONG%3E%20Endpoints%20%3C%2FSTRONG%3E%20button.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67949iF88C6BC98864B7B5%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFrom%20there%2C%20we%20need%20to%20copy%20the%20two%20URIs%20under%20%3CSTRONG%3E%20OAuth%202.0%20authorization%20endpoint%20(v2)%20%3C%2FSTRONG%3E%20and%20%3CSTRONG%3E%20OAuth%202.0%20token%20endpoint%20(v2)%20%3C%2FSTRONG%3E%20.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67951i2A9EE83F645112EE%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EClient%20Id%20%3C%2FSTRONG%3E%20can%20be%20retrieved%20in%20the%20main%20page%20under%20%3CSTRONG%3E%20Application%20(client)%20ID%20%3C%2FSTRONG%3E%20%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67953i5F51C852ECCCF807%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EClient%20Secret%20%3C%2FSTRONG%3E%20must%20be%20generated%20from%20the%20Azure%20portal.%20Click%20on%20%3CSTRONG%3E%20Certificates%20%26amp%3B%20secrets%20%3C%2FSTRONG%3E%20in%20the%20left%20panel%2C%20then%20click%20on%20%3CSTRONG%3E%20New%20client%20secret%20%3C%2FSTRONG%3E%20.%20Give%20it%20a%20description%20and%20choose%20an%20expiration%20date.%20In%20the%20context%20of%20a%20skill%2C%20you%20can%20safely%20choose%20%3CSTRONG%3E%20Never%20%3C%2FSTRONG%3E%20.%20This%20way%2C%20you%20won't%20have%20to%20remember%20to%20update%2C%20from%20time%20to%20time%2C%20the%20client%20secret%20in%20the%20Alexa%20Developer%20Portal.%20Once%20the%20secret%20has%20been%20generated%2C%20copy%20it%20in%20the%20portal%20and%20store%20it%20also%20somewhere%20safe.%20The%20reason%20is%20that%20this%20is%20the%20only%20time%20you'll%20be%20able%20to%20see%20the%20client%20secret%20in%20clear.%20After%20that%2C%20it%20will%20be%20automatically%20masked%20and%20there%20won't%20be%20any%20way%20to%20see%20it%20again.%20You'll%20be%20forced%20to%20generate%20a%20new%20one%2C%20if%20you%20lose%20it.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20998px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67955i7173E0BF03FEFFF1%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EClient%20Authentication%20Scheme%20%3C%2FSTRONG%3E%20%3A%20keep%20%3CSTRONG%3E%20HTTP%20Basic%20%3C%2FSTRONG%3E%20.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EScope%20%3C%2FSTRONG%3E%20must%20match%20the%20various%20scopes%20that%20we%20have%20enabled%20in%20the%20Azure%20portal.%20You%20can%20see%20them%20by%20clicking%20on%20%3CSTRONG%3E%20API%20permissions%20%3C%2FSTRONG%3E%20in%20the%20Azure%20app%20configuration.%20By%20default%2C%20the%20standard%20one%20is%20%3CSTRONG%3E%20User.Read%20%3C%2FSTRONG%3E%20%2C%20which%20allows%20the%20application%20to%20read%20the%20basic%20information%20of%20the%20user%20profile%2C%20like%20his%20name%20or%20his%20mail%20address.%20If%20you%20want%20to%20add%20more%20scopes%2C%20you%20can%20click%20on%20%3CSTRONG%3E%20Add%20a%20permission%20%3C%2FSTRONG%3E%20and%20explore%20all%20the%20other%20scopes%20that%20are%20offered%20by%20the%20Azure%20services%20or%20by%20your%20custom%20services.%20In%20this%20case%2C%20we're%20building%20a%20skill%20which%20integrates%20with%20the%20Microsoft%20Graph%2C%20so%20you'll%20find%20all%20the%20available%20permissions%20under%20%3CSTRONG%3E%20Microsoft%20Graph%20%3C%2FSTRONG%3E%20%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67956i338ED80EEE5697DF%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EScopes%20are%20categorized%20as%20%3CSTRONG%3E%20Delegated%20permissions%20%3C%2FSTRONG%3E%20and%20%3CSTRONG%3E%20Application%20permissions%20%3C%2FSTRONG%3E%20.%20In%20our%20case%2C%20we%20want%20the%20skill%20to%20access%20to%20the%20API%20as%20the%20signed%20user%2C%20so%20we%20can%20focus%20on%20the%20first%20category.%20From%20there%2C%20you'll%20be%20able%20to%20explore%20all%20the%20available%20scopes%20and%20enable%20the%20ones%20you%20need.%20Pay%20attention%20that%2C%20for%20some%20of%20them%2C%20you%20may%20see%20the%20value%20%3CSTRONG%3E%20Yes%20%3C%2FSTRONG%3E%20in%20the%20%3CSTRONG%3E%20Admin%20consent%20required%20%3C%2FSTRONG%3E%20column.%20If%20you're%20building%20an%20enterprise%20skill%20which%20authenticates%20with%20an%20Office%20365%20account%2C%20the%20administrator%20of%20the%20tenant%20may%20block%20the%20usage%20of%20some%20scopes%20for%20security%20reasons.%20In%20such%20cases%2C%20you%20will%20need%20to%20reach%20him%20to%20ask%20to%20enable%20the%20proper%20permissions.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20you%20have%20defined%20the%20scopes%20you%20need%2C%20you%20will%20need%20to%20copy%20them%20inside%20the%20%3CSTRONG%3E%20Scope%20%3C%2FSTRONG%3E%20field%20of%20the%20Alexa%20Developer%20Portal.%20If%20you%20have%20multiple%20scopes%2C%20you%20can%20click%20on%20the%20%3CSTRONG%3E%20%2B%20%3C%2FSTRONG%3E%20button%20to%20add%20new%20fields.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20last%20step%20requires%20the%20opposite%20process%20compared%20to%20what%20we%20have%20done%20so%20far.%20We'll%20need%20to%20take%20some%20info%20from%20the%20Alexa%20Developer%20Portal%20and%20copy%20them%20to%20the%20Azure%20one.%20In%20the%20OAuth%202.0%20flow%2C%20in%20fact%2C%20once%20the%20authentication%20process%20is%20completed%20the%20service%20will%20forward%20the%20response%20to%20a%20specific%20endpoint%20exposed%20by%20our%20application.%20In%20this%20case%2C%20since%20it's%20an%20Alexa%20skill%2C%20the%20endpoints%20are%20provided%20directly%20by%20Amazon.%20We%20can%20find%20them%20at%20the%20end%20of%20the%20Account%20Linking%20configuration%20page%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67958iE606C06F777B276C%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20need%20to%20copy%20them%20in%20the%20Azure%20Portal%2C%20in%20the%20%3CSTRONG%3E%20Authentication%20%3C%2FSTRONG%3E%20section%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67960i0564658EB621B83E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHit%20%3CSTRONG%3E%20Save%20%3C%2FSTRONG%3E%20to%20complete%20the%20process%2C%20then%20move%20back%20to%20the%20Alexa%20Developer%20Portal%20and%20hit%20%3CSTRONG%3E%20Save%20%3C%2FSTRONG%3E%20also%20there.%20We're%20done!%20Now%20account%20linking%20should%20have%20been%20properly%20enabled.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20how%20the%20configuration%20of%20your%20Alexa%20skill%20should%20look%20like%3A%20%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20980px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67962i46230B35101DFDDF%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20order%20to%20test%20the%20linking%2C%20we%20need%20to%20open%20the%20Alexa%20web%20application%2C%20which%20you%20can%20find%20at%20%3CA%20href%3D%22https%3A%2F%2Falexa.amazon.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Falexa.amazon.com%20%3C%2FA%3E%20.%20Login%20with%20your%20account%2C%20move%20to%20the%20%3CSTRONG%3E%20Skills%20%3C%2FSTRONG%3E%20section%20and%20click%20on%20the%20%3CSTRONG%3E%20Your%20skills%20%3C%2FSTRONG%3E%20button%20on%20the%20top%20right.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EPlease%20note!%20%3C%2FSTRONG%3E%20I%20apologize%20if%20the%20following%20screenshots%20are%20in%20Italian%2C%20but%20it%20seems%20that%20there's%20no%20way%20to%20force%20Amazon%20to%20display%20the%20application%20in%20English%20if%20your%20account%20is%20based%20in%20Italy.%20However%2C%20they%20should%20be%20helpful%20to%20understand%20where%20to%20look%20for%20the%20various%20options.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20998px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67963i6387B54146EA010A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20will%20find%20multiple%20categories%2C%20including%20one%20called%20%3CSTRONG%3E%20Skills%20for%20developers%20%3C%2FSTRONG%3E%20.%20Here%20you%20will%20find%20all%20the%20skills%20you%20have%20created%20in%20the%20Alexa%20Developer%20Portal.%20Look%20for%20the%20one%20you're%20working%20on%20and%20notice%20how%2C%20below%20the%20name%2C%20you%20will%20see%20now%20a%20message%20informing%20that%20account%20linking%20is%20required.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20660px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67965i17E8D445F42623CF%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EClick%20on%20the%20skill%20and%20enable%20it.%20If%20you%20did%20everything%20correctly%2C%20another%20windows%20or%20tab%20of%20your%20browser%20will%20open%20up%20to%20display%20the%20Microsoft%20Account%20authentication%20page.%20Login%20with%20your%20Microsoft%20Account%20or%20Office%20365%20account%20and%20you%20should%20be%20all%20set!%20You%20should%20see%20a%20confirm%20message%20notifying%20you%20that%20your%20account%20is%20now%20linked%20to%20the%20skill.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22handling-the-authentication-in-the-azure-function%22%20id%3D%22toc-hId--1074297286%22%20id%3D%22toc-hId--1040357469%22%3EHandling%20the%20authentication%20in%20the%20Azure%20Function%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20that%20we%20have%20completed%20the%20configuration%20on%20the%20portal%2C%20we%20need%20to%20start%20working%20on%20our%20backend%2C%20which%20is%20hosted%20by%20an%20Azure%20Function.%20We%20need%2C%20in%20fact%2C%20to%20retrieve%20the%20access%20token%20and%20use%20it%20to%20perform%20operations%20with%20the%20Microsoft%20Graph.%20%3CBR%20%2F%3EI%20won't%20explain%20from%20scratch%20how%20to%20build%20an%20Alexa%20Skill%20hosted%20by%20an%20Azure%20Function%2C%20since%20we%20already%20did%20it%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2Ftag%2Falexa%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20in%20the%20previous%20posts%20%3C%2FA%3E%20.%20Here%20we%20will%20focus%20only%20on%20the%20relevant%20snippets%20for%20handling%20the%20authentication%20process.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%20to%20Alexa.NET%20it's%20easy%20to%20retrieve%20the%20token%2C%20since%20it's%20stored%20in%20the%20%3CSTRONG%3E%20Session.User.AccessToken%20%3C%2FSTRONG%3E%20property%20of%20the%20%3CSTRONG%3E%20SkillRequest%20%3C%2FSTRONG%3E%20object%2C%20which%20is%20the%20one%20that%20maps%20the%20JSON%20coming%20from%20Alexa%20with%20all%20the%20details%20about%20the%20request%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3Epublic%20static%20async%20Task%26lt%3BIActionResult%26gt%3B%20Run(%5BHttpTrigger(AuthorizationLevel.Anonymous%2C%20%22post%22%2C%20Route%20%3D%20null)%5D%20HttpRequest%20req%2C%20ILogger%20log)%20%0A%7B%20%0A%20%20%20var%20json%20%3D%20await%20req.ReadAsStringAsync()%3B%20%0A%20%20%20var%20skillRequest%20%3D%20JsonConvert.DeserializeObject%26lt%3BSkillRequest%26gt%3B(json)%3B%20%0A%0A%20%20%20var%20token%20%3D%20skillRequest.Session.User.AccessToken%3B%20%0A%0A%20%20%20%2F%2F%2Fhandle%20the%20request%20and%20return%20the%20response%20%0A%7D%20%3C%2FPRE%3E%0A%3CP%3ENow%20that%20we%20have%20the%20token%2C%20we%20can%20use%20it%20to%20perform%20operations%20against%20the%20Microsoft%20Graph.%20The%20Graph%20is%20nothing%20more%20than%20a%20set%20of%20REST%20endpoints%2C%20which%20you%20can%20call%20by%20sending%20HTTP%20commands.%20Based%20on%20the%20type%20of%20operation%2C%20it%20could%20be%20a%20GET%2C%20a%20POST%2C%20a%20PUT%2C%20etc.%20The%20easiest%20way%20to%20understand%20the%20opportunities%20offered%20by%20the%20Microsoft%20Graph%20is%20to%20use%20%3CA%20href%3D%22https%3A%2F%2Fdeveloper.microsoft.com%2Fen-us%2Fgraph%2Fgraph-explorer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Graph%20Explorer%20%3C%2FA%3E%20%2C%20which%20is%20a%20sort%20of%20playground.%20You%20can%20login%20with%20your%20Microsoft%20Account%20or%20Office%20365%20account%20and%20then%20start%20making%20request%20to%20the%20APIs.%20The%20tool%20will%20offer%20you%20the%20opportunity%20to%20compose%20requests%20and%20to%20observe%20the%20JSON%20response%2C%20other%20than%20discovering%20all%20the%20available%20endpoints.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67967iC379AF0E7759F948%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHowever%2C%20we%20are%20not%20required%20to%20perform%20manual%20HTTP%20requests%20in%20order%20to%20use%20the%20Graph.%20Microsoft%2C%20in%20fact%2C%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoftgraph%3Futf8%3D%25E2%259C%2593%26amp%3Bq%3Dsdk%26amp%3Btype%3D%26amp%3Blanguage%3D%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20provides%20multiple%20SDKs%20%3C%2FA%3E%20which%20encapsulate%20all%20the%20logic%20to%20work%20with%20the%20Graph%2C%20similarly%20to%20how%20Alexa.NET%20abstracts%20all%20the%20JSON%20requests%20and%20responses%20and%20allows%20you%20to%20work%20with%20classes%20and%20objects.%20Of%20course%2C%20we%20have%20also%20%3CA%20href%3D%22https%3A%2F%2Fwww.nuget.org%2Fpackages%2FMicrosoft.Graph%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20a%20SDK%20for%20.NET%20%3C%2FA%3E%20%2C%20so%20we%20can%20easily%20integrate%20it%20in%20our%20Azure%20Function.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERight%20click%20on%20your%20project%20in%20Visual%20Studio%2C%20choose%20%3CSTRONG%3E%20Manage%20NuGet%20packages%20%3C%2FSTRONG%3E%20and%20install%20the%20package%20called%20%3CSTRONG%3E%20Microsoft.Graph%20%3C%2FSTRONG%3E%20.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20object%20that%20we%20can%20use%20to%20perform%20operations%20against%20the%20Graph%20is%20called%20%3CSTRONG%3E%20GraphServiceClient%20%3C%2FSTRONG%3E%20%2C%20which%20is%20included%20in%20the%20%3CSTRONG%3E%20Microsoft.Graph%20%3C%2FSTRONG%3E%20namespace.%20However%2C%20we%20can't%20use%20it%20as%20it%20is.%20We%20need%20to%20use%20an%20authenticated%20client%2C%20so%20we%20will%20need%20to%20supply%20the%20access%20token%20included%20in%20the%20request.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20a%20simple%20setup%20method%20that%20we%20can%20add%20to%20our%20Azure%20Function%3A%3C%2FP%3E%0A%3CPRE%3Epublic%20static%20GraphServiceClient%20GetAuthenticatedClientForUser(string%20token%2C%20ILogger%20logger)%20%0A%7B%20%0A%20%20%20GraphServiceClient%20graphClient%20%3D%20null%3B%20%0A%0A%20%20%20%2F%2F%20Create%20Microsoft%20Graph%20client.%20%0A%20%20%20try%20%0A%20%20%20%7B%20%0A%20%20%20%20%20%20graphClient%20%3D%20new%20GraphServiceClient(%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%22%2C%20new%20DelegateAuthenticationProvider(%20(requestMessage)%20%3D%26gt%3B%20%0A%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20requestMessage.Headers.Authorization%20%3D%20new%20AuthenticationHeaderValue(%22bearer%22%2C%20token)%3B%20%0A%20%20%20%20%20%20%20%20%20return%20Task.CompletedTask%3B%20%0A%20%20%20%20%20%20%7D))%3B%20%0A%0A%20%20%20%20%20%20return%20graphClient%3B%20%0A%20%20%20%7D%20%0A%20%20%20catch%20(Exception%20exc)%20%0A%20%20%20%7B%20%0A%20%20%20%20%20%20logger.LogError(exc%2C%20%22Could%20not%20create%20a%20graph%20client%22)%3B%20%0A%20%20%20%7D%20%0A%0A%20%20%20return%20graphClient%3B%20%0A%7D%20%3C%2FPRE%3E%0A%3CP%3EWe%20create%20a%20new%20instance%20of%20the%20%3CSTRONG%3E%20GraphServiceClient%20%3C%2FSTRONG%3E%20object%2C%20passing%20as%20parameter%3A%3C%2FP%3E%0A%3CP%3E%3CCODE%3E%3C%2FCODE%3E%3C%2FP%3E%0A%3COL%3E%0A%3COL%3E%0A%3CLI%3EThe%20endpoint%20we%20want%20to%20use%20for%20the%20Graph.%20We%20use%20the%20production%20one%2C%20which%20is%20%3CSTRONG%3E%20%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%20%3C%2FA%3E%20%3C%2FSTRONG%3E%20.%20There's%20also%20a%20preview%20one%2C%20which%20provides%20access%20to%20all%20the%20beta%20APIs%2C%20and%20it's%20available%20at%20%3CSTRONG%3E%20%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fbeta%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fgraph.microsoft.com%2Fbeta%20%3C%2FA%3E%20%3C%2FSTRONG%3E%20.%3C%2FLI%3E%0A%3CLI%3EA%20%3CSTRONG%3E%20DelegateAuthenticationProvider%20%3C%2FSTRONG%3E%20%2C%20which%20is%20the%20delegate%20used%20to%20handle%20the%20authentication.%20The%20delegate%20provides%20a%20reference%20to%20the%20HTTP%20request%20which%20is%20sent%20to%20the%20Microsoft%20Graph%2C%20that%20we%20need%20to%20customize%20by%20supplying%20the%20access%20token%20in%20the%20authorization%20header.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FOL%3E%0A%3CP%3ENow%20that%20we%20have%20an%20authenticated%20client%2C%20we%20can%20use%20it%20to%20perform%20operations%20with%20the%20Graph.%20For%20example%2C%20let's%20customize%20the%20welcome%20message%20of%20the%20skill%20by%20adding%20the%20name%20of%20the%20user.%20We%20do%20this%20in%20case%20the%20incoming%20request%20type%20is%20%3CSTRONG%3E%20LaunchRequest%20%3C%2FSTRONG%3E%20%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3Epublic%20static%20async%20Task%26lt%3BIActionResult%26gt%3B%20Run(%5BHttpTrigger(AuthorizationLevel.Anonymous%2C%20%22post%22%2C%20Route%20%3D%20null)%5D%20HttpRequest%20req%2C%20ILogger%20log)%20%0A%7B%20%0A%20%20%20var%20json%20%3D%20await%20req.ReadAsStringAsync()%3B%20%0A%20%20%20var%20skillRequest%20%3D%20JsonConvert.DeserializeObject%26lt%3BSkillRequest%26gt%3B(json)%3B%20%0A%0A%20%20%20%2F%2F%20Verifies%20that%20the%20request%20is%20indeed%20coming%20from%20Alexa.%20%0A%20%20%20var%20isValid%20%3D%20await%20skillRequest.ValidateRequestAsync(req%2C%20log)%3B%20%0A%20%20%20if%20(!isValid)%20%0A%20%20%20%7B%20%0A%20%20%20%20%20%20return%20new%20BadRequestResult()%3B%20%0A%20%20%20%7D%20%0A%0A%20%20%20var%20request%20%3D%20skillRequest.Request%3B%20%0A%20%20%20SkillResponse%20response%20%3D%20null%3B%20%0A%0A%20%20%20var%20token%20%3D%20skillRequest.Session.User.AccessToken%3B%20%0A%20%20%20var%20client%20%3D%20GetAuthenticatedClientForUser(token%2C%20log)%3B%20%0A%0A%20%20%20if%20(request%20is%20LaunchRequest%20launchRequest)%20%0A%20%20%20%7B%20%0A%20%20%20%20%20%20var%20me%20%3D%20await%20client.Me.Request().GetAsync()%3B%20%0A%20%20%20%20%20%20var%20welcomeMessage%20%3D%20%24%22Welcome%20%7Bme.DisplayName%7D%22%3B%20%0A%20%20%20%20%20%20response%20%3D%20ResponseBuilder.Tell(welcomeMessage)%3B%20%0A%20%20%20%7D%20%0A%0A%20%20%20return%20new%20OkObjectResult(response)%3B%20%0A%7D%20%3C%2FPRE%3E%0A%3CP%3EThe%20client%20simply%20maps%20all%20the%20endpoints%20that%20you%20can%20see%20in%20the%20Graph%20Explorer%20as%20objects.%20As%20such%2C%20in%20order%20to%20get%20the%20basic%20profile%20of%20the%20user%20(which%20is%20mapped%20with%20the%20endpoint%20%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2F%20%3C%2FA%3E%20)%2C%20we%20can%20use%20the%20%3CSTRONG%3E%20Me%20%3C%2FSTRONG%3E%20property%20exposed%20by%20the%20client.%20To%20actually%20perform%20the%20request%20we%20need%20to%20call%20the%20%3CSTRONG%3E%20Request()%20%3C%2FSTRONG%3E%20method%2C%20followed%20by%20the%20HTTP%20method%20we%20need%20to%20use.%20In%20this%20case%2C%20the%20%3CSTRONG%3E%20me%20%3C%2FSTRONG%3E%20endpoint%20replies%20to%20a%20HTTP%20GET%2C%20so%20we%20call%20the%20%3CSTRONG%3E%20GetAsync()%20%3C%2FSTRONG%3E%20method.%20%3CBR%20%2F%3EWe%20get%20in%20return%20an%20object%20which%20maps%20all%20the%20properties%20that%20we%20can%20see%20in%20the%20response%20JSON%20in%20Graph%20Explorer.%20For%20our%20scenario%2C%20we%20retrieve%20the%20name%20using%20the%20%3CSTRONG%3E%20DisplayName%20%3C%2FSTRONG%3E%20property%20and%20we%20add%20it%20to%20the%20welcome%20message.%20%3CBR%20%2F%3EThe%20rest%20of%20the%20code%20is%20the%20same%20we've%20seen%20in%20the%20other%20posts%3A%20we%20use%20the%20%3CSTRONG%3E%20ResponseBuilder%20%3C%2FSTRONG%3E%20class%20to%20create%20a%20response%20and%20then%20we%20send%20it%20back%20as%20response%20to%20the%20Alexa%20service%20which%20invoked%20our%20function.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20we%20that%20we%20have%20seen%20how%20to%20handle%20the%20authentication%20in%20the%20backend%2C%20the%20sky%20is%20the%20limit%20%3D)%3C%2Fimg%3E%20For%20example%2C%20if%20we%20follow%20our%20original%20idea%20of%20integrating%20OneDrive%20with%20our%20Alexa%20skill%2C%20we%20can%20create%20a%20new%20intent%20in%20the%20Interaction%20Model%20with%20name%3CSTRONG%3E%20%20Quota%3C%2FSTRONG%3E%20%20and%20then%2C%20in%20the%20function%2C%20handle%20it%20with%20the%20help%20of%20the%20Graph%20client%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3Eif%20(intentRequest.Intent.Name%20%3D%3D%20%22Quota%22)%20%0A%7B%20%0A%20%20%20var%20drive%20%3D%20await%20graphClient.Me.Drive.Request().GetAsync()%3B%20%0A%20%20%20int%20free%20%3D%20(int)(drive.Quota.Remaining.Value%20%2F%201024%20%2F%201024%20%2F%201024)%3B%20%0A%0A%20%20%20var%20quotaMessage%20%3D%20%24%22You%20have%20%7Bfree.ToString()%7D%20GB%20available%22%3B%20%0A%20%20%20response%20%3D%20ResponseBuilder.Tell(quotaMessage)%3B%20%0A%7D%3C%2FPRE%3E%0A%3CP%3EThe%20structure%20of%20the%20code%20is%20the%20same%20as%20the%20previous%20example.%20If%20we%20use%20%3CA%20href%3D%22https%3A%2F%2Fdeveloper.microsoft.com%2Fen-us%2Fgraph%2Fgraph-explore%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Graph%20Explorer%20%3C%2FA%3E%20%2C%20we%20can%20see%20how%20the%20information%20about%20the%20OneDrive%20storage%20of%20the%20user%20is%20available%20through%20the%20%3CSTRONG%3E%20%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2Fdrive%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2Fdrive%20%3C%2FA%3E%20%3C%2FSTRONG%3E%20endpoint.%20This%20endpoint%20is%20mapped%20by%20the%20property%20%3CSTRONG%3E%20Me.Drive%20%3C%2FSTRONG%3E%20%2C%20which%20returns%20a%20%3CSTRONG%3E%20Drive%20%3C%2FSTRONG%3E%20object%20that%20contains%20all%20the%20properties%20of%20the%20storage.%20Thanks%20to%20the%20%3CSTRONG%3E%20Quota%20%3C%2FSTRONG%3E%20property%20we%20can%20get%20the%20information%20we're%20looking%20for%2C%20which%20is%20the%20available%20space%2C%20and%20we%20use%20it%20to%20build%20a%20response%20for%20the%20user.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22wrapping-up%22%20id%3D%22toc-hId-668513049%22%20id%3D%22toc-hId-702452866%22%3EWrapping%20up%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20post%20we%20have%20seen%20how%20to%20enable%20account%20linking%20for%20an%20Alexa%20Skill%2C%20allowing%20it%20to%20identify%20the%20user%20and%20to%20provide%20customized%20responses.%20In%20this%20specific%20example%20we%20have%20leveraged%20AAD%20authentication%20and%20the%20Microsoft%20Graph%2C%20but%20any%20service%20which%20supports%20OAuth%202.0%20for%20the%20authentication%20flow%20is%20supported.%20You%20could%20integrate%20your%20Alexa%20skill%2C%20for%20example%2C%20with%20Twitter%2C%20GitHub%2C%20Facebook%2C%20etc.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%20to%20the%20OAuth%202.0%20configuration%20provided%20in%20the%20Alexa%20Developer%20Portal%2C%20you%20don't%20have%20to%20handle%20the%20authentication%20flow%20on%20your%20own%2C%20by%20manually%20communicating%20with%20the%20various%20authentication%20and%20access%20token%20endpoints.%20You%20just%20have%20to%20provide%20to%20Amazon%20the%20configuration%20of%20your%20service%20and%2C%20automatically%2C%20you%20will%20receive%20the%20access%20token%20in%20the%20body%20of%20the%20request%20which%20is%20delivered%20to%20your%20skill.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20find%20the%20full%20OneDrive%20sample%20on%20GitHub%20at%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoft%2FWindows-AppConsult-samples-PWA%2Ftree%2Fmaster%2FOneDriveAlexaSkill%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fgithub.com%2FMicrosoft%2FWindows-AppConsult-samples-PWA%2Ftree%2Fmaster%2FOneDriveAlexaSkill%20%3C%2FA%3E%20.%20Of%20course%2C%20you%20will%20find%20only%20the%20code%20of%20the%20backend%20Azure%20Function.%20It's%20up%20to%20you%20to%20create%20the%20most%20appropriate%20interaction%20model%20and%20to%20setup%20an%20Azure%20AD%20application%20to%20support%20the%20authentication.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20huge%20thanks%20to%20my%20friend%20%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2Fmarcominerva%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Marco%20Minerva%20%3C%2FA%3E%20%2C%20who%20is%20sharing%20with%20me%20lot%20of%20interesting%20activities%20around%20Alexa%20skills%20and%20who%20helped%20me%20in%20setting%20up%20Account%20Linking.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHappy%20coding!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-318127%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20MSDN%20on%20Dec%2005%2C%202018%20On%20this%20blog%20we%20have%20recently%20learned%20how%20to%20build%20an%20Alexa%20Skill%20using%20Microsoft%20tools%20and%20platforms%2C%20like%20C%23%2C%20Visual%20Studio%20and%20Azure%20Functions.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-318127%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Ealexa%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

First published on MSDN on Dec 05, 2018

On this blog we have recently learned how to build an Alexa Skill using Microsoft tools and platforms, like C#, Visual Studio and Azure Functions. The skill we have built in the previous posts was available to every user: it didn't need to know who the user is asking the questions in order to return a proper feedback. However, there are some scenarios where this approach isn't good enoguh. Think, for example, to a skill to order food that can be delivered at home. The skill must know who is placing the order, to charge your credit card with the total amount; or to pick your home location as delivery address.

 

For these scenarios, Alexa provides a feature called account linking . When the user enables the skill, he will be requested to authenticate to a 3rd party service using an OAuth 2.0 flow. Once the linking is successful, every request to the skill will be authenticated against the service and Alexa will send you, in the JSON request, the access token which is required to perform further operations.

 

You can identify skills which require account linking thanks to the message displayed under the Enable button in the Alexa Store.

 

 

In this blog post we're going to see how to implement this flow by adding support to Azure Active Directory authentication and the Microsoft Graph to a skill. We'll welcome the user by saying his name and we'll be able to provide information about his OneDrive storage.

 

Let's start!

 

The account linking

 

To setup the account linking you need to select your skill on the Alexa Developer Console and choose Account linking from the menu on the left:

 

 

In the first part of the page you'll be able to customize the configuration of the account linking. In order to enable it, you have to turn on the first option Do you allow users to create an account or link to an existing account with you? . This will enable the message you have seen in the previous image in the Alexa Store.
The second option can be used if your skill offers some features which can be leveraged also without linking an account. For example, the food delivery skill could offer a command to know the restaurants around you which can be freely invoked; however, as soon as you try to order some food, you'll be asked to link your account.
In our scenario, we leave this option turned off. Our skill needs to use the Microsoft Graph, so we aren't able to provide any feature if the user hasn't login with his Microsoft Account.

 

Then make sure to check Auth Code Grant as authorization grant type, since it's the safest one.

 

The rest of the page is dedicated to configure the OAuth 2.0 flow, which will be triggered when the user enables the skill and authenticates with the 3rd party service. Let's take a step back and take a look at how the OAuth 2.0 flow works:

 

 

The process involves three steps:



    1. First, the client application (in our case, the skill) sends an authorization request to the specific endpoint exposed by the service. The user will see a popup or he will be redirected to a different page (in case of a web application), where he will be asked to login with his credentials. This page belongs to the owner of the service. This means that the client application (in our case, neither Amazon nor our skill) will never get access to the credentials of the user.

 

    1. If the authorization request is approved, the application can use the received grant to request an access token. The access token is a unique identifier of the user which, however, doesn't contain any information that can be used to identify his credentials.

 

    1. Once we have the access token, we can now finally access to all the protected resources exposed by the service. The access token will be included as authorization header of every request and, if it's valid, we will receive the resource we have asked.



If we take a look now at the section to configure the OAuth 2.0 flow, we will see how the various fields match the flow we have just highlighted:



    • Authorization URI : this is the endpoint exposed by the service to ask the permission to start using it. You send a request with all the details to identify your application and, if approved, you will receive back the authorization code.

 

    • Access Token URI : this is the endpoint to use as next step. After we have been authorized to use the service, we can use the authorization code to send a request to this URI to get back the access token.

 

    • Client ID is the unique identifier of our application.

 

    • Client secret is a password which has been provided by the owner of the service to authenticate our application.

 

    • Scope is a set of values which which identiy the features of the service we want to leverage.



In our scenario, we need to authenticate to the Microsoft Graph using a Microsoft Account or an Office 365 account. Where can we find all the information we need to fill the Account Linking page in the Amazon portal?
In order to leverage authentication with a 3d party service, we need to register our application against it. For this reason, all the 3rd party services which provide authentication (Microsoft, Google, Twitter, GitHub, etc.) offer a developer portal that can be used to register an application and to obtain all the credentials we need to implement the authentication flow.

 

The one offered by Microsoft to register an application which needs to support Azure AD authentication is available in the Azure Portal. As such, the first step is to login to https://portal.azure.com using the account which you want to use to register your application (Microsoft Account or Office 365).
Once you are logged into Azure, open the Azure Active Directory section and choose App registrations (Preview) :

 

 

 

Let's start to register our application. Click on the New registration button.

 

 

First give to your application a unique name. In my case, I chose MyAlexaSkill . Then you must specify which account types you want to support:



    • Accounts in the organizational directory only : if you're logged in with an Office 365 account, you will be able to create an application which is authorized to allow the login only for users from your organization. This approach is useful when you're building an internal app, which my be used only by people from your company.

 

    • Accounts in any organizational directory : this option will enable any Office 365 user to login with their account, regardless of the tenant they belong to.

 

    • Accounts in any organizational directory and personal Microsoft Account : this option will enable not only Office 365 users to login, but also users with a personal Microsoft Account.



In our scenario, let's enable the third option. We want to enable every user to use the skill to know how much space they have left on OneDrive, regardless if it's the personal space or the business one.

 

Ignore, for the moment, the Redirect URI section. We're going to fill it later. Press Register at the end of the section.

 

Now we can start to fill the various field in the Alexa Developer Portal. Let's see where to retrieve the various parameters:



    • Authorization URI and Access Token URI : we can find this information by clicking on the Endpoints button.



 

From there, we need to copy the two URIs under OAuth 2.0 authorization endpoint (v2) and OAuth 2.0 token endpoint (v2) .

 



    • Client Id can be retrieved in the main page under Application (client) ID :





    • Client Secret must be generated from the Azure portal. Click on Certificates & secrets in the left panel, then click on New client secret . Give it a description and choose an expiration date. In the context of a skill, you can safely choose Never . This way, you won't have to remember to update, from time to time, the client secret in the Alexa Developer Portal. Once the secret has been generated, copy it in the portal and store it also somewhere safe. The reason is that this is the only time you'll be able to see the client secret in clear. After that, it will be automatically masked and there won't be any way to see it again. You'll be forced to generate a new one, if you lose it.





    • Client Authentication Scheme : keep HTTP Basic .

 

    • Scope must match the various scopes that we have enabled in the Azure portal. You can see them by clicking on API permissions in the Azure app configuration. By default, the standard one is User.Read , which allows the application to read the basic information of the user profile, like his name or his mail address. If you want to add more scopes, you can click on Add a permission and explore all the other scopes that are offered by the Azure services or by your custom services. In this case, we're building a skill which integrates with the Microsoft Graph, so you'll find all the available permissions under Microsoft Graph :



 

Scopes are categorized as Delegated permissions and Application permissions . In our case, we want the skill to access to the API as the signed user, so we can focus on the first category. From there, you'll be able to explore all the available scopes and enable the ones you need. Pay attention that, for some of them, you may see the value Yes in the Admin consent required column. If you're building an enterprise skill which authenticates with an Office 365 account, the administrator of the tenant may block the usage of some scopes for security reasons. In such cases, you will need to reach him to ask to enable the proper permissions.

 

Once you have defined the scopes you need, you will need to copy them inside the Scope field of the Alexa Developer Portal. If you have multiple scopes, you can click on the + button to add new fields.

 

The last step requires the opposite process compared to what we have done so far. We'll need to take some info from the Alexa Developer Portal and copy them to the Azure one. In the OAuth 2.0 flow, in fact, once the authentication process is completed the service will forward the response to a specific endpoint exposed by our application. In this case, since it's an Alexa skill, the endpoints are provided directly by Amazon. We can find them at the end of the Account Linking configuration page:

 

 

We need to copy them in the Azure Portal, in the Authentication section:

 

 

Hit Save to complete the process, then move back to the Alexa Developer Portal and hit Save also there. We're done! Now account linking should have been properly enabled.

 

This is how the configuration of your Alexa skill should look like:

 

In order to test the linking, we need to open the Alexa web application, which you can find at https://alexa.amazon.com . Login with your account, move to the Skills section and click on the Your skills button on the top right.

 

Please note! I apologize if the following screenshots are in Italian, but it seems that there's no way to force Amazon to display the application in English if your account is based in Italy. However, they should be helpful to understand where to look for the various options.

 

 

You will find multiple categories, including one called Skills for developers . Here you will find all the skills you have created in the Alexa Developer Portal. Look for the one you're working on and notice how, below the name, you will see now a message informing that account linking is required.

 

 

Click on the skill and enable it. If you did everything correctly, another windows or tab of your browser will open up to display the Microsoft Account authentication page. Login with your Microsoft Account or Office 365 account and you should be all set! You should see a confirm message notifying you that your account is now linked to the skill.

 

Handling the authentication in the Azure Function

 

Now that we have completed the configuration on the portal, we need to start working on our backend, which is hosted by an Azure Function. We need, in fact, to retrieve the access token and use it to perform operations with the Microsoft Graph.
I won't explain from scratch how to build an Alexa Skill hosted by an Azure Function, since we already did it in the previous posts . Here we will focus only on the relevant snippets for handling the authentication process.

 

Thanks to Alexa.NET it's easy to retrieve the token, since it's stored in the Session.User.AccessToken property of the SkillRequest object, which is the one that maps the JSON coming from Alexa with all the details about the request:

 

public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log) 
{ 
   var json = await req.ReadAsStringAsync(); 
   var skillRequest = JsonConvert.DeserializeObject<SkillRequest>(json); 

   var token = skillRequest.Session.User.AccessToken; 

   ///handle the request and return the response 
} 

Now that we have the token, we can use it to perform operations against the Microsoft Graph. The Graph is nothing more than a set of REST endpoints, which you can call by sending HTTP commands. Based on the type of operation, it could be a GET, a POST, a PUT, etc. The easiest way to understand the opportunities offered by the Microsoft Graph is to use Graph Explorer , which is a sort of playground. You can login with your Microsoft Account or Office 365 account and then start making request to the APIs. The tool will offer you the opportunity to compose requests and to observe the JSON response, other than discovering all the available endpoints.

 

 

However, we are not required to perform manual HTTP requests in order to use the Graph. Microsoft, in fact, provides multiple SDKs which encapsulate all the logic to work with the Graph, similarly to how Alexa.NET abstracts all the JSON requests and responses and allows you to work with classes and objects. Of course, we have also a SDK for .NET , so we can easily integrate it in our Azure Function.

 

Right click on your project in Visual Studio, choose Manage NuGet packages and install the package called Microsoft.Graph .

 

The object that we can use to perform operations against the Graph is called GraphServiceClient , which is included in the Microsoft.Graph namespace. However, we can't use it as it is. We need to use an authenticated client, so we will need to supply the access token included in the request.

 

Here is a simple setup method that we can add to our Azure Function:

public static GraphServiceClient GetAuthenticatedClientForUser(string token, ILogger logger) 
{ 
   GraphServiceClient graphClient = null; 

   // Create Microsoft Graph client. 
   try 
   { 
      graphClient = new GraphServiceClient("https://graph.microsoft.com/v1.0", new DelegateAuthenticationProvider( (requestMessage) => 
      { 
         requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token); 
         return Task.CompletedTask; 
      })); 

      return graphClient; 
   } 
   catch (Exception exc) 
   { 
      logger.LogError(exc, "Could not create a graph client"); 
   } 

   return graphClient; 
} 

We create a new instance of the GraphServiceClient object, passing as parameter:

    1. The endpoint we want to use for the Graph. We use the production one, which is https://graph.microsoft.com/v1.0 . There's also a preview one, which provides access to all the beta APIs, and it's available at https://graph.microsoft.com/beta .
    2. A DelegateAuthenticationProvider , which is the delegate used to handle the authentication. The delegate provides a reference to the HTTP request which is sent to the Microsoft Graph, that we need to customize by supplying the access token in the authorization header.

Now that we have an authenticated client, we can use it to perform operations with the Graph. For example, let's customize the welcome message of the skill by adding the name of the user. We do this in case the incoming request type is LaunchRequest :

 

public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log) 
{ 
   var json = await req.ReadAsStringAsync(); 
   var skillRequest = JsonConvert.DeserializeObject<SkillRequest>(json); 

   // Verifies that the request is indeed coming from Alexa. 
   var isValid = await skillRequest.ValidateRequestAsync(req, log); 
   if (!isValid) 
   { 
      return new BadRequestResult(); 
   } 

   var request = skillRequest.Request; 
   SkillResponse response = null; 

   var token = skillRequest.Session.User.AccessToken; 
   var client = GetAuthenticatedClientForUser(token, log); 

   if (request is LaunchRequest launchRequest) 
   { 
      var me = await client.Me.Request().GetAsync(); 
      var welcomeMessage = $"Welcome {me.DisplayName}"; 
      response = ResponseBuilder.Tell(welcomeMessage); 
   } 

   return new OkObjectResult(response); 
} 

The client simply maps all the endpoints that you can see in the Graph Explorer as objects. As such, in order to get the basic profile of the user (which is mapped with the endpoint https://graph.microsoft.com/v1.0/me/ ), we can use the Me property exposed by the client. To actually perform the request we need to call the Request() method, followed by the HTTP method we need to use. In this case, the me endpoint replies to a HTTP GET, so we call the GetAsync() method.
We get in return an object which maps all the properties that we can see in the response JSON in Graph Explorer. For our scenario, we retrieve the name using the DisplayName property and we add it to the welcome message.
The rest of the code is the same we've seen in the other posts: we use the ResponseBuilder class to create a response and then we send it back as response to the Alexa service which invoked our function.

 

Now we that we have seen how to handle the authentication in the backend, the sky is the limit =) For example, if we follow our original idea of integrating OneDrive with our Alexa skill, we can create a new intent in the Interaction Model with name Quota and then, in the function, handle it with the help of the Graph client:

 

if (intentRequest.Intent.Name == "Quota") 
{ 
   var drive = await graphClient.Me.Drive.Request().GetAsync(); 
   int free = (int)(drive.Quota.Remaining.Value / 1024 / 1024 / 1024); 

   var quotaMessage = $"You have {free.ToString()} GB available"; 
   response = ResponseBuilder.Tell(quotaMessage); 
}

The structure of the code is the same as the previous example. If we use Graph Explorer , we can see how the information about the OneDrive storage of the user is available through the https://graph.microsoft.com/v1.0/me/drive endpoint. This endpoint is mapped by the property Me.Drive , which returns a Drive object that contains all the properties of the storage. Thanks to the Quota property we can get the information we're looking for, which is the available space, and we use it to build a response for the user.

 

Wrapping up

 

In this post we have seen how to enable account linking for an Alexa Skill, allowing it to identify the user and to provide customized responses. In this specific example we have leveraged AAD authentication and the Microsoft Graph, but any service which supports OAuth 2.0 for the authentication flow is supported. You could integrate your Alexa skill, for example, with Twitter, GitHub, Facebook, etc.

 

Thanks to the OAuth 2.0 configuration provided in the Alexa Developer Portal, you don't have to handle the authentication flow on your own, by manually communicating with the various authentication and access token endpoints. You just have to provide to Amazon the configuration of your service and, automatically, you will receive the access token in the body of the request which is delivered to your skill.

 

You can find the full OneDrive sample on GitHub at https://github.com/Microsoft/Windows-AppConsult-samples-PWA/tree/master/OneDriveAlexaSkill . Of course, you will find only the code of the backend Azure Function. It's up to you to create the most appropriate interaction model and to setup an Azure AD application to support the authentication.

 

A huge thanks to my friend Marco Minerva , who is sharing with me lot of interesting activities around Alexa skills and who helped me in setting up Account Linking.

 

Happy coding!