如何让自己的Apk 能够登录亚马逊账户

xiaoxiao2021-02-28  20

1)安装Android studio2)下载Alexa SDK3)注册你的APK4)将alexa sdk 放到文件apk中5)添加网络权限6)将API KEY添加到项目中7)添加防止活动重启,导致登录界面消失8)将工作流程活动添加到项目中9)设计页面布局10)登录时常用几个API 接口介绍11)、常见的 api key 出现的问题12、参考示例

原文: Login with Amazon for Android https://developer.amazon.com/docs/login-with-amazon/android-docs.html

1)安装Android studio

安装好Android Studio 并且配备好SDK , AVD,这里不再叙述。

2)下载Alexa SDK

(1)下载Alexa SDk 点击跳转下载

3)注册你的APK

(1)打开Android Studio 新建一个项目 (2)访问亚马逊门户网,注册你的安全配置文件 网址链接容

这一步一定要切记,添加的 Security Profiles 一定要从 Login with Amazon 那个选项进入,不然app会出现找不到识别范围的错误。

(3) 点击 “Create a New Security Profile“,将带你去安全配置页面 (4)填写相应内容,保存 (5)保存之后,出现相关配置信息 (6)当配置完安全配置文件之后,我们将会依照特定的网站或者APK,与这个配置文件联系起来。 (7)将已注册的AppStore或开发者控制台应用程序添加到安全配置文件中 (8)进入这个网页 点击链接内容

跳转

(9)选择你用的那个 —>Manage —–> Kindle/Android Settings

在这里填写相应信息

对于图中MD5 和SHA256获取的方法。

android studio Apk 签名,获取 MD5 、 SHA1、 SHA256 的值参照

(10)找到生成的 .jks文件 运行:

keytool -list -v -alias <alias> -keystore <keystore.filename>

keytool 是java 安装路径 bin 文件下 alias 是你签名时自己填写的 keystore.filename 是你保存的签名文件路径

例如我的:

这样我们会得到一个api key.

(11)进入到这个界面 (12)点击show 可以看到生成的api key 文件

4)将alexa sdk 放到文件apk中

(1)将下载好的sdk 里面的 login-with-amazon-sdk.jar 复制到 project/app ctrl + c ,ctrl + v 将其放到libs 文件夹下面,没有就新建一个。 (2)右击 login-with-amazon-sdk.jar 选择 Add As Library.

5)添加网络权限

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

6)将API KEY添加到项目中

在apk 项目中 新建一个assets 文件夹—>新建 api_key.txt 文件,并将生成的api key 复制保存到这个文件中。

7)添加防止活动重启,导致登录界面消失

在AndroidManifest.xml 文件里的登录界面添加

android:configChanges=”keyboard|keyboardHidden|orientation|screenSize”

例如:

<activity android:name=".SampleLoginWithAmazonActivity" //添加 android:configChanges="keyboard|keyboardHidden|orientation" android:label="@string/app_name"> <!-- Prevents authorization dialog from closing when screen orientation is changed --> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>

8)将工作流程活动添加到项目中

当用户单击Amazon按钮登录时,API将启动Web浏览器,向用户呈现登录和同意页面。为了使浏览器活动正常工作,必须将工作流程活动添加到清单中。

如果已经集成亚马逊SDK或你有com.amazon.identity.auth.device.authorization.authorizationactivity活动在你的xml中声明,它必须被移除和替换的workflowactivity。

在 AndroidManifest.xml 添加如下代码

<activity android:name="com.amazon.identity.auth.device.workflow.WorkflowActivity" android:theme="@android:style/Theme.NoDisplay" android:allowTaskReparenting="true" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <!-- android:host must use the full package name found in Manifest General Attributes --> <data android:host="${applicationId}" android:scheme="amzn"/> </intent-filter> </activity>

9)设计页面布局

详细代码请看apk 源码

10)登录时常用几个API 接口介绍

(1)onCreat 中 首先初始化一个 RequestContext 。 首先需要声明一个请求上下文变量,并创建一个新的类实例。初始化RequestContext的最佳位置是在Android活动或片段的OnCube方法中。例如:

private RequestContext requestContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestContext = RequestContext.create(this); }

(2)onCreat 中 创建一个authorizelistener。 在oncreat 方法中,添加注册监听授权状态的监听接口,其中有onSuccess, onError,onCancel方法。 如果是在一个fragment 中实现,就在onCreatView中添加。

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestContext = RequestContext.create(this); //注册监听授权状态 requestContext.registerListener(new AuthorizeListener() { /* Authorization was completed successfully. */ //授权成功 @Override public void onSuccess(AuthorizeResult result) { /* Your app is now authorized for the requested scopes */ } /* There was an error during the attempt to authorize the application. */ //授权失败 @Override public void onError(AuthError ae) { /* Inform the user of the error */ } /* Authorization was cancelled before it could be completed. */ //取消授权 @Override public void onCancel(AuthCancellation cancellation) { /* Reset the UI to a ready-to-login state */ } }); }

(3)onResume 中 添加回调 RequestContext.onResume 为了适应Android应用程序的生命周期,实现活动或片段的onResume方法。这将触发所有监听的事件,你的应用程序退出的操作系统之前,用户完成注册流程registerlistener授权。

@Override protected void onResume() { super.onResume(); requestContext.onResume(); }

(4)onStart 中 添加获取访问令牌的判断 判断用户访问令牌是否获取成功

@Override protected void onStart(){ super.onStart(); Scope[] scopes = { ProfileScope.profile(), ProfileScope.postalCode() }; AuthorizationManager.getToken(this, scopes, new Listener<AuthorizeResult, AuthError>() { @Override public void onSuccess(AuthorizeResult result) { if (result.getAccessToken() != null) { /* The user is signed in */ } else { /* The user is not signed in */ } } @Override public void onError(AuthError ae) { /* The user is not signed in */ } }); }

(5)回调 AuthorizationManager.authorize

点击登录按钮,切换系统的web ,显示亚马逊登录界面。填写用户名 密码等

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* Previous onCreate declarations omitted */ // Find the button with the login_with_amazon ID // and set up a click handler View loginButton = findViewById(R.id.login_with_amazon); loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AuthorizationManager.authorize(new AuthorizeRequest .Builder(requestContext) .addScopes(ProfileScope.profile(), ProfileScope.postalCode()) .build()); }); }

(6)读取用户配置文件数据 apk 被授权之后,使用API检索用户配置文件。这个就是登录成功之后,获取的亚马逊用户名 、 邮箱等。

private void fetchUserProfile() { User.fetch(this, new Listener<User,AuthError>() { /* fetch completed successfully. */ @Override public void onSuccess(User user) { final String name = user.getUserName(); final String email = user.getUserEmail(); final String account = user.getUserId(); final String zipcode = user.getUserPostalCode(); runOnUiThread(new Runnable() { @Override public void run() { updateProfileData(name, email, account, zipcode); } }); } /* There was an error during the attempt to get the profile. */ @Override public void onError(AuthError ae) { /* Retry or inform the user of the error */ } }); }

(7)清除授权数据并注销用户 如何使用signout方法清除从authorizationmanager本地数据存储用户的授权数据。用户将不得不再次登录,以便应用程序检索配置文件数据。使用此方法注销用户,或排除应用程序中的登录问题。

当用户成功登录时,应提供注销机制,以便它们可以清除其配置文件数据和先前授权的范围。您的机制可能是超链接、按钮或菜单项。对于这个例子,我们将为按钮创建一个OnCutton方法。

当signout成功,更新UI,并提供重新登录机制,用户可以使用登录一次。如果signout返回一个错误,也可以让用户尝试登录了。

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* Previous onCreate declarations omitted */ // Find the button with the logout ID and set up a click handler View logoutButton = findViewById(R.id.logout); logoutButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AuthorizationManager.signOut(getApplicationContext(), new Listener<Void, AuthError>() { @Override public void onSuccess(Void response) { // Set logged out state in UI } @Override public void onError(AuthError authError) { // Log the error } }); } }); }

11)、常见的 api key 出现的问题

1)api key 是无效的 查看自己的api key 是否获取到正确MD5 和 SHA 256 , 有可能是我们在亚马逊网上填写的这两个值是错的 (1)我们采用 断点调试 的方法,来观察我们获得的api_key.txt 文件 和 自身APK 的 MD5 和 SHA 256的值 (2)找到导入到包里面的 APIKeyDecoder.class 这个类

这两个方法就是用来比对api_key.txt 文件 和 apk 自身的 MD5 SHA 256 的信息是否一样。

(3)断点调试的log

这里我们可以看到 上面试api_key.text 文件里的,下面出来的数据是apk 本身的,我这里两个数据是一样的。 要是两个数据不一样,那你就把,下面的你的真正的apk 的MD5 SHA256的值 ,重新填写到 亚马逊网站注册,一下。

2)有一种情况是 api key 是有效的,但是会出现 api key 范围的问题导致登陆不 这种情况,就是你在注册api_key 的时候没有将 你的Security Profile 放到 Login with Amazon 那一项里面,添加进去就可以了。

12、参考示例

跳转页面输入亚马逊账号密码:

登陆成功显示:

demo下载

文章参考: 亚马逊语言识别Alexa之AlexaAndroid的接入及AlexaAndroid的源码解析(一) https://blog.csdn.net/wangyongyao1989/article/details/80183020

实例代码下载:登录亚马逊账户apk

转载请注明原文地址: https://www.6miu.com/read-2603273.html

最新回复(0)