Gmail API只读属性403 iOS上禁止的错误

Gmail API只读属性403 iOS上禁止的错误,ios,oauth,oauth-2.0,google-oauth,gmail-api,Ios,Oauth,Oauth 2.0,Google Oauth,Gmail Api,我跟着 在view controller中,我添加了一个按钮来启动gmail api配置 @IBOutlet weak var btnSyncGmail: UIButton! //button to configure Gmail API 我在这里提到了oAuth的客户机id,我在中注释了隐藏客户机id****** private let kClientID = "*******************.apps.googleusercontent.com" private let kRedi

我跟着

在view controller中,我添加了一个按钮来启动gmail api配置

@IBOutlet weak var btnSyncGmail: UIButton! //button to configure Gmail API
我在这里提到了oAuth的客户机id,我在中注释了隐藏客户机id******

private let kClientID = "*******************.apps.googleusercontent.com"
private let kRedirectURI = "com.googleusercontent.apps.***********************:/oauthredirect"

private let kKeychainItemName = "Sync Gmail"
// If modifying these scopes, delete your previously saved credentials by
// resetting the iOS simulator or uninstall the app.
private let scopes = [kGTLRAuthScopeGmailReadonly]

private let service = GTLRGmailService()
let output = UITextView()

// When the view loads, create necessary subviews
// and initialize the Gmail API service
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// Construct a query and get a list of upcoming labels from the gmail API
func fetchLabels() {
    output.text = "Getting labels..."

    let query = GTLRGmailQuery_UsersLabelsList.query(withUserId: "me")
    service.executeQuery(query,
                         delegate: self,
                         didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:))
    )
}

// Display the labels in the UITextView
func displayResultWithTicket(ticket : GTLRServiceTicket,
                             finishedWithObject labelsResponse : GTLRGmail_ListLabelsResponse,
                              error : NSError?) {

    if let error = error {
        showAlert(title: "Error", message: error.localizedDescription)
        return
    }

    var labelString = ""

    if (labelsResponse.labels?.count)! > 0 {
        labelString += "Labels:\n"
        for label in labelsResponse.labels! {
            labelString += "\(label.name!)\n"
        }
    } else {
        labelString = "No labels found."
    }

    output.text = labelString

}


// Creates the auth controller for authorizing access to Gmail API
private func createAuthController() -> GTMOAuth2ViewControllerTouch {
    let scopeString = scopes.joined(separator: " ")
    return GTMOAuth2ViewControllerTouch(
        scope: scopeString,
        clientID: kClientID,
        clientSecret: nil,
        keychainItemName: kKeychainItemName,
        delegate: self,
        finishedSelector: #selector(viewController(vc:finishedWithAuth:error:))
    )
}

// Handle completion of the authorization process, and update the Gmail API
// with the new credentials.
func viewController(vc : UIViewController,
                    finishedWithAuth authResult : GTMOAuth2Authentication, error : NSError?) {

    if let error = error {
        service.authorizer = nil
        showAlert(title: "Authentication Error", message: error.localizedDescription)
        return
    }

    service.authorizer = authResult
    dismiss(animated: true, completion: nil)
}

// Helper for showing an alert
func showAlert(title : String, message: String) {
    let alert = UIAlertController(
        title: title,
        message: message,
        preferredStyle: UIAlertControllerStyle.alert
    )
    let ok = UIAlertAction(
        title: "OK",
        style: UIAlertActionStyle.default,
        handler: nil
    )
    alert.addAction(ok)
    present(alert, animated: true, completion: nil)
}
btnSyncGmail按钮操作

@IBAction func startSyncingGmail(_ sender: UIButton) {

    if let authorizer = service.authorizer,
        let canAuth = authorizer.canAuthorize , canAuth {
        fetchLabels()
    } else {
        present(
            createAuthController(),
            animated: true,
            completion: nil
        )
    }

    output.frame = view.bounds
    output.isEditable = false
    output.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
    output.autoresizingMask = [.flexibleHeight, .flexibleWidth]

    view.addSubview(output);

    if let auth = GTMOAuth2ViewControllerTouch.authForGoogleFromKeychain(
        forName: kKeychainItemName,
        clientID: kClientID,
        clientSecret: nil) {
        service.authorizer = auth
    }
}

我运行应用程序;点击按钮后,加载程序在几秒钟后启动,我得到“HTTP 403禁止”访问错误作为输出。我不明白为什么我会这样。我遗漏了什么吗?

403禁止的错误是由于作用域错误造成的,请尝试再次检查是否使用了正确的作用域。检查此项以了解每个范围的描述。使用此
https://mail.google.com/
允许在Gmail API中完全或完全访问。另外,别忘了启用Gmail API和您在开发控制台中使用的其他API。

我在开发控制台中检查,Gmail API已启用,我尝试了您提到的范围值,但我得到了相同的403禁止错误
@IBAction func startSyncingGmail(_ sender: UIButton) {

    if let authorizer = service.authorizer,
        let canAuth = authorizer.canAuthorize , canAuth {
        fetchLabels()
    } else {
        present(
            createAuthController(),
            animated: true,
            completion: nil
        )
    }

    output.frame = view.bounds
    output.isEditable = false
    output.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
    output.autoresizingMask = [.flexibleHeight, .flexibleWidth]

    view.addSubview(output);

    if let auth = GTMOAuth2ViewControllerTouch.authForGoogleFromKeychain(
        forName: kKeychainItemName,
        clientID: kClientID,
        clientSecret: nil) {
        service.authorizer = auth
    }
}