简体   繁体   中英

Fetching a user's "other" Google contacts in Android

If I understand correctly, in order to fetch the user Google contacts from my Android app, I should use the People API instead of the Contacts API . In my case, I want to get all user's contacts including the "other contacts", as shown in the figure below (one can see his/her other contacts by clicking on the other contacts link ):

在此处输入图片说明

Up to now, I have successfully used the People API as shown below. First I provide the required scopes to the Google SignIn Options:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestIdToken(getString(R.string.default_web_client_id))
                    .requestServerAuthCode(getString(R.string.default_web_client_id))
                    .requestEmail()
                    .requestProfile()
                    .requestScopes(new Scope(PeopleServiceScopes.CONTACTS_READONLY))
                    .build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

Then I use my webclient Id and secret to fetch the user's contacts:

public void getUserContacts () throws IOException {
            HttpTransport httpTransport = new NetHttpTransport();
            JacksonFactory jsonFactory = new JacksonFactory();

            // Go to the Google API Console, open your application's
            // credentials page, and copy the client ID and client secret.
            // Then paste them into the following code.
            String clientId = getString(R.string.webClientIDAutoCreated);
            String clientSecret = getString(R.string.webClientIDSecretAutoCreated);

            // Or your redirect URL for web based applications.
            String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";               
            String scope = "https://www.googleapis.com/auth/contacts.readonly";
            String serverAuthCode = userSettings.getString(USER_PREFS_SERVER_AUTH_CODE,"");

            // Step 1: Authorize -->
            String authorizationUrl = new GoogleBrowserClientRequestUrl(clientId, redirectUrl, Arrays.asList(scope)).build();

            // Point or redirect your user to the authorizationUrl.
            System.out.println("Go to the following link in your browser:");
            System.out.println(authorizationUrl);

            // Read the authorization code from the standard input stream.
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("What is the authorization code?");
            String code = in.readLine();
            // End of Step 1 <--

            // Step 2: Exchange -->
            GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(httpTransport, jsonFactory, clientId, clientSecret, serverAuthCode, redirectUrl).execute();
            // End of Step 2 <--

            GoogleCredential credential = new GoogleCredential.Builder()
                    .setTransport(httpTransport)
                    .setJsonFactory(jsonFactory)
                    .setClientSecrets(clientId, clientSecret)
                    .build()
                    .setFromTokenResponse(tokenResponse);

            PeopleService peopleService = new PeopleService.Builder(httpTransport, jsonFactory, credential)
                    .setApplicationName(getString(R.string.app_name))
                    .build();

            ListConnectionsResponse response = peopleService.people().connections()
                    .list("people/me")
                    .setPersonFields("names,emailAddresses")
                    .execute();

            // Print display name of connections if available.
            List<Person> connections = response.getConnections();
            if (connections != null && connections.size() > 0) {
                for (Person person : connections) {
                    List<Name> names = person.getNames();
                    if (names != null && names.size() > 0) {
                        myLog(TAG,DEBUG_OK,"Name: " + person.getNames().get(0).getDisplayName());
                        List<EmailAddress> emailAddresses = person.getEmailAddresses();
                        if (emailAddresses != null && emailAddresses.size() > 0) {
                            for (EmailAddress email: emailAddresses)
                                myLog(TAG,DEBUG_OK,"email: " + email.getValue());
                        }
                    }
                    else {
                        myLog(TAG,DEBUG_OK,"No names available for connection.");
                    }
                }
            }
            else {
                System.out.println("No connections found.");
            }
        }

I was hoping that this would get all available contacts, however it returns only a small subset. So my question is whether I need to pass / use any other scopes to read all contacts, including the "other contacts" list.

The People API doesn't appear to support the "Other Contacts" contacts as described in this answer . You should use the Contacts API to get the data you want.

People API allows to fetch other Contacts now as described here

https://developers.google.com/people/v1/other-contacts

ListOtherContactsResponse response = peopleService.otherContacts().list()
    .setReadMask("metadata,names,emailAddresses")
    .execute();

List<Person> otherContacts = response.getOtherContacts();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM