我試圖抓取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方案
的任務似乎很簡單,因爲你沒有添加範圍來請求人 –
@Man uGupta在哪裏? – Nitesh
檢查'getPeopleList' – Nitesh