首页 > 解决方案 > 在 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();
                }

            }
        });

    }

}

标签: javaandroid

解决方案


推荐阅读