Slide 1

Slide 1 text

Making a self Alexa client 2016.09.14 JAWS-UG IoT専門支部 with JAWS-UG-KOBE IoTサロン 2016-09 feat. Alexa meetup #02. Ver. TOKYO

Slide 2

Slide 2 text

誰やねん。 TiNm’S( ティムズ )と読みます。 プログラマー。 Ruby On Rails、AWSが主戦場。 好きなAWS サービスは、OpsWorks。 花澤香菜ちゃん好き。 JAWS-UG-KOBE コアメンバー。

Slide 3

Slide 3 text

Alexa Voice Service 1. Overview 2. Authentication 3. Establish Connection 4. Request & Response

Slide 4

Slide 4 text

1. Overview

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

2. Authentication

Slide 7

Slide 7 text

Authentication 2.1. Security Profile 2.2. Refresh token と access token 2.3. 認証方法

Slide 8

Slide 8 text

2.1 Security Profile

Slide 9

Slide 9 text

AVSを利用するデバイスの認証情報 Security Profileをamazon.comの認証URLに送信して、 access_tokenとrefresh tokenを取得する。

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

2.2 access token と refresh token

Slide 13

Slide 13 text

access token AVSのAPIをCallする際に付与するセキュリティコード Authorization ヘッダーに付与する。 Atza|IQEBLjAsAhQ3yD47Jkj09BfU_qgNk4 Access tokenの有効期間は1時間

Slide 14

Slide 14 text

refresh token access token の有効期間が過ぎた場合に、再度取得するた めのトークン。 後述する認証方式によっては、必ずしも必要ではない。 Atzr|IQEBLzAtAhUAibmh-1N0EsdqwqwdqdasdvferrE

Slide 15

Slide 15 text

2.3 認証方法

Slide 16

Slide 16 text

3. 認証方法 2種類のログイン方法 Amazon.comの アカウントとパスワードを入 力する。

Slide 17

Slide 17 text

Implicit Grant Client 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%22Spe aker%22,%22productInstanceAttributes%22%3A%7B%22deviceSerialNumber%22%3A%2212345%2 2%7D%7D%7D&response_type=token&redirect_uri=https%3A%2F%2Flocalhost http://localhost/?access_token=Atza%7CIQEBLjAsAhQhzDgHuUPvSgvM&t oken_type=bearer&expires_in=3600&scope=alexa%3Aall Request Response

Slide 18

Slide 18 text

Authorization Code Grant Client 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%22Spe aker%22,%22productInstanceAttributes%22%3A%7B%22deviceSerialNumber%22%3A%2212345%2 2%7D%7D%7D&response_type=code&redirect_uri=https%3A%2F%2Flocalhost Request Response https://localhost/?code=ANdNAVhyhqirUelHGEHA&scope=alexa%3Aall

Slide 19

Slide 19 text

Authorization Code Grant Client 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%22Spe aker%22,%22productInstanceAttributes%22%3A%7B%22deviceSerialNumber%22%3A%2212345%2 2%7D%7D%7D&response_type=code&redirect_uri=https%3A%2F%2Flocalhost Request Response https://localhost/?code=ANdNAVhyhqirUelHGEHA&scope=alexa%3Aall 次のページへつづく

Slide 20

Slide 20 text

Authorization Code Grant https://api.amazon.com/auth/o2/token POST /auth/o2/token HTTP/1.1 Host: api.amazon.com Content-Type: application/x-www-form-urlencoded Cache-Control: no-cache grant_type=authorization_code&code=ANBzsjhYZmNCTeAszagk&client_id=amzn1.application-oa2-client.b91a4d2fd2f64&client_s ecret=6963038c1c2063c33ab9eedc0cf8&redirect_uri=https%3A%2F%2Flocalhost Request [POST] Response HTTP/1.1 200 OK { "access_token": "Atza|IQEBLjAsAhRBejiZKPfn5HO2562GBt26qt23EA", "expires_in": 3600, "refresh_token": "Atzr|IQEBLzAtAhUAibmh-1N0EsdqwqwdqdasdvferrE", "token_type": "bearer" }

Slide 21

Slide 21 text

Implicit Grant access tokenは1時間で無効になる。 amazon.comにログインして再認証が必要。

Slide 22

Slide 22 text

Authorization Code Grant access tokenはrefresh tokenを利用して再認 証なしに更新できる。

Slide 23

Slide 23 text

access token の更新 https://api.amazon.com/auth/o2/token POST /auth/o2/token HTTP/1.1 Host: api.amazon.com Content-Type: application/x-www-form-urlencoded Cache-Control: no-cache grant_type=refresh_token&refresh_token=Atzr%7CIQEBLzAtAhUAibmh-1N0E&client_id=amzn1.application-oa2-client.b91a4d2fd2 f6&client_secret=6963038c1c2063c33ab9eedc0cf822 Request [POST] Response HTTP/1.1 200 OK { "access_token": "Atza|IQEBLjAsAhRBejiZKPfn5HO2562GBt26qt23EA", "expires_in": 3600, "refresh_token": "Atzr|IQEBLzAtAhUAibmh-1N0EsdqwqwdqdasdvferrE", "token_type": "bearer" }

Slide 24

Slide 24 text

3. Establish Connection

Slide 25

Slide 25 text

AVSのAPI プロトコルは HTTP/2 ※ 古いバージョンのAPI(v1)はhttp1.1です。

Slide 26

Slide 26 text

HTTP/2 Client Library が要ります。 https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/docs/managing-an-http-2 -connection C, Java のライブラリが紹介されています。 ラズパイだとPythonで扱えるほうが何かと手間が省けるので、 Hyper。 http://hyper.readthedocs.io/en/latest/

Slide 27

Slide 27 text

Establish and Synchronization

Slide 28

Slide 28 text

Connection :method = GET :scheme = https :path = /v20160207/directives authorization = Bearer {{YOUR_ACCESS_TOKEN}}

Slide 29

Slide 29 text

Synchronization :method = POST :scheme = https :path = /v20160207/events authorization = Bearer {{YOUR_ACCESS_TOKEN}} content-type = multipart/form-data; boundary={{BOUNDARY_TERM_HERE}}

Slide 30

Slide 30 text

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}}--

Slide 31

Slide 31 text

Managing HTTP2 Connection https://developer.amazon.com/public/solutions/alexa/alexa-voice-servic e/docs/managing-an-http-2-connection Constructing HTTP2 requests https://developer.amazon.com/public/solutions/alexa/alexa-voice-servic e/docs/avs-http2-requests

Slide 32

Slide 32 text

Request & Response

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

EventとDirectives AVSのAPIをCallするときに、付与するメッセージのこと。 例) デバイスからAVSにキャプチャした音声を送る場合は、 SpeechRecognizerインターフェースの recognize イベントを使う。 Event Directives AVSからデバイスに対してレスポンスとして送られるメッセージのこと。 AVSが応答した音声は、 SpeechSynthesizerインターフェースのSpeakディレクティブメッセージとともにデ バイスに返される。

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Request

Slide 37

Slide 37 text

{ "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 binary Content-Disposition: form-data; name="audio" Content-Type: application/octet-stream {{BINARY AUDIO ATTACHMENT}}

Slide 38

Slide 38 text

Audio binaryのフォーマット ● 16bit Linear PCM ● 16kHz sample rate ● Single channel ● Little endian byte order

Slide 39

Slide 39 text

Response

Slide 40

Slide 40 text

{ "directive": { "header": { "namespace": "SpeechSynthesizer", "name": "Speak", "messageId": "{{STRING}}", "dialogRequestId": "{{STRING}}" }, "payload": { "url": "{{STRING}}", "format": "{{STRING}}", "token": "{{STRING}}" } } } First Part: json object Second Part: audio binary Content-Disposition: form-data; name="audio" Content-Type: application/octet-stream {{BINARY AUDIO ATTACHMENT}}

Slide 41

Slide 41 text

API Overview https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/con tent/avs-api-overview Authentication https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/doc s/authorizing-your-alexa-enabled-product-from-a-website

Slide 42

Slide 42 text

さいごに

Slide 43

Slide 43 text

宣伝1:JAWS-UG-KOBE ● Atsushi Ando ● Kazuto Takeshita ● Tomoharu Ito https://www.facebook.com/groups/450069605016080/ https://jaws-ug-kobe.doorkeeper.jp/

Slide 44

Slide 44 text

https://github.com/jaws-ug-kobe/ AlexaPi.git Alexa Client for Raspberry PI coded by python 旧API(=v1)でAVSと接続しています。(2016/09/11時点) 宣伝2

Slide 45

Slide 45 text

V2 ブランチにて、新しい API(v20160207)への移行を進めて います。

Slide 46

Slide 46 text

いっしょに開発しませんか?

Slide 47

Slide 47 text

今日は、自作エコーたちを 神戸から持ってきますたw 宣伝3:

Slide 48

Slide 48 text

たくさん、戯れましょうw

Slide 49

Slide 49 text

ありがとうございました。