Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Making a self Alexa client Ver.TOKYO

haruharuharuby
September 14, 2016

Making a self Alexa client Ver.TOKYO

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

haruharuharuby

September 14, 2016
Tweet

More Decks by haruharuharuby

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

  4. 1. Overview

    View Slide

  5. View Slide

  6. 2. Authentication

    View Slide

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

    View Slide

  8. 2.1 Security Profile

    View Slide

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

    View Slide

  10. View Slide

  11. View Slide

  12. 2.2 access token と refresh token

    View Slide

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

    View Slide

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

    View Slide

  15. 2.3 認証方法

    View Slide

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

    View Slide

  17. 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

    View Slide

  18. 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

    View Slide

  19. 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
    次のページへつづく

    View Slide

  20. 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"
    }

    View Slide

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

    View Slide

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

    View Slide

  23. 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"
    }

    View Slide

  24. 3. Establish Connection

    View Slide

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

    View Slide

  26. 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/

    View Slide

  27. Establish and Synchronization

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  31. 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

    View Slide

  32. Request & Response

    View Slide

  33. View Slide

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

    View Slide

  35. View Slide

  36. Request

    View Slide

  37. {
    "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}}

    View Slide

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

    View Slide

  39. Response

    View Slide

  40. {
    "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}}

    View Slide

  41. 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

    View Slide

  42. さいごに

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide