android 服务器验证,Android auth_token验证和使用服务器端

该博客介绍了如何在Android应用中使用AccountManager获取OAuth2访问令牌,以便从Google API获取用户信息。关键步骤包括设置正确的授权令牌类型,请求用户授权,并处理获取到的访问令牌。示例代码展示了如何实现这一过程。
摘要由CSDN通过智能技术生成

您可能只是在authTokenType前面缺少oauth2:前缀.

此代码有效:

// Note the `oauth2:` prefix

private static final String AUTH_TOKEN_TYPE_USERINFO_PROFILE =

"oauth2:https://www.googleapis.com/auth/userinfo.profile";

// TODO: allow the use to choose which account to use

Account acct = accountManager.getAccountsByType("com.google")[0];

accountManager.getAuthToken(acct, AUTH_TOKEN_TYPE_USERINFO_PROFILE,

null, this, new AccountManagerCallback() {

@Override

public void run(AccountManagerFuture future) {

try {

String accessToken = future.getResult().getString(

AccountManager.KEY_AUTHTOKEN);

Log.i(TAG, "Got OAuth2 access token: " + accessToken);

/*

Your code here. Use one of two options. In each case replace ... with

the above OAuth2 access token:

1) GET https://www.googleapis.com/oauth2/v1/userinfo?access_token=...

2) GET https://www.googleapis.com/oauth2/v1/userinfo with this header:

Authorization: Bearer ...

*/

} catch (OperationCanceledException e) {

// TODO handle this case

Log.w(TAG, "The user has did not allow access");

} catch (Exception e) {

// TODO handle this exception

Log.w(TAG, "Unexpected exception", e);

}

}

}, null);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值