首页 > 解决方案 > 是否有任何工作示例/示例可用于使用适用于 Android 的 Google People API?

问题描述

我查看了google people API他们的官方文档页面

https://developers.google.com/people/quickstart/java#step_4_run_the_sample

尽管遵循了所有必要的步骤API都不起作用。

我在 GitHub 上遇到了以下示例,该示例效果不佳。https://github.com/Suleiman19/People-API-App

另外,我检查了谷歌示例

Credential credential = new AuthorizationCodeInstalledApp(
                flow, new LocalServerReceiver()).authorize("user");

lang.NoClassDefFoundError: Failed resolution of: Ljava/awt/Desktop;

标签: google-people-api

解决方案


下面的代码正在工作。能够访问人员 API

 private static class GetUserInfoTask extends AsyncTask<String, Void, Void> {
    private final Context context;
    private ISignIn iSignIn;
    private HttpTransport httpTransport = new NetHttpTransport();
    private JacksonFactory jsonFactory = new JacksonFactory();
    private String personEmail;
    private String personLastName;
    private String personFirstName;
    private String image;
    private String genderString;
    private String aboutMeLocal;
    private String phoneLocal;
    private long birthdayLocal;

    public GetUserInfoTask(Context context, ISignIn iSignIn) {
        this.context = context;
        this.iSignIn = iSignIn;
    }

    @Override
    protected Void doInBackground(String... params) {
        personEmail = params[0];
        personLastName = params[1];
        personFirstName = params[2];
        image = params[3];


        Person userProfile = null;
        Collection<String> scopes = new ArrayList<>();
        scopes.add(Scopes.PROFILE);
        GoogleAccountCredential mCredential =
                GoogleAccountCredential.usingOAuth2(context, scopes);
        //mCredential.setSelectedAccount(new Account(personEmail, context.getString(R.string.account_type)));
        mCredential.setSelectedAccountName(personEmail);

        People service = new People.Builder(httpTransport, jsonFactory, mCredential)
                .setApplicationName(context.getString(R.string.app_name)) // your app name
                .build();

        // Get info. on user
        try {
            userProfile = service.people().get("people/me").setRequestMaskIncludeField("person.biographies,person.birthdays,person.genders,person.phone_numbers").execute();
        } catch (IOException e) {
            FirebaseApp.initializeApp(context);
            FirebaseCrash.report(e);
            LogUtils.e(TAG, e.getMessage());
        }

        // Get whatever you want
        if (userProfile != null) {

            // Gender
            List<Gender> genders = userProfile.getGenders();
            if (genders != null && genders.size() > 0) {
                Gender gender = genders.get(0);
                if (gender != null) {
                    // save mGender
                    genderString = gender.getValue();
                    LogUtils.d(TAG, "mGender : " + gender.getValue());
                }
            }

            // BirthDay
            List<Birthday> birthdays = userProfile.getBirthdays();
            if (birthdays != null && birthdays.size() > 0) {
                Birthday birthday = birthdays.get(0);
                if (birthday != null && birthday.getDate() != null && birthday.getDate().getYear() != null && birthday.getDate().getMonth() != null
                        && birthday.getDate().getDay() != null) {
                    // save mBirthday
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(birthday.getDate().getYear(), birthday.getDate().getMonth(), birthday.getDate().getDay());
                    birthdayLocal = calendar.getTime().getTime();
                    LogUtils.d(TAG, "mBirthday : " + birthday.toString());
                }
            }

            // Phone Number
            List<PhoneNumber> phoneNumbers = userProfile.getPhoneNumbers();
            if (phoneNumbers != null && phoneNumbers.size() > 0) {
                PhoneNumber phoneNumber = phoneNumbers.get(0);
                if (phoneNumber != null) {
                    // save mPhoneNumber
                    phoneLocal = phoneNumber.getValue();
                    LogUtils.d(TAG, "mPhoneNumber : " + phoneNumber.getValue());
                }
            }

            // biography (About me)
            List<Biography> biographies = userProfile.getBiographies();
            if (biographies != null && biographies.size() > 0) {
                Biography biography = biographies.get(0);
                if (biography != null) {
                    // save biography
                    aboutMeLocal = biography.getValue();
                    LogUtils.d(TAG, "biography : " + biography.getValue());
                }
            }

        }
        try{
        ListConnectionsResponse response = service.people().connections()
                .list("people/me")
                // This line's really important! Here's why:
                // http://stackoverflow.com/questions/35604406/retrieving-information-about-a-contact-with-google-people-api-java
                .setRequestMaskIncludeField("person.names,person.emailAddresses,person.phoneNumbers,person.biographies")
                .execute();
        List<Person> connections = response.getConnections();

        if(connections!=null && connections.size()>0){
        for (Person person : connections) {
            getPersonInfo(person);
        }}

    } catch (IOException e) {
        e.printStackTrace();
    }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        iSignIn.onSaveUserInformation(personEmail, personFirstName, personLastName, image, genderString, birthdayLocal, phoneLocal, aboutMeLocal);
    }
}

推荐阅读