I this url gefolgtGmail API einzige Eigenschaft 403 verboten Fehler auf IOS lesen
import GoogleAPIClientForREST
import GTMOAuth2
In View-Controller, fügte ich eine Taste, um die gmail api Konfiguration zu initiieren
@IBOutlet weak var btnSyncGmail: UIButton! //button to configure Gmail API
I Client-ID für oAuth erwähnte hier ich kommentierte versteckte die Client-ID in ******
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 Schaltfläche acti auf
@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
}
}
Ich laufe die App; Nach dem Tippen auf den Button startete der Loader nach einigen Sekunden. Ich erhalte den Zugriffsfehler "HTTP 403 verboten" als Ausgabe. Ich verstehe nicht, warum ich das bekomme. Habe ich etwas verpasst?
Ich habe Dev-Konsole eingecheckt, Google Mail-API ist aktiviert und ich habe versucht, den Wert des Bereichs, den Sie erwähnt, aber ich bekomme den gleichen Fehler 403 Verboten –