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

时间:2013-07-05 04:20:47

标签: iphone xmppframework

我在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 个答案:

答案 0 :(得分:0)

您需要按照以下步骤操作:

首先,使用app 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;
    }
}

它会正常工作。