To access the Alexa API, you must carry Accesstoken, which is required to login authorization, this article mainly records the Amazon Alexa login authorization process on the Android platform.
First, register the app on the Amazon developer platform
Go to the Alexa bar on the Amazon Developer Portal
https://developer.amazon.com/edw/home.html#/
Click the Get Started of the Alexa Voice service to go to the app management page
Choose to register a product, my side selected is application, and then began to fill in the relevant information.
Here the application Type ID must be unique, and you need to remember this ID, in the code you need to include this ID,Display name is the name that the user will see when authorized. Fill in and go to next.
Creating a profile,profile should be the information you want to verify when you log in to the authorization, choose a new one, fill in the information and enter next
Select the Android/kindle settings column, fill in the relevant information, wherein the package and signature is the key to the verification, packages is the actual Android project name, signature is the MD5 value of the signature, The debug phase is also required to have a debug signature.
To generate a signature:
Keytool -genkey -alias xxx -keyalg RSA -validity 20000 -keystore yyy. KeyStore
Xxx:keystore's Alias
Effective days of 20000:keystore
Name of the Yyy.keystore:keystore
To view signature information:
Keytool-list-v-alias <xxx>-keystore <yyy.keystore>
When you are finished, click Add to generate a key.
This key is very important, but also need to import the Android project , the specific import see below. After the perfect information project is created, the next step is to add the relevant code to the Android project.
Second, add login authorization related code to Android project
1, download login with Amazon sdk,:https://developer.amazon.com/sdk-download, download and add to the project.
2. Increase network access in manifest
<uses-permission android:name= "Android.permission.INTERNET"/><uses-permission android:name= " Android.permission.ACCESS_NETWORK_STATE "/>
3, manifest in the declaration workflowactivity
<activity android:name="com.amazon.identity.auth.device.workflow.WorkflowActivity" android:theme="@android:style/Theme.NoDisplay"android:allowtaskreparenting="true" android:launchmode="singleTask"> <intent-filter> <action android:name="android.intent.action.VIEW"> <category android:name="android.intent.category.DEFAULT"> <category android:name="android.intent.category.BROWSABLE"> <!-- android:host must use the full package name found in Manifest General Attributes --> <data android:host="${applicationId}" android:scheme="amzn"> </intent-filter></activity>
4. Add key
Create a new Api_key.txt file in the assets directory that contains the key from the previous profile
5. Increase the login authorization related code
private RequestContext requestContext;
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestContext = RequestContext.create(this);requestContext.registerListener(new AuthorizeListener() { /* Authorization was completed successfully. */ @Override public void onSuccess(AuthorizeResult result) { /* Your app is now authorized for the requested scopes */
//result.getAccessToken 就是需要的AccessToken
} /* There was an error during the attempt to authorize the application. */ @Override public void onError(AuthError ae) { /* Inform the user of the error */ } /* Authorization was cancelled before it could be completed. */ @Override public void onCancel(AuthCancellation cancellation) { /* Reset the UI to a ready-to-login state */ }});
View Loginbutton = Findviewbyid (R.id.login_with_amazon); Loginbutton.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {
Final Jsonobject scopedata = new Jsonobject ();
Final Jsonobject productinstanceattributes = new Jsonobject ();
try {
Productinstanceattributes.put ("Deviceserialnumber", Settings.Secure.getString (Getcontentresolver (),
Settings.Secure.ANDROID_ID));
Scopedata.put ("Productinstanceattributes", productinstanceattributes);
Scopedata.put ("ProductID", product_id); //The product_id here is the application ID of the previous application
Authorizationmanager.authorize (New Authorizerequest.builder (RequestContext)
. Addscope (Scopefactory.scopenamed ("Alexa:all", Scopedata))
. Forgranttype (AuthorizeRequest.GrantType.ACCESS_TOKEN)
. Shouldreturnuserdata (False)
. build ());
} catch (Jsonexception e) {
LOG.E (TAG, "jsonexception =" +e);
}
});
}
@Overrideprotected void onResume() { super.onResume(); requestContext.onResume();}
@Overrideprotected void onStart(){ super.onStart();
Scope[] scopes = {Alexa_all_scope};
AuthorizationManager.getToken(this, scopes, new Listener<AuthorizeResult, AuthError>() { @Override public void onSuccess(AuthorizeResult result) { if (result.getAccessToken() != null) {//
就是需要的AccessToken
/* The user is signed in */
/* The user is not signed in */
/* The user is not signed in */
} }); }
The code is easy to understand and can be found in more detail
Https://developer.amazon.com/public/apis/engage/login-with-amazon/docs/use_sdk_android.html
Amazon Alexa login License (Android)