GTMAppAuth + MailCore:“无法建立到服务器的稳定连接." [英] GTMAppAuth + MailCore: "A stable connection to the server could not be established."

查看:264
本文介绍了GTMAppAuth + MailCore:“无法建立到服务器的稳定连接."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS下使用GMTAuth访问gmail.测试代码如下.可以使用MailCore访问该帐户,该代码本质上类似,但是用户/密码为oAuth.任何线索将不胜感激.

under iOS using GMTAuth to access gmail. The test code follows below. The account is accessible with MailCore, essentially similar code, but user/pw no oAuth. Any clues would be appreciated.

我得到以下日志输出:

__nw_connection_get_connected_socket_block_invoke 3 Connection has no connected handler
2017-02-24 17:20:02.977 Example-iOS[38329:24679819] finished checking account.
2017-02-24 17:20:10.526 Example-iOS[38329:24679819] error loading account: Error Domain=MCOErrorDomain Code=1 "A stable connection to the server could not be established."

这是代码:

self.imapSession = [[MCOIMAPSession alloc] init];
self.imapSession.hostname = @"imap.google.com";
self.imapSession.port = 993;
self.imapSession.username = [_authorization userEmail];
GTMAppAuthFetcherAuthorization* authorization =
[GTMAppAuthFetcherAuthorization authorizationFromKeychainForName:kExampleAuthorizerKey];
self.imapSession.OAuth2Token = authorization.authState.lastTokenResponse.accessToken;
self.imapSession.authType = MCOAuthTypeXOAuth2;
self.imapSession.connectionType = MCOConnectionTypeTLS;
GTMAppAuthExampleViewController * __weak weakSelf = self;
self.imapSession.connectionLogger = ^(void * connectionID, MCOConnectionLogType type, NSData * data) {
  @synchronized(weakSelf) {
    if (type != MCOConnectionLogTypeSentPrivate) {
      NSLog(@"event logged:%p %li withData: %@", connectionID, (long)type, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
      }
    }
  };

// Reset the inbox
self.messages = nil;

NSLog(@"checking account");
self.imapCheckOp = [self.imapSession checkAccountOperation];
[self.imapCheckOp start:^(NSError *error) {
    GTMAppAuthExampleViewController *strongSelf = weakSelf;
    NSLog(@"finished checking account.");
    if (error == nil) {
        // TBD: [strongSelf loadLastNMessages:NUMBER_OF_MESSAGES_TO_LOAD];
    } else {
        NSLog(@"error loading account: %@", error);
    }
    strongSelf.imapCheckOp = nil;
}];
}

推荐答案

更新:我想出了解决方案.这是我完整的工作代码:

Update: I figured out the solution. Here's my full working code:

func loadAccount() {
    // This is just a model file holding the account details...
    let account = emailAccounts[currentAccount]

    imapSession = MCOIMAPSession()
    imapSession?.hostname = account.hostname
    imapSession?.port = 993
    imapSession?.username = account.userName
    imapSession?.authType = .xoAuth2

    // This email helper code is from this guide:
    // https://github.com/MailCore/mailcore2/wiki/Implementing-OAuth-2.0
    imapSession?.oAuth2Token = EmailHelper.singleton().authorization?.authState.lastTokenResponse?.accessToken

    // This is important too (wasn't working with other values)
    imapSession?.connectionType = .TLS

    imapSession?.connectionLogger = { [unowned self] (connectionID, type, data) in
        let lockQueue = DispatchQueue(label: "com.inboxzero.LockQueue")
        lockQueue.sync() {
            if (type != MCOConnectionLogType(rawValue: NSInteger(2))) { // MCOConnectionLogTypeSentPrivate
                if let theData = data, let str = connectionID {
                     let theString = String(describing: theData)
                    print("event logged:\(str) \(type) withData: \(theString)")
                }
            }
        }
    }

    // Reset the inbox:
    messages = nil
    isLoading = false
    print("checking account \(self.imapSession!.username!)")
    imapCheckOp = self.imapSession?.checkAccountOperation()
    if let checkOp = imapCheckOp {
        checkOp.start( { (error) in
            print("finished checking \(self.imapSession!.username!)")
            if (error == nil) {
                self.loadLastNMessages(nMessages: self.NUMBER_OF_MESSAGES_TO_LOAD)
            }
            else {
                print("error loading account \(self.imapSession!.username!): %@", error!)
                self.alertWithTitle(title:"Mail Core", message:error.debugDescription)
                return
            }
        })
    }
}

这篇关于GTMAppAuth + MailCore:“无法建立到服务器的稳定连接."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆