Amazon Alexa登入授權(Android),amazonandroid
訪問Alexa的API,必須要攜帶AccessToken,也就是必須要登入授權,本文主要記錄Amazon Alexa在Android平台上的登入授權過程。
一、在亞馬遜開發人員平台註冊應用
進入亞馬遜開發人員平台的Alexa欄
https://developer.amazon.com/edw/home.html#/
點擊Alexa Voice Service的Get Started,進入到應用管理頁面
選擇註冊一個產品,我這邊選的是application,然後開始填寫相關資訊。
這裡Application Type ID必須唯一,並且需要記住這個id,在代碼中需要加入這個id,Display Name是授權時使用者會看到的名字。填好進入下一步
建立Profile,Profile應該就是登入授權時要校正的資訊,這裡選擇建立一個,填好資訊進入next
選擇Android/Kindle Settings欄,填寫相關資訊,其中Package和Signature是校正的關鍵,Package是實際Android工程的包名,Signature是簽名的MD5值,debug階段也是需要有一個debug的簽名的。
產生簽名:
keytool -genkey -alias xxx -keyalg RSA -validity 20000 -keystore yyy.keystore
xxx : keystore的alias
20000 : keystore的有效天數
yyy.keystore : keystore的名稱
查看簽名資訊:
keytool -list -v -alias <xxx> -keystore <yyy.keystore>
填好後點擊add,會產生一個key
這個key很重要,也是需要匯入的Android工程中的,具體匯入見後文。後面完善好資訊項目就建立完成了,接下來就需要在Android工程中添加相關代碼了。
二、添加登入授權相關代碼到Android工程
1、下載login with Amazon的sdk,:https://developer.amazon.com/sdk-download,下載後加入到工程中。
2、manifest中增加網路存取權限
<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
3、manifest中聲明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、添加Key
在assets目錄下建立api_key.txt檔案,內容為之前profile中的key
5、增加登入授權相關代碼
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);//這裡的PRODUCT_ID就是之前申請的Application ID
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 */
} else {
/* The user is not signed in */
} }
@Override
public void onError(AuthError ae) {
/* The user is not signed in */
} }); }
代碼比較簡單易懂,具體可以參見
https://developer.amazon.com/public/apis/engage/login-with-amazon/docs/use_sdk_android.html