Get the Amazon authorization code
Follow these steps to get the user’s Amazon authorization code from the Alexa app or the LWA authorization server. To get the Amazon authorization code- If your backend supports PKCE, generate the
code_verifierand derive thecode_challengeusing SHA-256, as defined in RFC 7636, before constructing the URLs. - From your app backend, construct the Alexa app URL and the LWA fallback URL with the required query parameters. Include the
statefor cross-site request forgery (CSRF) protection and the PKCEcode_challengeandcode_challenge_methodparameters for enhanced security. - Determine whether the Alexa app is installed on the user’s device.
- If the Alexa app is installed, open the Alexa app URL. If the Alexa app isn’t installed, open the LWA fallback URL in an in-app browser tab, not a native browser app.
- After the user acknowledges the linking request, the Alexa app or LWA redirects the user back to your app at your redirect URL with the Amazon authorization
codeandstate.
For details about how to handle the redirect, see the Apple guidelines on Supporting Universal Links in Your App and Android guidelines in Handling Android App Links. - To prevent CSRF attacks, validate the returned
statevalue against the value you sent in step 1. If your backend supports PKCE, use thecode_verifierto validate the token endpoint, based on the stored PKCE parameters. - Extract the Amazon authorization code from the redirect.
Success response
After the user acknowledges the linking request, the Alexa app or LWA redirects the user back to your app with the Amazon authorization code. The following example shows a successful redirect to your app’s Universal Link or App Link from the Alexa app.Error response
If the user denies the request or an error occurs, the Alexa app or LWA redirects the user back to your app with an error. The following example shows an error redirect:Browser fallback
If the user’s device can’t launch your app by using the redirect URL that you configured, the Alexa app redirects the user to that same redirect URL in their default browser. Fallback might happen, for example, when one of the following occurs:- Universal Links or App Links aren’t enabled in your app.
- Universal Links or App Links aren’t enabled in your app for the redirect URLs that you provided.
- The version of the app on the user’s device is an older version that doesn’t support Universal Links or App Links.
iOS examples
Example: Get the URLs (iOS)
Example: Get the URLs (iOS)
Example: Open the URLs (iOS)
Example: Open the URLs (iOS)
- This example uses the UIApplication.shared.open API to open the URL with the universalLinksOnly option.
- URLs only open when there is an app (in this case, the Alexa app) that’s configured to handle Universal Links with the universalLinksOnly option. You can therefore pass closure expressions in
completionHandlerto handle the case in which the Alexa app doesn’t launch (for example, the Alexa app wasn’t installed or is a version that doesn’t support Universal Links). - If your app isn’t able to launch the Alexa app when using the universalLinksOnly option, your app needs to get the Amazon authorization code by using LWA. For the LWA Fallback URL format, see URLs and endpoints. You can use SFAuthenticationSession on iOS 11 or ASWebAuthenticationSession on iOS 12.0 or higher. This gets the Safari user session and cookies, which avoids an extra login when the user is already logged in to Amazon.com on Safari. This will cause your app to display a window that asks the user’s consent to share website information. iOS defines the content of the consent message.
- For iOS versions below 11.0, this example uses SFSafariViewController to send users to the LWA page. The SFSafariViewController doesn’t ask for the user’s consent to share website information; the Safari browser therefore doesn’t share cookies with the SFSafariViewController.
Example: Open LWA URL (iOS)
Example: Open LWA URL (iOS)
Example: Handle the Universal Link response (iOS)
Example: Handle the Universal Link response (iOS)
Android examples
Example: Get the URLs (Android)
Example: Get the URLs (Android)
Example: Open the URLs (Android)
Example: Open the URLs (Android)
- This example uses the
PackageManagerto verify that the Alexa app is installed and to verify that the installed version of the Alexa app can handle app-to-app account linking. - If the Alexa app isn’t installed or can’t handle app-to-app account linking, your app must get the Amazon authorization code by using the LWA flow. For the LWA fallback URL format, see URLs and endpoints.
- This example opens the URLs by using the Android
ACTION_VIEWintent. The app-to-app URL will open in the Alexa app and the LWA fallback URL will open in the user’s default browser.
Example: Handle the App Link response (Android)
Example: Handle the App Link response (Android)
State generation example
In this example, the app generates and validates the state between the request and the response. You must validate incoming requests by using the state to prevent cross-site request forgery.Errors
If an error occurs when your app tries to get the Amazon authorization code, theredirect_uri that the Alexa app uses to redirect the user back to your app includes an error query parameter, as described by the OAuth 2.0 specification. The Alexa app and the LWA fallback both follow the OAuth 2.0 specification, so they both return the same error codes to your app. For details about why the request to get the Amazon authorization code failed, check the error_description query parameter and the OAuth 2.0 specification description for that error code.
| Error code | Message to show to user | Possible causes |
|---|---|---|
| invalid_request | We are experiencing a problem connecting with Alexa to link your account. Please try again later. | Request is missing state, scope, and/or response_type |
| unauthorized_client | We are experiencing a problem connecting with Alexa to link your account. Please try again later. | The provided client_id doesn’t have access to request consent to link the user’s account |
| access_denied | None. (The user shouldn’t see an error message because they were the one who denied the access.) | The user rejected the request to link their account |
| unsupported_response_type | We are experiencing a problem connecting with Alexa to link your account. Please try again later. | Parameter response_type isn’t equal to code |
| invalid_scope | We are experiencing a problem connecting with Alexa to link your account. Please try again later. | Invalid scope parameter |
| server_error | Sorry, Alexa encountered an unexpected error while trying to link your account. Please try again. | Unexpected server error |
| temporarily_unavailable | Sorry, Alexa encountered a momentary error while trying to link your account. Please try again later. | Temporary server error |

