Amazon Alexa skill account linking using IdentityServer4
It took a lot of reading and quite some time to wade though exactly what was required to get Amazon Alexa account linking working with our Identity Server 4 oauth server. Most of the stuff out there was to perform account linking with Amazon's own OAUTH server, and not IdentityServer4.
Well, I finally got to the bottom of it all, and to save you devs valuable time and frustrations, I've laid it all out below:
Alexa voice command → Amazon Lambda function → [Hidden Identity Server 4 call] → Asp.Net Core API → Return Speech back to Alexa to say aloud.
The IDataService is used solely for accessing the database and creating a return dto class.
The ISpeechServer takes the dto class and creates speech from it. For example:
Notice that the Controller is protected with
[Authorize(Policy = AuthSecrets.CadAlexaApi)]
That policy is declared in the Startup.cs
Nothing special in the Program.cs:
Startup.cs:
AuthConfig.cs
AuthSecrets.cs
Clone the IdentityServer4 samples source code from GitHub and copy the Quickstarts, Views and wwwroot folders to your identity server implementation. I previously tried other Quickstarts from other IdentityServer repos, and found this one to be the best. Your mileage may vary...
Nothing special in SeedData.cs
Click on Build, and the ACCOUNT LINKING tab on the left
Select the "Auth Code Grant" with the following options:
Authorization URI: https://url-to-your-identity-server/connect/authorize
Access Token URI: https://url-to-your-identity-server/connect/token
Client ID: ALEXA
Client Secret: take the raw unencrypted string from AuthSecrets[CadAlexaApi].Secret
Client Authentication Scheme: HTTP Basic (Recommended)
Scopes:
Default Access Token Expiration Time: 31536000 Not sure if you can leave this blank or not.
The Redirect URLs shown on your screen are what you need to for Client configuration above.
Install the NPM package node-fetch
Zip the folder up, and upload it to the amazon lambda associated with your skill.
Well, I finally got to the bottom of it all, and to save you devs valuable time and frustrations, I've laid it all out below:
- Create your Asp.Net Core API
- Configure Identity Server 4 for account linking
- Create your Alexa Skill
- Account link your Alexa skill to Identity Server 4. Amazon will take care to call your Identity Server 4 to obtain a token and manage refresh tokens for you.
- Call your API from Alexa.
Alexa voice command → Amazon Lambda function → [Hidden Identity Server 4 call] → Asp.Net Core API → Return Speech back to Alexa to say aloud.
Asp.Net Core API
The controller for your Alexa API should look something like this:The IDataService is used solely for accessing the database and creating a return dto class.
The ISpeechServer takes the dto class and creates speech from it. For example:
Notice that the Controller is protected with
[Authorize(Policy = AuthSecrets.CadAlexaApi)]
That policy is declared in the Startup.cs
Configure Identity Server 4 for account linking
I've separated the identity server 4 from the API and is in a separate solution.Nothing special in the Program.cs:
Startup.cs:
AuthConfig.cs
AuthSecrets.cs
Clone the IdentityServer4 samples source code from GitHub and copy the Quickstarts, Views and wwwroot folders to your identity server implementation. I previously tried other Quickstarts from other IdentityServer repos, and found this one to be the best. Your mileage may vary...
Nothing special in SeedData.cs
Account link your Alexa skill to Identity Server 4
In https://developer.amazon.com/alexa/console/ask/Click on Build, and the ACCOUNT LINKING tab on the left
Select the "Auth Code Grant" with the following options:
Authorization URI: https://url-to-your-identity-server/connect/authorize
Access Token URI: https://url-to-your-identity-server/connect/token
Client ID: ALEXA
Client Secret: take the raw unencrypted string from AuthSecrets[CadAlexaApi].Secret
Client Authentication Scheme: HTTP Basic (Recommended)
Scopes:
- openid
- AlexaApi
- offline_access
Default Access Token Expiration Time: 31536000 Not sure if you can leave this blank or not.
The Redirect URLs shown on your screen are what you need to for Client configuration above.
Call your API from Alexa
I've kept the lambda function as Node.JS.Install the NPM package node-fetch
Zip the folder up, and upload it to the amazon lambda associated with your skill.