Nitesh
Nitesh

Reputation: 2024

Google Contacts OAUTH2

I'm trying to fetch Google Contacts but when user login option appears on it shows asks permission for Know your age range and language. But no permissions for contacts. Also when user is logged in successfully it redirects me to google search page.

@IBOutlet weak var signInButton: GIDSignInButton!
var peopleDataArray: Array<Dictionary<NSObject, AnyObject>> = []
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    GIDSignIn.sharedInstance().delegate = self
    GIDSignIn.sharedInstance().uiDelegate = self
    GIDSignIn.sharedInstance().clientID = ""

    GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/plus.login")
    GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/plus.me")

    GIDSignIn.sharedInstance().signInSilently()

}
fileprivate func performGetRequest(targetURL: URL, completion: @escaping (_ data: Data?, _ HTTPStatusCode: Int, _ error: NSError?) -> Void){
    let request = NSMutableURLRequest(url: targetURL)
    request.httpMethod = "GET"

    let sessionConfiguration = URLSessionConfiguration.default
    let session = URLSession(configuration: sessionConfiguration)

    let task = session.dataTask(with: request as URLRequest) { (data, response, error) in
        DispatchQueue.main.async(execute: { () -> Void in
            completion(data, (response as! HTTPURLResponse).statusCode, error as NSError?)
        })        }
    task.resume()
}


func getPeopleList() {
    let urlString = ("https://www.googleapis.com/plus/v1/people/me/people/visible?access_token=\(GIDSignIn.sharedInstance().currentUser.authentication.accessToken)")
    let url = NSURL(string: urlString)

    URLSession.shared.dataTask(with: (url as? URLRequest)!, completionHandler: {
        (data, response, error) in
        if(error != nil){
            print("error")
        }else{
            do{
                let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! Dictionary<NSObject, AnyObject>
                                // Get the array with people data dictionaries.

                print(json)
                OperationQueue.main.addOperation({
                })

            }catch let error as NSError{
                print(error)
            }
        }
    }).resume()

}

}

    extension ViewController: GIDSignInDelegate, GIDSignInUIDelegate{


        func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
            if let err = error{
                print(error.localizedDescription)
            }else{
                getPeopleList()
            }
        }


    }

URL Scheme

enter image description here

Upvotes: 0

Views: 101

Answers (1)

Manu Gupta
Manu Gupta

Reputation: 1759

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
    GIDSignIn.sharedInstance().delegate = self
    GIDSignIn.sharedInstance().uiDelegate = self
    GIDSignIn.sharedInstance().clientID = ""

GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/plus.login")
GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/plus.me")
GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/contacts.readonly")


GIDSignIn.sharedInstance().signInSilently()

}

You need to add these scopes and then try as know you are only asking permission to get your information

 // App Delegate Handle Url Scheme
 open func application(_ application: UIApplication, open url: URL, 
 sourceApplication: String?, annotation: Any) -> Bool {
    var returnScheme: Bool!
    if url.scheme == "dropbox"{
        returnScheme = true
    }else if url.scheme == "Goolge URL SCHEME Which you have added in plist file"{
        returnScheme = GIDSignIn.sharedInstance().handle(url,
                                                         sourceApplication: sourceApplication,
                                                         annotation: annotation)
    }
    return returnScheme
}

Upvotes: 1

Related Questions