Swift MailCore2 GTMAppAuth,允许不太安全的应用程序访问

时间:2019-02-01 09:49:35

标签: swift google-api mailcore2

我正在使用MailCore2和GTMAppAuth构建测试应用程序,但遇到此错误:

  

无法建立与服务器的稳定连接。

我关注了诸如thisthisthis之类的SO帖子,因此我认为我的代码应该是正确的。我的实现如下:

//At AppDelegate
var currentAuthorizationFlow: OIDAuthorizationFlowSession?

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
{
    if currentAuthorizationFlow!.resumeAuthorizationFlow(with: url) {
        self.currentAuthorizationFlow = nil
        return true
    }
    return false
}

class ViewController: UIViewController {

    let kIssuer = "https://accounts.google.com"
    let kClientID = "\(MY_CLIENTID).apps.googleusercontent.com"
    let kRedirectURI = "com.googleusercontent.apps\(MY_CLIENTID):/oauthredirect"
    let kExampleAuthorizerKey = "googleOAuthCodingKey"

    override func viewDidLoad() {
        super.viewDidLoad()
        authenticateGmail()
    }

    func authenticateGmail() {
        let issuer = URL(string: kIssuer)!
        let redirectURI = URL(string: kRedirectURI)!
        let appDelegate = UIApplication.shared.delegate as! AppDelegate

        OIDAuthorizationService.discoverConfiguration(forIssuer: issuer) { (configuration, error) in
            //handleError

            if let configuration = configuration {
                let scopes = [OIDScopeOpenID, OIDScopeProfile, "https://mail.google.com/"]
                let request = OIDAuthorizationRequest(configuration: configuration, clientId: self.kClientID, scopes: scopes, redirectURL: redirectURI, responseType: OIDResponseTypeCode, additionalParameters: nil)

                appDelegate.currentAuthorizationFlow = OIDAuthState.authState(byPresenting: request, presenting: self, callback: { (authState, error) in
                    //handleError

                    if let authState = authState {
                        if let accessToken = authState.lastTokenResponse?.accessToken {
                            NSLog("Successfully authenticated: %@", accessToken)
                            self.fetchEmailsFromGmail(accessToken: accessToken)
                        }
                    }
                })
            }
        }
    }

    func fetchEmailsFromGmail(accessToken: String) {
        let session = MCOIMAPSession()
        session.hostname = "imap.gmail.com"
        session.port = 993
        session.username = "XXX@\(COMPANY_DOMAIN)"
        session.authType = .xoAuth2
        session.connectionType = .TLS
        session.oAuth2Token = accessToken

        let fetchFolderOperation = session.fetchAllFoldersOperation()
        fetchFolderOperation?.start({ (error, folders) in
            //handleError

            if let folders = folders, !folders.isEmpty {
                print(folders)
            }
        })
    }

我的实现使我能够成功进行身份验证,即印有accessToken。但是,当它尝试获取文件夹时,它会抛出:

  

无法建立与服务器的稳定连接。

问题是,可以通过以下两个步骤解决此问题:

  1. 允许通过“ Google帐户”页面进行安全性较低的应用访问
  2. 遵循此解决方案here

但是,我认为这些方法并不是真正安全的方法。当我将电子邮件连接到其他电子邮件客户端(例如Outlook等)时,我不记得需要执行任何这些操作,因此我不认为上述是真正的解决方案。

有人可以建议吗?我的代码有问题吗,还是我真的不得不采取上述两个步骤?

0 个答案:

没有答案