alexa_remote_control.sh でリフレッシュトークンを使用する

スポンサーリンク
スポンサーリンク

下記の記事で紹介した alexa_remote_control.sh でEメール・パスワード認証を使用していましたが、気づいたら動作しなくなっていました。
リフレッシュトークン使用で動作するようになりますので、設定を説明します。
なお、RaspberryPi/Linux での設定方法です。

スクリプトから Fireタブレットや FireTV Stick にしゃべらせる – パソコン鳥のブログ
Amazon Echo でMP3ファイルを再生する(Linux) – パソコン鳥のブログ
スクリプトからEchoでAlexa Skills Kitサウンドライブラリのサウンド再生 – パソコン鳥のブログ

こちらでは alexa_remote_control.sh 実行で以下のエラーが出るようになっていましたが、本記事の設定で動作するようになりました。

cookie does not exist. logging in ...
ERROR: Amazon Login was unsuccessful. Possibly you get a captcha login screen.
 Try logging in to https://alexa.amazon.co.jp with your browser. In your browser
 make sure to have all Amazon related cookies deleted and Javascript disabled!

 (For more information have a look at /tmp/.alexa.login)

 To avoid issues with captcha, try using Multi-Factor Authentication.
 To do so, first set up Two-Step Verification on your Amazon account, then
 configure this script (or the environment) with your MFA secret.
 Support for Multi-Factor Authentication requires 'oathtool' to be installed.
スポンサーリンク

リフレッシュトークンの取得

リフレッシュトークンは、クライアント専用に割り当てられる値のため、以下の設定はクライアントごとに必要です。

リフレッシュトークンを取得するために、alexa-cookie-cli を使用します。

alexa-cookie-cliを取得し、ディレクトリに展開します。

wget https://github.com/adn77/alexa-cookie-cli/archive/refs/heads/master.zip
unzip master.zip
cd alexa-cookie-cli-master/

package.json の alexa-cookie2 の箇所を次のように修正します。

修正前
"alexa-cookie2": "^4.0.1",

修正後
"alexa-cookie2": "^4.1.3",

次のコマンドを実行します。

npm install

run npm audit fix to fix them, or npm audit for details のような表示が出たら、次のコマンドを実行します。

npm audit fix

次を実行します。/tmp/.alexa.* でAlexaのセッション情報などが保存されているので削除しておきます。

rm /tmp/.alexa.*

次を実行します。
実行で、「Error: You can try to get the cookie manually by opening http://localhost:8080/ with your browser. / null」と出ても気にしないでください。

node cli.js -p amazon.co.jp -a ja-JP -P 8080

上記実行で、待ち状態になります。
上記を実行したクライアント上で、ブラウザで http://127.0.0.1:8080 にアクセスし、Amazonのアカウントでサインインしてください。
(テキストベースで動作するブラウザ lynx でもOKです。lynx で2段階認証も通せます)

ブラウザでサインインすると、上記の node cli.js~を実行していたターミナルに、Atnr| で始まるコードが表示されます。
これがリフレッシュトークンです。
後で、alexa_remote_control.sh に設定します。

node cli.js~ を実行していたターミナルで CTRL-C を押して終了させます。
ブラウザは閉じて構いません。
また、alexa-cookie-cli を展開したディレクトリは消してOKです。

alexa_remote_control.sh の設定

alexa_remote_control.sh を取得します。

wget https://raw.githubusercontent.com/thorsten-gehrig/alexa-remote-control/master/alexa_remote_control.sh
chmod +x alexa_remote_control.sh

alexa_remote_control.sh 内の SET_REFRESH_TOKEN に、先ほどの「リフレッシュトークンの取得」の手順で取得したリフレッシュトークン(Atnr| で始まるコード)を記述します。

また、その他の変数を下記のように設定します。

SET_LANGUAGE='ja-JP'
SET_TTS_LOCALE='ja-JP'
SET_AMAZON='amazon.co.jp'
SET_ALEXA='alexa.amazon.co.jp'

alexa_remote_control.sh 内には SET_EMAIL、SET_PASSWORD の変数がありますが、使用されないので設定不要です。
デフォルトのままでもいいですし、コメントアウトしてもいいです。

2023.11.13 last_alexa()、last_command()の修正を追加
2023.11.14 amazon.de を amazon.co.jp に修正
続けて、alexa_remote_control.sh 内の last_alexa()、last_command() を丸ごと次のように変更します。

last_alexa()
{
AMAZON_NEW_API='amazon.co.jp/alexa-privacy/apd/rvh'  #New System Variable für Alexa History
${CURL} ${OPTS} -s -b ${COOKIE} -A "${BROWSER}" -H "DNT: 1" -H "Connection: keep-alive" -L\
 -H "Content-Type: application/json; charset=UTF-8" -H "Referer: https://www.${AMAZON_NEW_API}" -H "Origin: https://www.${AMAZON}"\
 -H "csrf: $(awk "\$0 ~/.${AMAZON}.*csrf[ \\s\\t]+/ {print \$7}" ${COOKIE})" -X GET\
 "https://www.${AMAZON_NEW_API}/customer-history-records?startTime=$(date +%s --date "-24 hours")&endTime=$(date +%s --date "+24 hours")&recordType="VOICE_HISTORY"&maxRecordSize=1" | ${JQ} -r '[.customerHistoryRecords[] | select(.recordType == "VOICE_HISTORY" ) | select(.utteranceType == "GENERAL") | .device[]] | values[0]' 
}

last_command()
{
AMAZON_NEW_API='amazon.co.jp/alexa-privacy/apd/rvh'  #New System Variable für Alexa History
ACTIVITIES=$(${CURL} ${OPTS} -s -b ${COOKIE} -A "${BROWSER}" -H "DNT: 1" -H "Connection: keep-alive" -L\
 -H "Content-Type: application/json; charset=UTF-8" -H "Referer: https://www.${AMAZON_NEW_API}" -H "Origin: https://www.${AMAZON}"\
 -H "csrf: $(awk "\$0 ~/.${AMAZON}.*csrf[ \\s\\t]+/ {print \$7}" ${COOKIE})" -X GET\
 "https://www.${AMAZON_NEW_API}/customer-history-records?startTime=$(date +%s --date "-24 hours")&endTime=$(date +%s --date "+24 hours")&recordType="VOICE_HISTORY"&maxRecordSize=10")
 
if [ -z "$DEVICE" ] ; then
	echo "$ACTIVITIES" | ${JQ} -r '[.customerHistoryRecords[] | select(.recordType == "VOICE_HISTORY" ) | select(.voiceHistoryRecordItems[].recordItemType == "CUSTOMER_TRANSCRIPT" ) | .voiceHistoryRecordItems[].transcriptText] | values[0]'
else
	echo "$ACTIVITIES" | ${JQ} -r --arg jqdevice "$DEVICE" '[.customerHistoryRecords[] | select(.recordType == "VOICE_HISTORY" ) | select(.device[] == $jqdevice) | select(.voiceHistoryRecordItems[].recordItemType == "CUSTOMER_TRANSCRIPT" ) | .voiceHistoryRecordItems[].transcriptText] | values[0]'
fi
}

これで、alexa_remote_control.sh の設定は完了です。

動作確認

alexa_remote_control.sh -a を実行すると、下記のようにAlexa端末の一覧が表示されます。

$ ./alexa_remote_control.sh -a
the following devices exist in your account:
子供部屋
リビング
台所
FireHD8
FireHD7
Fire TV stick
This Device

動作しない場合は、リフレッシュトークンを alexa_remote_control.sh 内の SET_REFRESH_TOKEN に間違いなく設定したか確認してください。
また、リフレッシュトークンは、クライアント専用の値のため、別のクライアントでの値を設定していないか確認してください。

さて、これで alexa_remote_control.sh が動作するようになりましたが、機能しなくなったものもありました。
本ブログ中で紹介した次の記事の機能は、エラーが出てダメでした。

2023.11.13
先の手順「alexa_remote_control.sh の設定」のalexa_remote_control.sh 内の last_alexa()、last_command() の修正で、次の記事の内容は動作するようになります。

最後にAlexa端末に話しかけた内容を確認する – パソコン鳥のブログ
複数のAlexa端末のどれから呼びかけたのか判定する(RaspberryPi,Ubuntu) – パソコン鳥のブログ

下の記事の機能は、動作OKでした。

スクリプトから Fireタブレットや FireTV Stick にしゃべらせる – パソコン鳥のブログ
Amazon Echo でMP3ファイルを再生する(Linux) – パソコン鳥のブログ
スクリプトからEchoでAlexa Skills Kitサウンドライブラリのサウンド再生 – パソコン鳥のブログ

参考

アレクサでインターホンの通知をする #Python – Qiita

コメント

タイトルとURLをコピーしました