2017-04-15 125 views
0

我試圖抓取Google通訊錄,但當用戶登錄選項出現時,它顯示請求Know your age range and language的權限。但沒有聯繫人權限。當用戶登錄成功時,它會將我重定向到谷歌搜索頁面。Google Contacts OAUTH2

@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方案

enter image description here

+0

的任務似乎很簡單,因爲你沒有添加範圍來請求人 –

+0

@Man uGupta在哪裏? – Nitesh

+0

檢查'getPeopleList' – Nitesh

回答

1
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() 

} 

您需要添加這些範圍,然後嘗試爲知道你是隻要求允許獲取您的信息

// 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 
} 
+0

這幫助我顯示聯繫人權限,但登錄後它會轉到谷歌搜索頁面。這個問題仍然存在。 – Nitesh

+0

您是否在您的應用程序中實施了url方案以將您重定向迴應用程序。如果url.scheme ==「」{ returnScheme = GIDSignIn.sharedInstance()。句柄(url, sourceApplication:sourceApplication, 註釋:註釋) } –

+0

是的。如果我錯過添加url方案,應用程序會崩潰。 – Nitesh