java - 在 Android 应用程序中使用 Google 登录后如何获取 Google 帐户联系人?
问题描述
我正在使用 Firebase Auth 登录我的应用程序后尝试检索 Google 联系人。最初,我没有请求范围权限以稍后在需要时访问用户联系人,我正在调用范围请求。
在获得用户的许可后,我想为此将谷歌联系人获取到我的应用程序,我需要谷歌的访问令牌。如果授予范围权限,我会在登录时得到它,如果用户允许应用程序访问联系人但它说未经授权,我不知道为什么它会突然过期。
private void getAuthToken(GoogleSignInAccount account) throws IOException, GoogleAuthException {//Get current user account google access token
Scope SCOPE_CONTACTS_READ = new Scope("https://www.googleapis.com/auth/contacts.readonly");
Scope SCOPE_EMAIL = new Scope(Scopes.EMAIL);
if (GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(getApplicationContext()),
SCOPE_CONTACTS_READ,
SCOPE_EMAIL)){
Log.i(TAG," going to get accessToken");
//Get google Access token in the background
AsyncTask.execute(new Runnable() {
@Override
public void run() {
try {
String scope = "oauth2:"+ SCOPE_EMAIL+" "+ SCOPE_CONTACTS_READ;
Log.i(TAG," accessToken in background task");
String accessToken = GoogleAuthUtil.getToken(getApplicationContext(), account.getAccount(), scope, new Bundle());
Log.d(TAG, "accessToken:"+accessToken);
if (accessToken!=null){
sharedUtilities.setGoogleAccessToken(accessToken);
}
} catch (IOException e) {
e.printStackTrace();
} catch (GoogleAuthException e) {
e.printStackTrace();
}
}
});
}
}
解决方案
推荐阅读
- sql - Postgres 查询用于检查买卖完成后可用的股票
- node.js - Mongoose:findOneAndUpdate 不适用于多种条件
- python-3.x - Python Selenium 无法在 Azure 管道中运行
- jquery - jQuery kendo grid 如何为图标编辑、更新、取消、删除添加工具提示
- vue.js - 无法使用 Vue-Router 获取 URL 中的参数
- android - 如何从 Playstore 获取自适应高分辨率图标源?
- visual-studio - Visual Studio 2019 未发布 CSHTML 文件
- python - django Queryset filter(a__b__c=d): 获取 b 用于进一步过滤
- spring-boot - java.lang.IllegalStateException:之前查找 Docker 环境的尝试失败。不会重试。请查看日志并检查配置
- javascript - 在 JavaScript 中提交表单时动态导入模块