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

时间:2018-07-31 13:38:17

标签: ios iphone swift google-calendar-api google-contacts

我正在尝试将Google日历事件和Google联系人获取到我的Swift(iOS)应用程序。 我能够成功登录Google登录,也能够获取日历事件。 但是,在尝试获取Google Contacts时,抛出403错误,但是,我已经在Google Developers Console中为我的项目启用了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 个答案:

没有答案