簡單來說就是
每個跟AVS(Amazon Voice Service ) 互動的裝置都需要先註冊在AVS
然後用註冊的資料(product ID)取得 user_code , token
接著就可以用 token 發給 AVS 詢問一些簡單的問題
也就是所謂的general question.
例如 What time is it? Tell me a joke. Wikipedia Hong Kong
如果你登入的帳號有開發AVS skill 的話那就可以自訂問題以及自訂回應問題的方法
例如發email 或者送API request 到自己的其他服務
取得 user_code 的方法
curl \ --data-urlencode "response_type=device_code" \ --data-urlencode "client_id=amzn1.application-oa2-client.75ecd91677d949f8b473891703fe167b" \ --data-urlencode "scope=alexa:all" \ --data-urlencode "scope_data={\ \"alexa:all\":{\ \"productID\":\"owendock\",\ \"productInstanceAttributes\":{\ \"deviceSerialNumber\":\"12345\"\ }\ }\ }" \ -H "Content-Type: application/x-www-form-urlencoded" -X POST https://api.amazon.com/auth/O2/create/codepair
然後把user code 輸入 https://amazon.com/a/code 進行登入
上面這個是你的裝置沒有瀏覽界面以及輸入裝置的時候的登入方法
官方手冊上面有app, site 最後才是 code-base 的登入方法
本篇所有API 都在 code-base 這邊可以找到說明
取得 token 的方法
登入完成之後就可以用下面 API 取得 token,沒有登入之前嘗試取得 token 會失敗
curl \ --data-urlencode "grant_type=device_code" \ --data-urlencode "device_code=2cfdfd43-4b11-4469-8304-10e38db41d1c" \ --data-urlencode "user_code=FBNJZN" \ -H "Content-Type: application/x-www-form-urlencoded" -X POST https://api.amazon.com/auth/O2/token
沒有登入或者登入失敗會看到下面的錯誤訊息
{"error_description":"The code pair has not been authorized","error":"authorization_pending"}
{"error_description":"The code pair is invalid. Please restart the code pair workflow","error":"invalid_code_pair"}
token 快要過期之前要自己主動refresh token,其實 token 拿到之後立刻refresh token 也不錯回報錯誤,也是拿的到 new token
curl \ --data-urlencode "grant_type=refresh_token" \ --data-urlencode "refresh_token=$AT" \ --data-urlencode "client_id=amzn1.application-oa2-client.75ecd91677d949f8b473891703fe167b" \ -H "Content-Type: application/x-www-form-urlencoded" -X POST https://api.amazon.com/auth/O2/token
拿到 token 之後就可以對 AVS 進行互動
下面是 speechrecognizer 的一個測試 refer here
curl -i --trace-ascii z.out \ -o test1.mp3 \ -H "Authorization: Bearer ${AT}" \ -F "metadata=<test.json;type=application/json; charset=UTF-8" \ -F "audio=<test.wav;type=audio/L16; rate=16000; channels=1" \ "https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize"
聲音檔案規格如下
All captured audio sent to AVS should be encoded as: 16bit Linear PCM 16kHz sample rate Single channel Little endian byte order
test.wav 音檔下載
https://drive.google.com/open?id=1Zt64oUm0xFQfEFkbVnK9Ig7ahAs2vFNf