2016.09.14(Wed) JAWS-UG IoT Salon with Alexa meetup #02. Collaborate JAWS-UG IoT dedicated branch with JAWS-UG-KOBE
How to make Alexa self client basics
Making a self Alexa client2016.09.14 JAWS-UG IoT専門支部 with JAWS-UG-KOBEIoTサロン 2016-09feat. Alexa meetup #02.Ver. TOKYO
View Slide
誰やねん。TiNm’S( ティムズ )と読みます。プログラマー。Ruby On Rails、AWSが主戦場。好きなAWS サービスは、OpsWorks。花澤香菜ちゃん好き。JAWS-UG-KOBE コアメンバー。
Alexa Voice Service1. Overview2. Authentication3. Establish Connection4. Request & Response
1. Overview
2. Authentication
Authentication2.1. Security Profile2.2. Refresh token と access token2.3. 認証方法
2.1 Security Profile
AVSを利用するデバイスの認証情報Security Profileをamazon.comの認証URLに送信して、access_tokenとrefresh tokenを取得する。
2.2 access token と refresh token
access tokenAVSのAPIをCallする際に付与するセキュリティコードAuthorization ヘッダーに付与する。Atza|IQEBLjAsAhQ3yD47Jkj09BfU_qgNk4Access tokenの有効期間は1時間
refresh tokenaccess token の有効期間が過ぎた場合に、再度取得するためのトークン。後述する認証方式によっては、必ずしも必要ではない。Atzr|IQEBLzAtAhUAibmh-1N0EsdqwqwdqdasdvferrE
2.3 認証方法
3. 認証方法2種類のログイン方法Amazon.comのアカウントとパスワードを入力する。
Implicit GrantClient IDを送信してログインする。https://www.amazon.com/ap/oa?client_id=amzn1.application-oa2-client.b91a4d2fd2f641f2a15ea469&scope=alexa%3Aall&scope_data=%7B%22alexa%3Aall%22%3A%7B%22productID%22%3A%22Speaker%22,%22productInstanceAttributes%22%3A%7B%22deviceSerialNumber%22%3A%2212345%22%7D%7D%7D&response_type=token&redirect_uri=https%3A%2F%2Flocalhosthttp://localhost/?access_token=Atza%7CIQEBLjAsAhQhzDgHuUPvSgvM&token_type=bearer&expires_in=3600&scope=alexa%3AallRequestResponse
Authorization Code GrantClient ID、Client secretを送信してログインする。https://www.amazon.com/ap/oa?client_id=amzn1.application-oa2-client.b91a4d2fd2f641f2a15ea469&scope=alexa%3Aall&scope_data=%7B%22alexa%3Aall%22%3A%7B%22productID%22%3A%22Speaker%22,%22productInstanceAttributes%22%3A%7B%22deviceSerialNumber%22%3A%2212345%22%7D%7D%7D&response_type=code&redirect_uri=https%3A%2F%2FlocalhostRequestResponsehttps://localhost/?code=ANdNAVhyhqirUelHGEHA&scope=alexa%3Aall
Authorization Code GrantClient ID、Client secretを送信してログインする。https://www.amazon.com/ap/oa?client_id=amzn1.application-oa2-client.b91a4d2fd2f641f2a15ea469&scope=alexa%3Aall&scope_data=%7B%22alexa%3Aall%22%3A%7B%22productID%22%3A%22Speaker%22,%22productInstanceAttributes%22%3A%7B%22deviceSerialNumber%22%3A%2212345%22%7D%7D%7D&response_type=code&redirect_uri=https%3A%2F%2FlocalhostRequestResponsehttps://localhost/?code=ANdNAVhyhqirUelHGEHA&scope=alexa%3Aall次のページへつづく
Authorization Code Granthttps://api.amazon.com/auth/o2/tokenPOST /auth/o2/token HTTP/1.1Host: api.amazon.comContent-Type: application/x-www-form-urlencodedCache-Control: no-cachegrant_type=authorization_code&code=ANBzsjhYZmNCTeAszagk&client_id=amzn1.application-oa2-client.b91a4d2fd2f64&client_secret=6963038c1c2063c33ab9eedc0cf8&redirect_uri=https%3A%2F%2FlocalhostRequest [POST]ResponseHTTP/1.1 200 OK{"access_token": "Atza|IQEBLjAsAhRBejiZKPfn5HO2562GBt26qt23EA","expires_in": 3600,"refresh_token": "Atzr|IQEBLzAtAhUAibmh-1N0EsdqwqwdqdasdvferrE","token_type": "bearer"}
Implicit Grantaccess tokenは1時間で無効になる。amazon.comにログインして再認証が必要。
Authorization Code Grantaccess tokenはrefresh tokenを利用して再認証なしに更新できる。
access token の更新https://api.amazon.com/auth/o2/tokenPOST /auth/o2/token HTTP/1.1Host: api.amazon.comContent-Type: application/x-www-form-urlencodedCache-Control: no-cachegrant_type=refresh_token&refresh_token=Atzr%7CIQEBLzAtAhUAibmh-1N0E&client_id=amzn1.application-oa2-client.b91a4d2fd2f6&client_secret=6963038c1c2063c33ab9eedc0cf822Request [POST]ResponseHTTP/1.1 200 OK{"access_token": "Atza|IQEBLjAsAhRBejiZKPfn5HO2562GBt26qt23EA","expires_in": 3600,"refresh_token": "Atzr|IQEBLzAtAhUAibmh-1N0EsdqwqwdqdasdvferrE","token_type": "bearer"}
3. Establish Connection
AVSのAPI プロトコルは HTTP/2※ 古いバージョンのAPI(v1)はhttp1.1です。
HTTP/2 Client Library が要ります。https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/docs/managing-an-http-2-connectionC, Java のライブラリが紹介されています。ラズパイだとPythonで扱えるほうが何かと手間が省けるので、 Hyper。http://hyper.readthedocs.io/en/latest/
Establish and Synchronization
Connection:method = GET:scheme = https:path = /v20160207/directivesauthorization = Bearer{{YOUR_ACCESS_TOKEN}}
Synchronization:method = POST:scheme = https:path = /v20160207/eventsauthorization = Bearer {{YOUR_ACCESS_TOKEN}}content-type = multipart/form-data;boundary={{BOUNDARY_TERM_HERE}}
Synchronization(つづき)--{{BOUNDARY_TERM_HERE}}Content-Disposition: form-data; name="metadata"Content-Type: application/json; charset=UTF-8{"context": [],"event": {"header": {"namespace": "System","name": "SynchronizeState","messageId": "{{STRING}}"},"payload": { }}}--{{BOUNDARY_TERM_HERE}}--
Managing HTTP2 Connectionhttps://developer.amazon.com/public/solutions/alexa/alexa-voice-service/docs/managing-an-http-2-connectionConstructing HTTP2 requestshttps://developer.amazon.com/public/solutions/alexa/alexa-voice-service/docs/avs-http2-requests
Request & Response
EventとDirectivesAVSのAPIをCallするときに、付与するメッセージのこと。例) デバイスからAVSにキャプチャした音声を送る場合は、 SpeechRecognizerインターフェースのrecognize イベントを使う。EventDirectivesAVSからデバイスに対してレスポンスとして送られるメッセージのこと。AVSが応答した音声は、 SpeechSynthesizerインターフェースのSpeakディレクティブメッセージとともにデバイスに返される。
Request
{"context": [{"header": {"namespace": "AudioPlayer","name": "PlaybackState"},"payload": {"token": "{{STRING}}","offsetInMilliseconds": {{LONG}},"playerActivity": "{{STRING}}"}}],"event": {"header": {"namespace": "SpeechRecognizer","name": "Recognize","messageId": "{{STRING}}","dialogRequestId": "{{STRING}}"},"payload": {"profile": "CLOSE_TALK","format": ""AUDIO_L16_RATE_16000_CHANNELS_1}}}First Part: json object Second Part: audio binaryContent-Disposition: form-data; name="audio"Content-Type: application/octet-stream{{BINARY AUDIO ATTACHMENT}}
Audio binaryのフォーマット● 16bit Linear PCM● 16kHz sample rate● Single channel● Little endian byte order
Response
{"directive": {"header": {"namespace": "SpeechSynthesizer","name": "Speak","messageId": "{{STRING}}","dialogRequestId": "{{STRING}}"},"payload": {"url": "{{STRING}}","format": "{{STRING}}","token": "{{STRING}}"}}}First Part: json object Second Part: audio binaryContent-Disposition: form-data; name="audio"Content-Type: application/octet-stream{{BINARY AUDIO ATTACHMENT}}
API Overviewhttps://developer.amazon.com/public/solutions/alexa/alexa-voice-service/content/avs-api-overviewAuthenticationhttps://developer.amazon.com/public/solutions/alexa/alexa-voice-service/docs/authorizing-your-alexa-enabled-product-from-a-website
さいごに
宣伝1:JAWS-UG-KOBE● Atsushi Ando● Kazuto Takeshita● Tomoharu Itohttps://www.facebook.com/groups/450069605016080/https://jaws-ug-kobe.doorkeeper.jp/
https://github.com/jaws-ug-kobe/AlexaPi.gitAlexa Client for Raspberry PI coded by python旧API(=v1)でAVSと接続しています。(2016/09/11時点)宣伝2
V2 ブランチにて、新しいAPI(v20160207)への移行を進めています。
いっしょに開発しませんか?
今日は、自作エコーたちを神戸から持ってきますたw宣伝3:
たくさん、戯れましょうw
ありがとうございました。