代码之家  ›  专栏  ›  技术社区  ›  Anilkumar iOS Developer

无法通过在Swift中调用API获取Google联系人

  •  0
  • Anilkumar iOS Developer  · 技术社区  · 7 年前

    我能够成功登录到谷歌登录,并能够获取日历事件。 但是,在尝试获取Google联系人时,它抛出了403错误,但是,我已经在Google开发者控制台中启用了我的项目API。

    private let scopes = ["https://www.googleapis.com/auth/calendar.readonly","https://www.googleapis.com/auth/plus.login","https://www.googleapis.com/auth/plus.me","https://www.googleapis.com/auth/contacts.readonly"]
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
            GIDSignIn.sharedInstance().clientID = kClientID
            GIDSignIn.sharedInstance().uiDelegate = self
            GIDSignIn.sharedInstance().scopes = scopes 
    
            // GIDSignIn.sharedInstance().signIn()
            GIDSignIn.sharedInstance().delegate = self
            self.myTableView.isHidden = true
            self.defaultButton.isHidden = false
            dataArray.removeAllObjects()
            self.myTableView.estimatedRowHeight = UITableViewAutomaticDimension
            self.myTableView.rowHeight = UITableViewAutomaticDimension
        }
     func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
            if (error == nil) {
    
                userEmailID = user.profile.email
            } else {
                print("\(error.localizedDescription)")
            }
    
            if let error = error {
                showAlert(title: "Authentication Error", message: error.localizedDescription)
                self.service.authorizer = nil
                self.myTableView.isHidden = true
                self.defaultButton.isHidden = false
            } else {
                self.service.authorizer = user.authentication.fetcherAuthorizer()
                fetchEvents()
                getPeopleList()
                self.myTableView.isHidden = false
                self.defaultButton.isHidden = true
                self.myTableView.dataSource = self
                self.myTableView.delegate = self
            }
    
        }
    
    
      func getPeopleList() {
            if let accessToken = GIDSignIn.sharedInstance().currentUser.authentication.accessToken {
                let urlString = ("https://www.googleapis.com/plus/v1/people/me/people/visible?access_token=\(accessToken)")
    //            let url = NSURL(string: urlString)
                print(accessToken)
                print(urlString)
    
                Alamofire.request(urlString).response { response in // method defaults to `.get`
                    debugPrint(response.data)
                    print(response)
                }
            }
    
        }
    

    { Status Code: 403, Headers {
        "Cache-Control" =     (
            "private, max-age=0"
        );
        "Content-Encoding" =     (
            gzip
        );
        "Content-Length" =     (
            136
        );
        "Content-Type" =     (
            "application/json; charset=UTF-8"
        );
        Date =     (
            "Tue, 31 Jul 2018 13:35:51 GMT"
        );
        Expires =     (
            "Tue, 31 Jul 2018 13:35:51 GMT"
        );
        Server =     (
            GSE
        );
        Vary =     (
            Origin,
            "X-Origin"
        );
        "Www-Authenticate" =     (
            "Bearer realm=\"https://accounts.google.com/\", error=insufficient_scope, scope=\"https://www.googleapis.com/auth/plus.login\""
        );
        "alt-svc" =     (
            "quic=\":443\"; ma=2592000; v=\"44,43,39,35\""
        );
        "x-content-type-options" =     (
            nosniff
        );
        "x-frame-options" =     (
            SAMEORIGIN
        );
        "x-xss-protection" =     (
            "1; mode=block"
        );
    } }
    

    0 回复  |  直到 7 年前