Files
windmill_workflow/alexa-api/auth3.js
2026-04-04 09:15:09 +09:00

77 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* auth3.js - メール/パスワードで直接認証2FA なしのアカウント向け)
*
* 使い方:
* AMAZON_EMAIL="xxx@xxx.com" AMAZON_PASSWORD="yourpass" node auth3.js
*
* 成功すると .env を更新して終了します。
*/
const AlexaRemote = require('alexa-remote2');
const fs = require('fs');
const path = require('path');
const email = process.env.AMAZON_EMAIL;
const password = process.env.AMAZON_PASSWORD;
if (!email || !password) {
console.error('[ERROR] 環境変数 AMAZON_EMAIL と AMAZON_PASSWORD を設定してください');
console.error(' 例: AMAZON_EMAIL="xxx@xxx.com" AMAZON_PASSWORD="pass" node auth3.js');
process.exit(1);
}
console.log(`[INFO] ${email} でログイン試行中...`);
const alexa = new AlexaRemote();
alexa.init(
{
email,
password,
alexaServiceHost: 'alexa.amazon.co.jp',
amazonPage: 'amazon.co.jp',
acceptLanguage: 'ja-JP',
useWsMqtt: false,
setupProxy: false,
logger: (msg) => {
if (!msg.includes('verbose') && !msg.includes('Bearer')) {
console.log('[alexa]', msg);
}
},
onSucess: (refreshedCookie) => {
saveCookie(refreshedCookie, 'onSucess refresh');
},
},
(err) => {
if (err) {
console.error('[ERROR] 認証失敗(詳細):', err);
console.error('\n考えられる原因:');
console.error(' - パスワードが違う');
console.error(' - Amazon が CAPTCHA を要求している(後で再試行)');
console.error(' - 2FA が実際は有効になっている');
process.exit(1);
}
// 認証成功
const cookie = alexa.cookie;
saveCookie(cookie, 'init success');
process.exit(0);
}
);
function saveCookie(cookie, source) {
if (!cookie) {
console.error(`[${source}] Cookie が空です`);
return;
}
const cookieStr = typeof cookie === 'string' ? cookie : JSON.stringify(cookie);
const envPath = path.join(__dirname, '.env');
fs.writeFileSync(envPath, 'ALEXA_COOKIE=' + cookieStr + '\n');
console.log('\n==============================================');
console.log(' 認証成功!');
console.log('==============================================');
console.log('.env を更新しました:', envPath);
console.log('Cookie length:', cookieStr.length);
}