Swift
Swift
Google People API - List the User's Contacts
See more Google People Examples
Gets a list of people in the user's contacts.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// 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
let jsonToken = CkoJsonObject()!
success = jsonToken.loadFile(path: "qa_data/tokens/googlePeople.json")
if success != true {
print("Failed to load googleContacts.json")
return
}
let http = CkoHttp()!
http.authToken = jsonToken.string(of: "access_token")
let sbResponseBody = CkoStringBuilder()!
success = http.quickGetSb(url: "https://people.googleapis.com/v1/people/me/connections?personFields=names,addresses,emailAddresses", sbContent: sbResponseBody)
if success == false {
print("\(http.lastErrorText!)")
print("\(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
// }
let json = CkoJsonObject()!
json.emitCompact = false
json.load(json: sbResponseBody.getAsString())
print("\(json.emit()!)")
var resourceName: String?
var etag: String?
var j: Int
var count_j: Int
var metadataPrimary: Bool
var metadataSourceType: String?
var metadataSourceId: String?
var displayName: String?
var familyName: String?
var givenName: String?
var displayNameLastFirst: String?
var unstructuredName: String?
var value: String?
var formattedValue: String?
var streetAddress: String?
var city: String?
var region: String?
var postalCode: String?
var country: String?
var countryCode: String?
var totalPeople: Int = json.int(of: "totalPeople").intValue
var totalItems: Int = json.int(of: "totalItems").intValue
var i: Int = 0
var count_i: Int = json.size(ofArray: "connections").intValue
while i < count_i {
json.i = i
resourceName = json.string(of: "connections[i].resourceName")
etag = json.string(of: "connections[i].etag")
j = 0
count_j = json.size(ofArray: "connections[i].names").intValue
while j < count_j {
json.j = j
metadataPrimary = json.bool(of: "connections[i].names[j].metadata.primary")
metadataSourceType = json.string(of: "connections[i].names[j].metadata.source.type")
metadataSourceId = json.string(of: "connections[i].names[j].metadata.source.id")
displayName = json.string(of: "connections[i].names[j].displayName")
familyName = json.string(of: "connections[i].names[j].familyName")
givenName = json.string(of: "connections[i].names[j].givenName")
displayNameLastFirst = json.string(of: "connections[i].names[j].displayNameLastFirst")
unstructuredName = json.string(of: "connections[i].names[j].unstructuredName")
j = j + 1
}
j = 0
count_j = json.size(ofArray: "connections[i].emailAddresses").intValue
while j < count_j {
json.j = j
metadataPrimary = json.bool(of: "connections[i].emailAddresses[j].metadata.primary")
metadataSourceType = json.string(of: "connections[i].emailAddresses[j].metadata.source.type")
metadataSourceId = json.string(of: "connections[i].emailAddresses[j].metadata.source.id")
value = json.string(of: "connections[i].emailAddresses[j].value")
j = j + 1
}
j = 0
count_j = json.size(ofArray: "connections[i].addresses").intValue
while j < count_j {
json.j = j
metadataPrimary = json.bool(of: "connections[i].addresses[j].metadata.primary")
metadataSourceType = json.string(of: "connections[i].addresses[j].metadata.source.type")
metadataSourceId = json.string(of: "connections[i].addresses[j].metadata.source.id")
formattedValue = json.string(of: "connections[i].addresses[j].formattedValue")
streetAddress = json.string(of: "connections[i].addresses[j].streetAddress")
city = json.string(of: "connections[i].addresses[j].city")
region = json.string(of: "connections[i].addresses[j].region")
postalCode = json.string(of: "connections[i].addresses[j].postalCode")
country = json.string(of: "connections[i].addresses[j].country")
countryCode = json.string(of: "connections[i].addresses[j].countryCode")
j = j + 1
}
i = i + 1
}
}