Not able to fetch Google Contacts by calling API i

2019-03-03 11:12发布

问题:

I am trying to fetch Google Calendar events and Google Contacts to my Swift (iOS) application. I am able to login successfully to Google sign-in and able to fetching the Calendar events too. But, while trying to fetch Google Contacts, its throwing 403 error, but, I have already Enabled the API to my project in google developers console.

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)
            }
        }

    }

And after executing this program, its showing following error.

{ 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"
    );
} }

Can anyone suggest me, where I am doing wrong?