Last active
November 14, 2018 06:48
-
-
Save ameerhamza6733/403396fc5ff0a514cfc65e386295cc14 to your computer and use it in GitHub Desktop.
Retrieving information about a contact with Google People API (java) android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// call this mathod from your onCreate | |
private void GoogleLogin() { | |
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | |
.requestScopes(new Scope(PeopleScopes.USERINFO_PROFILE)) | |
.requestIdToken(getString(R.string.default_web_client_id)) | |
.requestEmail() | |
// The serverClientId is an OAuth 2.0 web client ID, u can get this from google api console like any other api key | |
.requestServerAuthCode(getString(R.string.clientID), false) | |
.requestProfile() | |
.build(); | |
mGoogleSignInClient = GoogleSignIn.getClient(this, gso); | |
Intent signInIntent = mGoogleSignInClient.getSignInIntent(); | |
startActivityForResult(signInIntent, RC_SIGN_IN); | |
} | |
//then override this in your activity | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); | |
if (requestCode == RC_SIGN_IN) { | |
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); | |
try { | |
// Google Sign In was successful, authenticate with Firebase | |
GoogleSignInAccount account = task.getResult(ApiException.class); | |
if (account != null) | |
PeopleApiCall(account.getServerAuthCode());// call people api to get more user info e.g user gender, data of birth | |
} catch (ApiException e) { | |
// Google Sign In failed, update UI appropriately | |
if (progressDialog != null) | |
progressDialog.dismiss(); | |
Log.w(TAG, "Google sign in failed", e); | |
Toast.makeText(LoginActivity.this, "onGoogle Singin Error" + e.getMessage(), Toast.LENGTH_LONG).show(); | |
} | |
} | |
} | |
private void PeopleApiCall(String serverAuthCode) { | |
Observable.fromCallable(() -> { | |
try { | |
People peopleService = PeopleHelper.setUp(LoginActivity.this, serverAuthCode); | |
return peopleService.people().get("people/me").setRequestMaskIncludeField("person.photos,person.names,person.genders,person.birthdays").execute();//if you want get more user filds read this link https://stackoverflow.com/a/35623288/5727285 | |
} catch (Exception e) { | |
return null; | |
} | |
}) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe((person) -> { | |
if (progressDialog != null) | |
progressDialog.dismiss(); | |
if (person != null) { | |
if (person.getGenders() != null && person.getGenders().size() > 0) { | |
updateUI(person.getGenders().get(0).getValue());//update user info in firebase auth | |
} | |
if (person.getPhotos() != null && person.getPhotos().size() > 0) { | |
Log.d(TAG, "user cover photo: " + person.getPhotos().get(0).getUrl()); | |
} | |
if (person.getBirthdays() != null && person.getBirthdays().size() > 0) { | |
Log.d(TAG, "data of birhts" + person.getBirthdays().get(0).getText()); | |
Log.d(TAG, "data of birhts" + person.getBirthdays().get(0).getDate().getYear()); | |
Log.d(TAG, "data of birhts" + person.getBirthdays().get(0).toString()); | |
} | |
// Log.d(TAG, String.format("googleOnComplete: gender: %s, birthday: %s, about: %s, cover: %s", profileGender, profileBirthday, profileAbout, profileCover)); | |
} | |
}); | |
} |
People Helper Class
import android.content.Context;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.people.v1.People;
import java.io.IOException;
/**
* Created by Suleiman19 on 4/4/16.
*/
public class PeopleHelper {
private static final String APPLICATION_NAME = "Peoples App";
public static People setUp(Context context, String serverAuthCode) throws IOException {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
// Redirect URL for web based applications.
// Can be empty too.
String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
// Exchange auth code for access token
GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
httpTransport,
jsonFactory,
context.getString(R.string.clientID),
context.getString(R.string.clientSecret),
serverAuthCode,
redirectUrl).execute();
// Then, create a GoogleCredential object using the tokens from GoogleTokenResponse
GoogleCredential credential = new GoogleCredential.Builder()
.setClientSecrets(context.getString(R.string.clientID), context.getString(R.string.clientSecret))
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.build();
credential.setFromTokenResponse(tokenResponse);
// credential can then be used to access Google services
return new People.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PeopleHelper ?