Google Contacts OAUTH2

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

enter image description here

+0

的任务似乎很简单,因为你没有添加范围来请求人 –

+0

@Man uGupta在哪里? – Nitesh

+0

检查'getPeopleList' – Nitesh

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