Das erste, was zu tun ist, folgen Sie der Google-Orientierung bei Add Google Sign-In to Your Android App.
Dann müssen Sie die GoogleSignInOptions ändern:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestProfile()
.requestEmail()
.build();
Wenn Sie eine andere Bereiche hinzufügen, müssen Sie es wie folgt tun:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
.requestProfile()
.requestEmail()
.build();
Und 'onActivityResult' innen ‚if (result.isSuccess()) {‘einfügen this:
new requestUserInfoAsync(this /* Context */, acct).execute();
und dieses Verfahren erzeugen:
private static class requestUserInfoAsync extends AsyncTask<Void, Void, Void> {
// Global instance of the HTTP transport.
private static HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
// Global instance of the JSON factory.
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private Context context;
private GoogleSignInAccount acct;
private String birthdayText;
private String addressText;
private String cover;
public requestUserInfoAsync(Context context, GoogleSignInAccount acct) {
this.context = context;
this.acct = acct;
}
@Override
protected Void doInBackground(Void... params) {
// On worker thread
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
context, Collections.singleton(Scopes.PROFILE)
);
credential.setSelectedAccount(new Account(acct.getEmail(), "com.google"));
People service = new People.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(context.getString(R.string.app_name) /* whatever you like */)
.build();
// All the person details
try {
Person meProfile = service.people().get("people/me").execute();
List<Birthday> birthdays = meProfile.getBirthdays();
if (birthdays != null && birthdays.size() > 0) {
Birthday birthday = birthdays.get(0);
// DateFormat.getDateInstance(DateFormat.FULL).format(birthdayDate)
birthdayText = "";
try {
if (birthday.getDate().getYear() != null) {
birthdayText += birthday.getDate().getYear() + " ";
}
birthdayText += birthday.getDate().getMonth() + " " + birthday.getDate().getDay();
} catch (Exception e) {
e.printStackTrace();
}
}
List<Address> addresses = meProfile.getAddresses();
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
addressText = address.getFormattedValue();
}
List<CoverPhoto> coverPhotos = meProfile.getCoverPhotos();
if (coverPhotos != null && coverPhotos.size() > 0) {
CoverPhoto coverPhoto = coverPhotos.get(0);
cover = coverPhoto.getUrl();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Log.i("TagTag", "birthday: " + birthdayText);
Log.i("TagTag", "address: " + addressText);
Log.i("TagTag", "cover: " + cover);
}
}
Mit diesem Sie die Methoden innerhalb ‚Person meProfile‘ können andere Informationen zu bekommen, aber Sie können das bekommen nur die öffentlichen Informationen des Benutzers sonst wird es null sein.
Haben Sie versucht, [GoogleSignInResult] (https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInResult) zu verwenden? – gerardnimo
Das einzige Objekt, das Daten über den Benutzer in GoogleSignInResult enthält, ist GoogleSignInAccount, das nicht die Daten enthält, die ich brauche. –
Ich fand dieses alte [thread] (http://stackoverflow.com/questions/2108537/which-google-api-to-use-for-getting-users-first-name-last-name-bild-etc). Ich hoffe, das wird helfen. – gerardnimo