2016-12-20 2 views
-1

에만 속성 403 금지 된 에러를 읽고 난 OAUTH 위해 클라이언트 ID를 언급의 Gmail API는 I보기 제어기 <a href="https://developers.google.com/gmail/api/quickstart/ios?ver=swift" rel="nofollow noreferrer">this url</a></p> <pre><code>import GoogleAPIClientForREST import GTMOAuth2 </code></pre> <p>따라 아이폰 OS

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

에서 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 버튼 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 금지 된 오류는 범위 지정 오류로 인해 올바른 범위를 사용하는지 다시 한 번 확인하십시오. 각 범위에 대한 설명을 보려면 Gmail scopes을 확인하십시오. 이 https://mail.google.com/ 범위를 사용하면 Gmail API에서 전체 또는 전체 액세스 권한을 가질 수 있습니다. 또한 개발자 콘솔에서 사용중인 Gmail API 및 기타 API를 사용 설정하는 것을 잊지 마십시오.

+0

데브 콘솔에서 Gmail API를 사용하도록 설정했는데 내가 언급 한 범위 값을 시도했지만 동일한 403 오류가 발생했습니다. –