Fetching a user's "other" Google contacts in Android - java

If I understand correctly, in order to fetch the user Google contacts from my Android app, I should use the People API instead of the Contacts API. In my case, I want to get all user's contacts including the "other contacts", as shown in the figure below (one can see his/her other contacts by clicking on the other contacts link):
Up to now, I have successfully used the People API as shown below. First I provide the required scopes to the Google SignIn Options:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestServerAuthCode(getString(R.string.default_web_client_id))
.requestEmail()
.requestProfile()
.requestScopes(new Scope(PeopleServiceScopes.CONTACTS_READONLY))
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
Then I use my webclient Id and secret to fetch the user's contacts:
public void getUserContacts () throws IOException {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
// Go to the Google API Console, open your application's
// credentials page, and copy the client ID and client secret.
// Then paste them into the following code.
String clientId = getString(R.string.webClientIDAutoCreated);
String clientSecret = getString(R.string.webClientIDSecretAutoCreated);
// Or your redirect URL for web based applications.
String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
String scope = "https://www.googleapis.com/auth/contacts.readonly";
String serverAuthCode = userSettings.getString(USER_PREFS_SERVER_AUTH_CODE,"");
// Step 1: Authorize -->
String authorizationUrl = new GoogleBrowserClientRequestUrl(clientId, redirectUrl, Arrays.asList(scope)).build();
// Point or redirect your user to the authorizationUrl.
System.out.println("Go to the following link in your browser:");
System.out.println(authorizationUrl);
// Read the authorization code from the standard input stream.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is the authorization code?");
String code = in.readLine();
// End of Step 1 <--
// Step 2: Exchange -->
GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(httpTransport, jsonFactory, clientId, clientSecret, serverAuthCode, redirectUrl).execute();
// End of Step 2 <--
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setClientSecrets(clientId, clientSecret)
.build()
.setFromTokenResponse(tokenResponse);
PeopleService peopleService = new PeopleService.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(getString(R.string.app_name))
.build();
ListConnectionsResponse response = peopleService.people().connections()
.list("people/me")
.setPersonFields("names,emailAddresses")
.execute();
// Print display name of connections if available.
List<Person> connections = response.getConnections();
if (connections != null && connections.size() > 0) {
for (Person person : connections) {
List<Name> names = person.getNames();
if (names != null && names.size() > 0) {
myLog(TAG,DEBUG_OK,"Name: " + person.getNames().get(0).getDisplayName());
List<EmailAddress> emailAddresses = person.getEmailAddresses();
if (emailAddresses != null && emailAddresses.size() > 0) {
for (EmailAddress email: emailAddresses)
myLog(TAG,DEBUG_OK,"email: " + email.getValue());
}
}
else {
myLog(TAG,DEBUG_OK,"No names available for connection.");
}
}
}
else {
System.out.println("No connections found.");
}
}
I was hoping that this would get all available contacts, however it returns only a small subset. So my question is whether I need to pass / use any other scopes to read all contacts, including the "other contacts" list.

The People API doesn't appear to support the "Other Contacts" contacts as described in this answer. You should use the Contacts API to get the data you want.

People API allows to fetch other Contacts now as described here
https://developers.google.com/people/v1/other-contacts
ListOtherContactsResponse response = peopleService.otherContacts().list()
.setReadMask("metadata,names,emailAddresses")
.execute();
List<Person> otherContacts = response.getOtherContacts();

Related

Importing csv data from Storage to Cloud SQL not working - status always "pending"

I am new to java (I have experience with C# though)
Sadly, I inherited a terrible project (the code is terrible) and what I need to accomplish is to import some csv files into Cloud SQL
So there's a WS which runs this task, apparently the dev followed this guide to import data. But it is not working. Here's the code (Essential parts, actually it is longer and more ugly)
InstancesImportRequest requestBody = new InstancesImportRequest();
ImportContext ic = new ImportContext();
ic.setKind("sql#importContext");
ic.setFileType("csv");
ic.setUri(bucketPath);
ic.setDatabase(CLOUD_SQL_DATABASE);
CsvImportOptions csv = new CsvImportOptions();
csv.setTable(tablename);
List<String> list = new ArrayList<String>();
// here there is some code that populates the list with the columns
csv.setColumns(list);
ic.setCsvImportOptions(csv);
requestBody.setImportContext(ic);
SQLAdmin sqlAdminService = createSqlAdminService();
SQLAdmin.Instances.SQLAdminImport request = sqlAdminService.instances().sqladminImport(project, instance, requestBody);
Operation response = request.execute();
System.out.println("Executed : Going to sleep.>"+response.getStatus());
int c = 1;
while(!response.getStatus().equalsIgnoreCase("Done")){
Thread.sleep(10000);
System.out.println("sleeped enough >"+response.getStatus());
c++;
if(c==50){
System.out.println("timeout?");
break;
}
}
public static SQLAdmin createSqlAdminService() throws IOException, GeneralSecurityException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = GoogleCredential.getApplicationDefault();
if (credential.createScopedRequired()) {
credential =
credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
}
return new SQLAdmin.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("Google-SQLAdminSample/0.1")
.build();
}
I am not quite sure how response should be treated, it seems it is an async request. Either way, I always get status Pending; it seems it is not even start to executing.
Of course it ends timing out. What is wrong here, why the requests never starts ? I couldn't find any actual example on the internet about using this java sdk to import files, except the link I gave above
Well, the thing is that the response object is static, so it will always return "Pending" as the initial status since it is a string in the object - it is not actually being updated.
To get the actual status, you have to requested it to google using the sdk. I did something like this (it will be better to use a smaller sleep time, and make it grow as you try more times)
SQLAdmin.Instances.SQLAdminImport request = sqlAdminService.instances().sqladminImport(CLOUD_PROJECT, CLOUD_SQL_INSTANCE, requestBody);
// execution of our import request
Operation response = request.execute();
int tried = 0;
Operation statusOperation;
do {
// sleep one minute
Thread.sleep(60000);
// here we are requesting the status of our operation. Name is actually the unique identifier
Get requestStatus = sqlAdminService.operations().get(CLOUD_PROJECT, response.getName());
statusOperation = requestStatus.execute();
tried++;
System.out.println("status is: " + statusOperation.getStatus());
} while(!statusOperation.getStatus().equalsIgnoreCase("DONE") && tried < 10);
if (!statusOperation.getStatus().equalsIgnoreCase("DONE")) {
throw new Exception("import failed: Timeout");
}

Check if user is logged in aws cognito, JAVA

I am using Java as back end to communicate with AWS Cognito. I am able to login, logout, create users, sign out and other functions. I am also able to verify an access token by following this link:
But I want to verify if a user is logged in or not.
In JAVA, is there a isLoggedin() function that returns a boolean or is there a way to see if the token is revoked? These functions exist for Android and iOS but what about JAVA.
I thought this verifies if the token is active, but it only verifies if the token is in the right format:
// This parses token to JWT.
JWT jwtparser = JWTParser.parse(accessToken);
String JWTissuer = jwtparser.getJWTClaimsSet().getIssuer();
JWSHeader header = (JWSHeader) jwtparser.getHeader();
Object token_use = jwtparser.getJWTClaimsSet().getClaim("token_use");
Object exp = jwtparser.getJWTClaimsSet().getClaim("iat");
Date expirationDate = jwtparser.getJWTClaimsSet().getExpirationTime();
// Read in JSON Key file saved somewhere safe
File file = new File("jwks.json");
String content = FileUtils.readFileToString(file, "utf-8");
JSONObject JsonObjects = new JSONObject(content);
JSONArray keysArray = JsonObjects.getJSONArray("keys");
JSONObject keyString = (JSONObject) keysArray.get(1);
if (header.getKeyID().equals(keyString.get("kid")) && token_use.toString().equals("access") && JWTissuer.equals("https://cognito-idp.us-west-2.amazonaws.com/us-west-2_xxxxxxx")) {
return true;
} else { return false; }
I want to see if a user is logged in. I have not found an appropriate method to do so.
Mahalo
I found a work around:
public boolean isLoggedin(String accessToken) {
GetUserRequest request = new GetUserRequest();
request.withAccessToken(accessToken);
AWSCognitoIdentityProviderClientBuilder builder =
AWSCognitoIdentityProviderClientBuilder.standard();
builder.withRegion("us-west-2");
AWSCognitoIdentityProvider cognitoCreate = builder.build();
GetUserResult result = cognitoCreate.getUser(request);
try {
System.out.println("success");
return true;
} catch (Exception e) {
e.getMessage();
return false;
}
}

Insert row without overwritting data

I end up here a lot from google and since I started trying to make an android app for myself I'm stumped since the v4 API isn't very helpful. I've search on here but I haven't seen an answer that answer my question.
I have a budget spreadsheet. My app is going to grab the data from the sheet and filter it to my current payweek. So I can look at all my current transactions and future ones so I can know how much I have extra to spend. Then I want to just be able to insert a new traction from the app, say if I get gas I want to be able to add that in a couple clicks rather than try to spend a few minutes editing the sheets in the sheet app. Later I plan to customize the formula for the cell I'm putting it in as well as copy it to the cells north and south of it so the math works out.
I used the Android Quickstart to be able to read data easy enough. I've since tweaked that to filter the sheets data to filter my data. I've even gone as far to get it be able to write data to the sheet. The problem is that I can't find any example in java/android in the same style as the quick start to know how to insert a row in the middle of the sheet instead of overwrite the cells. I'm assuming I have to use the INSERTDATAOPTION=INSERT_ROWS somewhere but I can't find anywhere to add it. The documentation suggest I use spreadsheets.values.append but .append never shows up as an option. I only get BatchGet, BatchUpdate, Get, Update.
I'm a beginner and I'm sure my code is clumsy that I've pieced together after hours of googling but I'm sure I'm just missing something easy. Any help would be appreciated.
private List<String> PostDataForApi() throws IOException {
String spreadsheetID = getResources().getString(R.string.my_google_spreadsheet_id);
Integer sheetID = getResources().getInteger(R.integer.my_google_sheet_id);
List<RowData> rowData = new ArrayList<RowData>();
List<CellData> cellData = new ArrayList<CellData>();
String value = "test";
String formula = "=IF(COUNTBLANK(C510) = 2,\"\",Sum(B511+(SUM(C510))))";
String value2 = "999";
CellData cell = new CellData();
cell.setUserEnteredValue(new ExtendedValue().setStringValue(value));
CellData cell2 = new CellData();
cell2.setUserEnteredValue(new ExtendedValue().setFormulaValue(formula));
CellData cell3 = new CellData();
cell3.setUserEnteredValue(new ExtendedValue().setStringValue(value2));
cellData.add(cell);
cellData.add(cell2);
cellData.add(cell3);
rowData.add(new RowData().setValues(cellData));
BatchUpdateSpreadsheetRequest batchRequests = new BatchUpdateSpreadsheetRequest();
BatchUpdateSpreadsheetResponse response;
List<Request> requests = new ArrayList<Request>();
AppendCellsRequest appendCellReq = new AppendCellsRequest();
appendCellReq.setSheetId(sheetID);
appendCellReq.setRows( rowData );
appendCellReq.setFields("userEnteredValue");
requests = new ArrayList<Request>();
requests.add( new Request().setAppendCells(appendCellReq));
batchRequests = new BatchUpdateSpreadsheetRequest();
batchRequests.setRequests( requests );
response = this.mService.spreadsheets().batchUpdate(spreadsheetID, batchRequests).execute();
System.out.println(response.toPrettyString());
return null;
}
I figured it out after some mind numbing throw things against the wall and see what sticks. I had to do it in two steps. The first step will insert a row into your sheet at row 32 so you'll get a blank row 33. Then the second area will insert values into that blank row. I hope this helps someone in the future.
String spreadsheetID = getResources().getString(R.string.my_google_spreadsheet_id);
Integer sheetID = getResources().getInteger(R.integer.my_google_sheet_id);
BatchUpdateSpreadsheetResponse response;
BatchUpdateSpreadsheetRequest batchRequests = new BatchUpdateSpreadsheetRequest();
List<Request> requests = new ArrayList<Request>();
InsertDimensionRequest insertDimensionRequest = new InsertDimensionRequest();
DimensionRange dimRange = new DimensionRange();
dimRange.setStartIndex(32);
dimRange.setEndIndex(33);
dimRange.setSheetId(sheetID);
dimRange.setDimension("ROWS");
insertDimensionRequest.setRange(dimRange);
insertDimensionRequest.setInheritFromBefore(false);
requests.add( new Request().setInsertDimension(insertDimensionRequest));
batchRequests = new BatchUpdateSpreadsheetRequest();
batchRequests.setRequests( requests );
response = this.mService.spreadsheets().batchUpdate(spreadsheetID, batchRequests).execute();
System.out.println(response.toPrettyString());
List<List<Object>> argData = getData(entryTitle, entryValue);
ValueRange vRange = new ValueRange();
vRange.setRange("2016!A33");
vRange.setValues(argData);
List<ValueRange> vList = new ArrayList<>();
vList.add(vRange);
BatchUpdateValuesRequest batchRequest = new BatchUpdateValuesRequest();
batchRequest.setValueInputOption("USER-ENTERED");
batchRequest.setData(vList);
this.mService.spreadsheets().values().batchUpdate(spreadsheetID, batchRequest).execute();
I've been having sooo much unnecessary hustle with the v4 google sheets api, that it was ridiculous. Therefore, I reverted to the gdata (v3) api, much better easier to follow and much better documented also.
Here is the link to setting it up and a few examples https://developers.google.com/google-apps/spreadsheets/
The only thing missing was the authorization process in those notes, which was a hustle, but after some digging I was able to get a authorization code base, as shown below.
public class YourClass {
// Application name
private static final String APPLICATION_NAME = "Your-Application-Name";
// account info and p12
private static final String ACCOUNT_P12_ID = "Get-the-details-developer-console-google";
private static final File P12FILE = new File("D:/path/Drive API Test-bf290e0ee314.p12");
// scopes
private static final List<String> SCOPES = Arrays.asList(
"https://docs.google.com/feeds",
"https://spreadsheets.google.com/feeds");
// Spreadsheet API URL
private static final String SPREADSHEET_URL = "https://spreadsheets.google.com/feeds/spreadsheets/private/full";
private static final URL SPREADSHEET_FEED_URL;
static {
try {
SPREADSHEET_FEED_URL = new URL(SPREADSHEET_URL);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
// Authorize
private static Credential authorize() throws Exception {
System.out.println("authorize in");
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(ACCOUNT_P12_ID)
.setServiceAccountPrivateKeyFromP12File(P12FILE)
.setServiceAccountScopes(SCOPES)
.build();
boolean ret = credential.refreshToken();
// debug dump
System.out.println("refreshToken:" + ret);
// debug dump
if (credential != null) {
System.out.println("AccessToken:" + credential.getAccessToken());
}
System.out.println("authorize out");
return credential;
}
// Get service
private static SpreadsheetService getService() throws Exception {
System.out.println("service in");
SpreadsheetService service = new SpreadsheetService(APPLICATION_NAME);
service.setProtocolVersion(SpreadsheetService.Versions.V3);
Credential credential = authorize();
service.setOAuth2Credentials(credential);
// debug dump
System.out.println("Schema: " + service.getSchema().toString());
System.out.println("Protocol: " + service.getProtocolVersion().getVersionString());
System.out.println("ServiceVersion: " + service.getServiceVersion());
System.out.println("service out");
return service;
}
From that onward, I was able to perform a number of inserts and appends to the sheets, without any major hustle.

How do I create a Google Spreadsheet with a service account and share to other google users in java?

I have an application where I, with a Google Service Account, gather lots of information about my site from the Analytics API.
My next step is to create a spreadsheet with the service account
and share the document with a couple of users.
I have checked out the documentation at https://developers.google.com/google-apps/spreadsheets/ but I can't find anything there about service accounts and sharing the documents.
So my first question is this possible?
If not do I need to implement the "use my personal account" as exemplified in the documentation?
If yes could you please provide me with an example?
Thank you!
It is possible, see the example below (the example does need a bit of tweaking):
Create the drive service:
GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY)
.setServiceAccountId(confBean.getServiceAccountId()).setServiceAccountScopes("https://www.googleapis.com/auth/drive")
.setServiceAccountPrivateKeyFromP12File(new File("path to the P12File"))
.setServiceAccountUser("user#domain.com")
.build();
Drive drive = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build();
Create the spreadsheet:
com.google.api.services.drive.model.File file = new com.google.api.services.drive.model.File();
file.setTitle("test");
file.setMimeType("application/vnd.google-apps.spreadsheet");
Insert insert = this.drive.files().insert(file);
file = insert.execute();
Create a spreadsheet service:
GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY)
.setServiceAccountId(confBean.getServiceAccountId()).setServiceAccountScopes("https://spreadsheets.google.com/feeds")
.setServiceAccountPrivateKeyFromP12File(new File("path to the P12File"))
.setServiceAccountUser("user#domain.com")
.build();
SpreadsheetService service = new SpreadsheetService("MySpreadsheetIntegration-v1");
service.setOAuth2Credentials(credential);
Retrieve the sheet:
SpreadsheetService s = googleConn.getSpreadSheetService();
String spreadsheetURL = "https://spreadsheets.google.com/feeds/spreadsheets/" + file.getId();
SpreadsheetEntry spreadsheet = s.getEntry(new URL(spreadsheetURL), SpreadsheetEntry.class);
Add the data:
WorksheetFeed worksheetFeed = s.getFeed(spreadsheet.getWorksheetFeedUrl(), WorksheetFeed.class);
List<WorksheetEntry> worksheets = worksheetFeed.getEntries();
WorksheetEntry worksheet = worksheets.get(0);
URL cellFeedUrl= worksheet.getCellFeedUrl ();
CellFeed cellFeed= s.getFeed (cellFeedUrl, CellFeed.class);
CellEntry cellEntry= new CellEntry (1, 1, "aa");
cellFeed.insert (cellEntry);
Also, see this related question

Facebook login plugin with server side access to users data

I have got few things to work e.g. Using -
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
I am able to get all the details of the user, name, User ID etc.
My Problem is how to take all this information to the server "safely". I don't want this information to be sniffed on its way to server. I use JAVA(Servet/JSP) language, PLEASE HELP ME ON THIS. I wish there was some way like registration plugin where Facebook sends all the information on a redirect_url link.
Regards,
Jagpreet Singh
EDIT: If anybody requires the Java Code -
// it is important to enable url-safe mode for Base64 encoder
Base64 base64 = new Base64(true);
// split request into signature and data
String[] signedRequest = request.getParameter("signed_request").split("\\.", 2);
logger.info("Received signed_request = " + Arrays.toString(signedRequest));
// parse signature
String sig = new String(base64.decode(signedRequest[0].getBytes("UTF-8")));
// parse data and convert to JSON object
JSONObject data = (JSONObject) JSONSerializer.toJSON(new String(base64.decode(signedRequest[1].getBytes("UTF-8"))));
logger.warn("JSON Value = " + data);
// check signature algorithm
if (!"HMAC-SHA256".equals(data.getString("algorithm"))) {
// unknown algorithm is used
logger.error("HMAC-SHA256 Algo? = false, returning ERROR");
return ERROR;
} else {
logger.error("HMAC-SHA256 Algo? = true, Checking if data is signed correctly...");
}
// check if data is signed correctly
if (!hmacSHA256(signedRequest[1], fbSecretKey).equals(sig)) {
// signature is not correct, possibly the data was tampered with
logger.warn("DATA signed correctly? = false, returning ERROR");
return ERROR;
} else {
logger.warn("DATA signed correctly? = true, checking if user has authorized the APP...");
}
// check if user authorized the APP (FACEBOOK User)
if (!data.has("user_id") || !data.has("oauth_token")) {
// this is guest, create authorization url that will be passed
// to javascript
// note that redirect_uri (page the user will be forwarded to
// after authorization) is set to fbCanvasUrl
logger.warn("User has authorized the APP? = false, returning ERROR");
return ERROR;
} else {
logger.warn("User has authorized the APP? = true, Performing User Registration...");
// this is authorized user, get their info from Graph API using
// received access token
// String accessToken = data.getString("oauth_token");
// FacebookClient facebookClient = new
// DefaultFacebookClient(accessToken);
// User user = facebookClient.fetchObject("me", User.class);
}
Facebook sends a signed_request parameter when you authenticate with a client-side method. You can pass this to your server, authenticate it, and then unpack it to get at the information you need. It is encrypted with your app secret, so you can be sure that it is secure.
See the signed_request documentation for more information.

Categories

Resources