I am having a lot of trouble integrating Google contacts API into my app. Basically what I need is to request permission to the user and fetch all of the user's Gmail contacts(name and email).
For reference this is where the Google Contacts API is: https://developers.google.com/google-apps/contacts/v3/
I already have Google SignIn implemented correctly and the user authenticated that way.
I am really struggling to put all the pieces together, here's what I have so far:
func retrieveContacts(){
let clientID = "blabla-5blablablabla9gisc.apps.googleusercontent.com"
let clientSecret = "blablablaDXEwmgilOXgVsQ"
//How do I ask permission to the user to look at their contacts?
let url2 = URL(string: "https://www.googleapis.com/auth/contacts.readonly")
let user = GIDSignIn.sharedInstance().currentUser
//let name = user?.profile.name
let email = user?.profile.email
var url = URL(string:"")
if let aEmail = email{
url = URL(string: "https://www.google.com/m8/feeds/contacts/\(aEmail)/full")
print(email)
}
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard error == nil else {
print(error)
return
}
guard let data = data else {
print("Data is empty")
return
}
print(response)
}
task.resume()
}
Right now I am getting an Error Code:401 Not authorized, which is obvious because I am not really requesting permission to the user anywhere.
In Swift 4.2 and Xcode 10.1
It's fetching gmail contacts along with saved phone numbers in JSON formate.
But before that you need to mention Scopes
Definitely you will get good response in JSON formate.
Xcode 8 Swift 3
You have to send access token in your request along with the Contacts API Version to get full contacts list.