Alexa授权

按照下面的步骤进行Android应用授权

  1. 到Amazon获取一个apiKey文件
  2. 你需要声明一个RequestContext的成员变量和创建一个新的类实例。若要创建实例,请将当前应用程序上下文传递到静态工厂方法。初始化最好的地方是在你的Activity的onCreate方法。例如:
private RequestContext mRequestContext;

@Override
protected void onCreate(Bundle savedInstance) {
  super.onCreate(savedInstance);
  mRequestContext = RequestContext.create(this);
}

3.创建一个authorizelistenerimpl类
AuthorizeListenerImpl继承AuthorizeListener这个抽象类, 实现它的3个抽象方法。授权成功、失败、取消时会调用对应方法。如下:



private class AuthorizeListenerImpl extends AuthorizeListener {

  /* Authorization was completed successfully. */
  @Override
  public void onSuccess(final AuthorizeResult authorizeResult) {
  }

  /* There was an error during the attempt to authorize the application. */
  @Override
  public void onError(final AuthError authError) {
  }

  /* Authorization was cancelled before it could be completed. */
  @Override
  public void onCancel(final AuthCancellation authCancellation) {
  }
}

4.创建一个authorizelistenerimpl实例并注册它

@Override
protected void onCreate(Bundle savedInstance) {
  super.onCreate(savedInstance);
  mRequestContext = RequestContext.create(this);
  mRequestContext.registerListener(new AuthorizeListenerImpl());
}

5.重写resume方法



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

6.登录Amazon账号代码如下:

 final JSONObject scopeData = new JSONObject();
          final JSONObject productInstanceAttributes = new JSONObject();

          try {
              productInstanceAttributes.put("deviceSerialNumber", PRODUCT_DSN);
              scopeData.put("productInstanceAttributes", productInstanceAttributes);
              scopeData.put("productID", PRODUCT_ID);

              AuthorizationManager.authorize(new AuthorizeRequest.Builder(mRequestContext)
                  .addScope(ScopeFactory.scopeNamed("alexa:all", scopeData))
                  .forGrantType(AuthorizeRequest.GrantType.ACCESS_TOKEN)
                      .shouldReturnUserData(false)
                  .build());
          } catch (JSONException e) {
              // handle exception here
          }

7.创建token获取监听者



public class TokenListener implements Listener<AuthorizeResult, AuthError> {
  /* getToken completed successfully. */
  @Override
  public void onSuccess(AuthorizeResult authorizeResult) {
  }

  /* There was an error during the attempt to get the token. */
  @Override
  public void onError(AuthError authError) {
  }
}

8.登录成功时获取token

AuthorizationManager.getToken(this, new Scope[] { ScopeFactory.scopeNamed("alexa:all") }, new TokenListener());

9.在tokenListener的onSuccess事件中获取token

/* getToken completed successfully. */
@Override
public void onSuccess(AuthorizeResult authorizeResult) {
  String accessToken = authorizeResult.getAccessToken();
}

10.在activity的onstart事件调用AuthorizationManager.getToken来判断是否已经授权



private static final Scope ALEXA_ALL_SCOPE = ScopeFactory.scopeNamed("alexa:all");

@Override
protected void onStart(){
  super.onStart();
  AuthorizationManager.getToken(this, new Scope[] { ALEXA_ALL_SCOPE }, new TokenListener());
}

得到token就可以使用它去访问AVS

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值