Zigglzworth
Zigglzworth

Reputation: 6813

Amazon Alexa Skills Kit: How do you link with external app account / userId

In an Amazon alexa skill request, there is a userId. I'm trying to understand what this is and if there is some reference for it because I want to link and Amazon Echo user account with an account in my own app and to do this I would have to have some kind of static userId to work with.

Example request:

{
 "version": "1.0",
 "session": {
   "new": false,
   "application": {
   "applicationId": "amzn1.echo-sdk-ams.app.[unique-value-here]"
  },
  "sessionId": "session1234",
  "attributes": {},
  "user": {
    "userId": null //IS THERE A DETAILED REFERENCE OF THIS SOMEWHERE?
  }
},
"request": {
"type": "IntentRequest",
"requestId": "request5678",
"intent": {
  "name": "MyColorIsIntent",
  "slots": {
    "Color": {
      "name": "Color",
      "value": "blue"
    }
  }
}
}
}

Upvotes: 17

Views: 13184

Answers (4)

Abhijit Jagtap
Abhijit Jagtap

Reputation: 2702

In an Amazon alexa skill request i am trying to bind it with username.

As you can not get an actual user name. But, to be more complete there are different ways that are used to do what you are trying to to. It would depend on what you're skill is trying to accomplish to know which is the approach for you.

Your options are:

  1. Linking a user name in your system to the Alexa user. In that case you do not get a user name. You get a token, and you would link the user name together in your system using that token. Read: https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/linking-an-alexa-user-with-a-user-in-your-system

  2. If what you're trying to do is just to uniquely identify one user from another, but you don't care or need any specific information on that user. The request comes with a user (session.user.userId). It's a random character string that will identify that user for all requests they send, but tells you nothing about them. Read: https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/alexa-skills-kit-interface-reference

  3. You could have an intent which would be something like "SetUser" and let the user speak to you the username they would like. Then you could use #2 above to link them together in some data storage somewhere. I don't know if Amazon supports this route though as it would really be a way to circumvent #1. So, it depends on what you're trying to do. If you need personal information on the user use #1. If you just need to know that it's some user vs another user use #2.

Upvotes: 0

JustinAngel
JustinAngel

Reputation: 16092

Great question.

Short answer: You're going to have to build your own pairing between your 3rd party user and the Alexa UserID. There's no built-in support into the Alexa Skills SDK that lets you associate an Alexa UserID with your user ID. You're going to have to create a specific voice intent that associates Alexa UserIDs to your Users DB.

Longer answer: Let's start by talking about that Alexa UserID you get in each request. The Alexa UserID you get is an LWA (Login-With-Amozon) user ID. It's primary purpose to allow Alexa Skills to reliably detect repeating users.

So what doesn't work? The issue you're going to run into is that the LWA userId is always anonymized to each Alexa app. That's important because it makes sure users aren't tracked; but it also prevents you from associating the Alexa userID with your own LWA userID.

From the "Login with Amazon - Developer Guide" (page 10)

Every company that creates websites or apps for Login with Amazon gets the same user_id for a customer. However, when a customer logs in to another company's app or site, the user_id will be different. This is so user_id cannot be used to track customers across the Web.

What I'm trying to say is that you can't just implement LWA in iOS, Android or Web apps and expect to get the same LWA userId for an account as you would get as an Alexa userID. For example, if you implemented LWA on your Android app an had [email protected] user login to their Amazon account you might get amzn1.account.123456 as a userID, but when that same [email protected] user talks to their paired Echo you'll get a amzn1.account.98765 or any other totally different userId. I actually wasted two days building this architecture which is how I know it doesn't work.

So what does work? A voice-centric variation of Pin authentication seems best.

Let's look at another space of apps with a similar problem: TV apps (xbox, android TV, etc). A lot of those apps require you to login in order to get access to content (e.g. hulu, netflix, etc). But using a remote control to enter a username and password is just plain old bad UX. So what did we do for TV apps? Users go to myService.com/tv, login to their account and get a special short, numerical and time-sensitive pin code they can input to their TV.

When I was implementing an Alexa Skill we decided to take a similar approach. Users would login into our website, iOS app or Android app, go to a dedicated Echo page and then get a pin code. The on-screen instructions that would read something like this:

Go to your Echo and say:

'Launch foo'

'My pin is one two three four'

In our foo skill we have a PairingIntent intent listen to "my pin is {one two three four|pinCode}" sample utterance. After receiving a PairingIntent we'd check if that pin code was valid and if so associated that Alexa userID with our own users DB. If the pin was valid Echo would say something like "Oh, hi there bob! You now have access to all your awesome stuff.". If the pin code wasn't valid alexa would prompt users to try again.

Hopefully this makes sense. There are other options to associate 3rd party accounts with Alexa Skills but this voice-pin approach is the simplest.

Upvotes: 30

Lucas
Lucas

Reputation: 2886

I don't know why the original answer has been deleted but Amazon now lets you link an Alexa User with a user in your system. Here's the announcement.

How End Users Set Up Account Linking for a Skill

Users link their accounts using the Amazon Alexa app. Note that users must use the app. There is no support for establishing the link solely by voice.

Users normally start the process when initially enabling your skill in the app:

  1. In the Alexa app, the user enables the skill.
  2. The app displays your login page right within the app, using the authorization URL you provide when registering your skill on the developer portal. When the companion app calls this URL, it includes state, client_id, and scope as query string parameters.

    • The state is used by the Alexa service during the account linking process. Your page needs to keep track of this value, as you must
      pass it back later.
    • The client_id is defined by you. Your login page can use this to determine that the request came from your Alexa skill.
    • The scope is an optional list of access scopes indicating the level of access requested. You define the set of scopes to support when enabling account linking for your skill.
  3. The user logs in using their normal credentials for your site.

  4. Your service authenticates the user and then generates an access token that uniquely identifies the user in your system.

  5. Your service redirects the user to an Amazon-specific URL and passes along the state, access_token, and token_type in the URL fragment.

  6. The Alexa service validates the returned information and then saves the access_token for the Alexa user.

At this point, the skill is enabled, the user’s Alexa account is linked to the account in your service, and the skill is ready to be used.

Upvotes: 13

Joseph Jaquinta
Joseph Jaquinta

Reputation: 2138

The userid given to the Alexa skill is guaranteed to be unique per user. It is anonymized according to the developer ID, so it will be the same across all of your skills, but it will differ from developer to developer. There is no way to explicitly link it back to any real world ID. You have to do that yourself. My general recommendation is to do it like Firefox sync. If both your Alexa skill and your application share a back-end, then when a user wants to sync from Alexa, generate a 4-character sync code, store it in the back end, read it to the user, and tell them to go to the web site and type it in. On the web site, when they give a sync code, match it up and form your link between the two. I talk about this in a little more detail in my book, but that's the gist of it.

Upvotes: 0

Related Questions