2016-12-20 46 views
-1

,我添加一个按钮来启动Gmail的API只读属性403禁止错误iOS上

@IBOutlet weak var btnSyncGmail: UIButton! //button to configure Gmail API 

我提到客户端ID为OAUTH Gmail的API配置在这里我评论隐藏的客户端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 button acti on

@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禁止”访问错误作为输出。我不明白为什么我要这样做。我错过了什么吗?

回答

0

403 Forbidden错误是由于作用域错误造成的,请尝试仔细检查您是否使用了正确的作用域。检查此Gmail scopes以了解每个范围的说明。使用此https://mail.google.com/范围可以在Gmail API中拥有完整或完整的访问权限。此外,请不要忘记启用开发者控制台中使用的Gmail API和其他API。

+0

我检查了开发者控制台,Gmail API已启用,并且我尝试了您提到的范围值,但我得到的是相同的403 Forbidden错误 –