Google Contacts OAUTH2
问题描述:
我试图抓取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方案
答
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
}
的任务似乎很简单,因为你没有添加范围来请求人 –
@Man uGupta在哪里? – Nitesh
检查'getPeopleList' – Nitesh