【问题标题】:Fetching a user's "other" Google contacts in Android在 Android 中获取用户的“其他”Google 联系人
【发布时间】:2018-08-31 07:59:38
【问题描述】:

如果我理解正确,为了从我的 Android 应用中获取用户 Google 联系人,我应该使用 People API 而不是 Contacts API。就我而言,我想获取所有用户的联系人,包括“其他联系人”,如下图所示(点击other contacts link可以看到他/她的其他联系人):

到目前为止,我已经成功使用了 People API,如下所示。首先,我向 Google 登录选项提供所需的范围:

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);

然后我使用我的网络客户端 ID 和密码来获取用户的联系人:

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.");
            }
        }

我希望这会得到所有可用的联系人,但它只返回一小部分。所以我的问题是我是否需要通过/使用任何其他范围来读取所有联系人,包括“其他联系人”列表。

【问题讨论】:

    标签: java android google-people-api google-contacts-api


    【解决方案1】:

    People API 似乎不支持this answer 中所述的“其他联系人”联系人。您应该使用Contacts API 来获取您想要的数据。

    【讨论】:

    • 确实,根据您发布的答案,看来我必须使用联系人 API。谢谢!
    • 我正在尝试通过文档使用联系人 API,但它不起作用。请参阅stackoverflow.com/questions/52301265/…
    【解决方案2】:

    People API 现在允许按照此处所述获取其他联系人

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

    ListOtherContactsResponse response = peopleService.otherContacts().list()
        .setReadMask("metadata,names,emailAddresses")
        .execute();
    
    List<Person> otherContacts = response.getOtherContacts();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多