我将我找到的解决方案用于帮助其他人寻找它。
要做的第一件事就是在Add Google Sign-In to Your Android App处遵循Google方向。
然后您必须将GoogleSignInOptions更改为:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestProfile()
.requestEmail()
.build();
如果您需要添加其他范围,可以这样做:
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();
并且在' onActivityResult'内部' if(result.isSuccess()){'插入此:
new requestUserInfoAsync(this /* Context */, acct).execute();
并创建此方法:
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);
}
}
使用此功能,您可以使用&#39; Person meProfile&#39;获取其他信息,但您只能获取用户的公开信息,否则将为空。
修改
在上面的解决方案中,我使用了firebase。 如果您不使用它,只需更改第二行:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("YOUR APP ID")
.requestProfile()
.requestEmail()
.build();
应用程序ID代码位于Google developer console