You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter应用中接入Login with Amazon并读写Alexa Skill?

Flutter集成Login with Amazon及Alexa Skill读写操作

一、接入Login with Amazon

1. 开发者控制台配置

  • 登录Amazon开发者控制台,创建移动应用,获取Client ID和Client Secret。
  • 配置应用的包名(Android)/Bundle ID(iOS),并设置重定向URL(比如myapp://login)——这个URL要和后续代码里的一致。

2. 添加Flutter依赖

在pubspec.yaml里加入适配的登录包:

dependencies:
  login_with_amazon: ^2.0.0
  shared_preferences: ^2.2.2 # 可选,用来持久化存储令牌

执行flutter pub get安装依赖。

3. 平台端配置

Android

在AndroidManifest.xml中添加权限和跳转活动:

<uses-permission android:name="android.permission.INTERNET" />

<activity
    android:name="com.amazon.identity.auth.device.authorization.AuthorizationActivity"
    android:theme="@android:style/Theme.NoDisplay">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="myapp" android:host="login" />
    </intent-filter>
</activity>

iOS

在Info.plist中配置URL Scheme和允许的查询Scheme:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>myapp</string>
        </array>
    </dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>amzn</string>
</array>

4. 实现登录逻辑

import 'package:login_with_amazon/login_with_amazon.dart';
import 'package:shared_preferences/shared_preferences.dart';

final AmazonLogin _amazonLogin = AmazonLogin();

// 发起登录请求
Future<void> amazonLogin() async {
  final result = await _amazonLogin.login(
    scopes: ['profile', 'alexa::ask:skills:readwrite'], // 必须包含Alexa权限范围
    redirectUrl: 'myapp://login',
    clientId: '你的Client ID',
  );

  if (result.status == AuthorizationStatus.authorized) {
    // 将访问令牌存入本地
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('amazon_access_token', result.accessToken!);
    // 登录成功后的业务逻辑,比如跳转主页
  } else {
    // 登录失败,提示用户
  }
}

// 登出操作
Future<void> amazonLogout() async {
  await _amazonLogin.logout();
  final prefs = await SharedPreferences.getInstance();
  await prefs.remove('amazon_access_token');
}

二、Alexa Skill读写操作

1. 确认权限配置

登录时必须请求alexa::ask:skills:readwrite权限范围,否则拿到的令牌无法访问Alexa Skill API。如果之前没加这个范围,需要引导用户重新登录获取新令牌。

2. 调用Alexa Skill API

先添加http包依赖:

dependencies:
  http: ^1.1.0

读取Skill交互模型

import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';

Future<Map<String, dynamic>?> getSkillModel(String skillId) async {
  final prefs = await SharedPreferences.getInstance();
  final token = prefs.getString('amazon_access_token');
  if (token == null) return null;

  final url = Uri.parse('https://api.amazonalexa.com/v1/skills/$skillId/stages/development/interactionModel/locales/en-US');
  final response = await http.get(
    url,
    headers: {
      'Authorization': 'Bearer $token',
      'Content-Type': 'application/json',
    },
  );

  if (response.statusCode == 200) {
    return jsonDecode(response.body);
  } else {
    print('获取模型失败:${response.statusCode}');
    return null;
  }
}

更新Skill交互模型

import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';

Future<bool> updateSkillModel(String skillId, Map<String, dynamic> modelData) async {
  final prefs = await SharedPreferences.getInstance();
  final token = prefs.getString('amazon_access_token');
  if (token == null) return false;

  final url = Uri.parse('https://api.amazonalexa.com/v1/skills/$skillId/stages/development/interactionModel/locales/en-US');
  final response = await http.put(
    url,
    headers: {
      'Authorization': 'Bearer $token',
      'Content-Type': 'application/json',
    },
    body: jsonEncode(modelData),
  );

  return response.statusCode == 204; // 204状态码表示更新成功
}

3. 常见问题处理

  • 令牌过期:如果API返回401错误,需引导用户重新登录获取新令牌。
  • 权限不足:检查登录时的权限范围是否正确,以及Amazon开发者控制台中应用的权限配置。
  • 环境切换:根据Skill的部署阶段(development/live)调整URL中的stages参数。

内容的提问来源于stack exchange,提问作者Krishna prasad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 15:55:14