Amazon Alexa登錄授權(Android)


訪問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;
@Override
protected 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() {
@Override
public 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);
}
});
}

@Override
protected void onResume() {
    super.onResume();
    requestContext.onResume();
}

@Override
protected 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM