【问题标题】:Is there any working sample/example available to use Google People API for Android?是否有任何工作示例/示例可用于使用适用于 Android 的 Google People API?
【发布时间】:2018-05-02 09:22:18
【问题描述】:

我在他们的官方文档页面上查看了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


    【解决方案1】:

    以下代码正在运行。能够访问人员 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);
        }
    }
    

    【讨论】:

    • 如何使用这个异步任务?我无法在 gradle“com.google.apis:google-api-services-people:v1-rev20180918-1.26.0”的 v26 下找到 GoogleAccountManager 类。我需要阅读用户性别、用户电话号码和用户 DoB。我无法通过 People API 阅读。请告诉我。
    • 谢谢你花了两天时间试图让这一切顺利进行,谷歌文档留下了很多想象空间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 2019-11-22
    • 1970-01-01
    • 2023-03-13
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多