React Native
React Native
Google People API - List the User's Contacts
See more Google People Examples
Gets a list of people in the user's contacts.Chilkat React Native Downloads
import { Http, JsonObject, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// It is assumed we previously obtained an OAuth2 access token.
// This example loads the JSON access token file
// originally obtained by this example: Get Google People API OAuth2 Access Token
// or refreshed by this example: Refresh Google People API OAuth2 Access Token
const jsonToken = new JsonObject();
try {
jsonToken.loadFile('qa_data/tokens/googlePeople.json');
} catch {
console.log('Failed to load googleContacts.json');
return;
}
const http = new Http();
http.authToken = jsonToken.stringOf('access_token');
const sbResponseBody = new StringBuilder();
try {
await http.quickGetSbAsync('https://people.googleapis.com/v1/people/me/connections?personFields=names,addresses,emailAddresses', sbResponseBody);
} catch {
console.log(http.lastErrorText);
console.log(sbResponseBody.getAsString());
return;
}
// Sample JSON Response
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// {
// "connections": [
// {
// "resourceName": "people/c8871101035606120608",
// "etag": "%EgkBAj0JQhBANy4aBAECBQciDERLSGdVTFNPbzNJPQ==",
// "names": [
// {
// "metadata": {
// "primary": true,
// "source": {
// "type": "CONTACT",
// "id": "7b1c7b6409e718a0"
// }
// },
// "displayName": "Chilkat Cloud",
// "familyName": "Cloud",
// "givenName": "Chilkat",
// "displayNameLastFirst": "Cloud, Chilkat",
// "unstructuredName": "Chilkat Cloud"
// }
// ],
// "emailAddresses": [
// {
// "metadata": {
// "primary": true,
// "source": {
// "type": "CONTACT",
// "id": "7b1c7b6409e718a0"
// }
// },
// "value": "support@***.com"
// }
// ]
// },
// {
// "resourceName": "people/c7607335470312011517",
// "etag": "%EgkBAj0JQhBANy4aBAECBQciDGZETUtHVTVMazI4PQ==",
// "names": [
// {
// "metadata": {
// "primary": true,
// "source": {
// "type": "CONTACT",
// "id": "6992af4e0b2d36fd"
// }
// },
// "displayName": "Matt Smith",
// "familyName": "Smith",
// "givenName": "Matt",
// "displayNameLastFirst": "Smith, Matt",
// "unstructuredName": "Matt Smith"
// }
// ],
// "addresses": [
// {
// "metadata": {
// "primary": true,
// "source": {
// "type": "CONTACT",
// "id": "6992af4e0b2d36fd"
// }
// },
// "formattedValue": "2222 E Foorest Ave\nWheaton, IL 60999\nUS",
// "streetAddress": "2222 E Foorest Ave",
// "city": "Wheaton",
// "region": "IL",
// "postalCode": "60999",
// "country": "US",
// "countryCode": "US"
// }
// ],
// "emailAddresses": [
// {
// "metadata": {
// "primary": true,
// "source": {
// "type": "CONTACT",
// "id": "6992af4e0b2d36fd"
// }
// },
// "value": "matt@***.com"
// },
// {
// "metadata": {
// "source": {
// "type": "CONTACT",
// "id": "6992af4e0b2d36fd"
// }
// },
// "value": "admin@***.com"
// }
// ]
// }
// ],
// "totalPeople": 2,
// "totalItems": 2
// }
const json = new JsonObject();
json.emitCompact = false;
json.load(sbResponseBody.getAsString());
console.log(json.emit());
let resourceName = '';
let etag = '';
let j = 0;
let countJ = 0;
let metadataSourceType = '';
let metadataSourceId = '';
let displayName = '';
let familyName = '';
let givenName = '';
let displayNameLastFirst = '';
let unstructuredName = '';
let value = '';
let formattedValue = '';
let streetAddress = '';
let city = '';
let region = '';
let postalCode = '';
let country = '';
let countryCode = '';
const totalPeople = json.intOf('totalPeople');
const totalItems = json.intOf('totalItems');
let i = 0;
const countI = json.sizeOfArray('connections');
while (i < countI) {
json.i = i;
resourceName = json.stringOf('connections[i].resourceName');
etag = json.stringOf('connections[i].etag');
j = 0;
countJ = json.sizeOfArray('connections[i].names');
while (j < countJ) {
json.j = j;
json.boolOf('connections[i].names[j].metadata.primary');
metadataSourceType = json.stringOf('connections[i].names[j].metadata.source.type');
metadataSourceId = json.stringOf('connections[i].names[j].metadata.source.id');
displayName = json.stringOf('connections[i].names[j].displayName');
familyName = json.stringOf('connections[i].names[j].familyName');
givenName = json.stringOf('connections[i].names[j].givenName');
displayNameLastFirst = json.stringOf('connections[i].names[j].displayNameLastFirst');
unstructuredName = json.stringOf('connections[i].names[j].unstructuredName');
j++;
}
j = 0;
countJ = json.sizeOfArray('connections[i].emailAddresses');
while (j < countJ) {
json.j = j;
json.boolOf('connections[i].emailAddresses[j].metadata.primary');
metadataSourceType = json.stringOf('connections[i].emailAddresses[j].metadata.source.type');
metadataSourceId = json.stringOf('connections[i].emailAddresses[j].metadata.source.id');
value = json.stringOf('connections[i].emailAddresses[j].value');
j++;
}
j = 0;
countJ = json.sizeOfArray('connections[i].addresses');
while (j < countJ) {
json.j = j;
json.boolOf('connections[i].addresses[j].metadata.primary');
metadataSourceType = json.stringOf('connections[i].addresses[j].metadata.source.type');
metadataSourceId = json.stringOf('connections[i].addresses[j].metadata.source.id');
formattedValue = json.stringOf('connections[i].addresses[j].formattedValue');
streetAddress = json.stringOf('connections[i].addresses[j].streetAddress');
city = json.stringOf('connections[i].addresses[j].city');
region = json.stringOf('connections[i].addresses[j].region');
postalCode = json.stringOf('connections[i].addresses[j].postalCode');
country = json.stringOf('connections[i].addresses[j].country');
countryCode = json.stringOf('connections[i].addresses[j].countryCode');
j++;
}
i++;
}
}