在 iPhone 中使用 facebook 进行 XMPP 身份验证

Posted

技术标签:

【中文标题】在 iPhone 中使用 facebook 进行 XMPP 身份验证【英文标题】:XMPP authentication with facebook in iPhone 【发布时间】:2013-07-05 04:20:47 【问题描述】:

我在 iPhone 中使用xmpp framework 创建了一个用于 facbook 聊天的示例应用程序,它运行良好,但是当我将它集成到主项目中时,它显示以下错误服务器不支持 X-FACEBOOK-PLATFORM 身份验证下面是我的代码

- (void)xmppStreamDidConnect:(XMPPStream *)sender

    NSError *error;
    if (![xmppStream isSecure])
     
        NSError *error = nil;
        BOOL result = [xmppStream secureConnection:&error];
        if (result == NO)
        

        
    
    else
    
        [xmppStream authenticateWithFacebookAccessToken:FBSession.activeSession.accessTokenData.accessToken error:&error];
        NSLog(@"%@",error);          
    

【问题讨论】:

【参考方案1】:

您需要遵循以下步骤:

首先,使用应用 ID 创建 Facebook 流:

- (void)setupStreamFacebook


    //NSAssert(xmppStream == nil, @"Method setupStream invoked multiple times");

    // Setup xmpp stream
    //
    // The XMPPStream is the base class for all activity.
    // Everything else plugs into the xmppStream, such as modules/extensions and delegates.
    NSAssert(nil != self.facebookAppId, @"Please setup facebook app id");
    NSAssert(nil != self.facebookToken, @"Please pass facebook access token");

    xmppStreamFacebook = [[XMPPStream alloc] initWithFacebookAppId:self.facebookAppId];



#if !TARGET_IPHONE_SIMULATOR
           
        xmppStream.enableBackgroundingOnSocket = YES;
    
#endif

    // Setup reconnect
    xmppReconnectFacebook = [[XMPPReconnect alloc] init];

    // Setup roster 

        xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] initWithInMemoryStore];

    if (!xmppRosterFacebook) 
        xmppRosterFacebook = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterStorage];
    

    xmppRosterFacebook.autoFetchRoster = YES;
    xmppRosterFacebook.autoAcceptKnownPresenceSubscriptionRequests = YES;

    // Setup vCard support

    if (!xmppvCardTempModule) 
        xmppvCardTempModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:xmppvCardStorage];
    

    if (!xmppvCardAvatarModule) 
        xmppvCardAvatarModule = [[XMPPvCardAvatarModule alloc] initWithvCardTempModule:xmppvCardTempModule];
    


    if (!xmppCapabilities) 
        xmppCapabilities = [[XMPPCapabilities alloc] initWithCapabilitiesStorage:xmppCapabilitiesStorage];
    


    xmppCapabilities.autoFetchHashedCapabilities = YES;
    xmppCapabilities.autoFetchNonHashedCapabilities = NO;

    // Activate xmpp modules

    [xmppReconnectFacebook         activate:xmppStreamFacebook];
    [xmppRosterFacebook            activate:xmppStreamFacebook];
    [xmppvCardTempModule   activate:xmppStreamFacebook];
    [xmppvCardAvatarModule activate:xmppStreamFacebook];
    [xmppCapabilities      activate:xmppStreamFacebook];

    // Add ourself as a delegate to anything we may be interested in

    [xmppStreamFacebook addDelegate:self delegateQueue:dispatch_get_main_queue()];
    [xmppRosterFacebook addDelegate:self delegateQueue:dispatch_get_main_queue()];


    // You may need to alter these settings depending on the server you're connecting to
    allowSelfSignedCertificates = NO;
    allowSSLHostNameMismatch = NO;

    NSError *fbError = nil;


然后,生成 Facebook 访问令牌并将其传递给以下方法:

[xmppStreamFacebook authenticateWithFacebookAccessToken:self.facebookToken error:&fbError];
// NSLog(@"facebook erro = %@",error);
[xmppStreamFacebook connectWithTimeout:5 error:&fbError];

更新以下代表:

- (void)xmppStreamDidConnect:(XMPPStream *)sender

    NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
    NSLog(@"Stream Host: %@", sender.hostName);
    if ([sender isEqual:xmppStreamFacebook])
    
        if (![xmppStreamFacebook isSecure])
        
            NSLog(@"       if (![xmppStreamFacebook isSecure])");
            NSError *error = nil;
            BOOL result = [xmppStreamFacebook secureConnection:&error];
            
            if (result == NO)
            
                NSLog(@"%@: Error in xmpp STARTTLS: %@", THIS_FILE, error);
            
        
        else
        
            NSLog(@"authenticateWithFacebookAccessToken");
            NSError *error = nil;
            BOOL result = [xmppStreamFacebook authenticateWithFacebookAccessToken:self.facebookToken error:&error];
            
            if (result == NO)
            
                NSLog(@"%@: Error in xmpp auth: %@", THIS_FILE, error);
            
        
        
    


- (void)xmppStream:(XMPPStream *)sender willSecureWithSettings:(NSMutableDictionary *)settings

    NSLog(@"%@: %@", THIS_FILE, THIS_METHOD);
    if ([sender isEqual:xmppStreamFacebook])
    
        NSLog(@"do not secure facebook");
        return;
    

它会正常工作的。

【讨论】:

回答太晚了我已经实施了这个并在 4 个月前完成了这个项目,但谢谢 @Satendra dagar

以上是关于在 iPhone 中使用 facebook 进行 XMPP 身份验证的主要内容,如果未能解决你的问题,请参考以下文章

通过 Facebook iOS Sdk iphone 以编程方式对新闻提要进行喜欢/评论

使用 xmppframework 在 iphone 上进行 facebook 聊天

iPhone应用程序中的Facebook SDK登录

Facebook iPhone 应用程序中某些用户的 XMPPAuthentication 失败

如何生成 iPhone 模拟器构建或 .zip 文件以在 Facebook 中提交以在 iOS 中进行审核

iPhone / iOS Facebook SDK - 您可以在应用内登录并保留登录凭据吗?