XMPP 无法获得名册 iOS

XMPP Can't get Rosters iOS

我发送了一个 IQ 请求,但我没有得到正确的 result.The 返回 IQ 是错误的

<error code="403" type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error>

我的代码是:

- (void)queryRoster {
    NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:roster"];
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    XMPPJID *myJID = self.stream.myJID;
    [iq addAttributeWithName:@"from" stringValue:@"admin@127.0.0.1"];
    [iq addAttributeWithName:@"to" stringValue:@"127.0.0.1"];
    [iq addAttributeWithName:@"id" stringValue:@"1993"];
    [iq addAttributeWithName:@"type" stringValue:@"get"];
    [iq addChild:query];
    [self.stream sendElement:iq];
}

我认为可能存在身份验证问题。请再次检查您的配置文件。还有一种可能性,如果您的配置文件正确,那么您需要检查您是否在 Ejabbered 管理主页上提供了正确的访问规则。

转到您的 Ejabbered 管理员主页 -> 使用管理员凭据登录 -> 在左侧菜单中,您会找到“访问规则”选项卡。

您将在下页找到有关访问规则的更多信息。

Access Rules for Ejabbered XMPP

检查完所有细节后尝试使用以下代码:-

- (void)FetchFriends 
{
    NSError *error = [[NSError alloc] init];
    NSXMLElement *query = [[NSXMLElement alloc] initWithXMLString:@"<query xmlns='jabber:iq:roster'/>"error:&error];
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
[iq addAttributeWithName:@"type" stringValue:@"get"];
    [iq addAttributeWithName:@"id" stringValue:@"ID_NAME"];
    [iq addAttributeWithName:@"from" stringValue:@"yourid_name@***.com"]; // Try with name in-place of ip like 127.0.0.1
[iq addChild:query];
    [xmppStream sendElement:iq];
}

并在委托方法中检查服务器响应

- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
    NSXMLElement *queryElement = [iq elementForName: @"query" xmlns: @"jabber:iq:roster"];
    if (queryElement) 
    {
        NSArray *itemElements = [queryElement elementsForName: @"item"];
        for (int i=0; i<[itemElements count]; i++)
        {
            NSLog(@"Friend: %@",[[itemElements[i] attributeForName:@"jid"]stringValue]);

        }
    }
    return NO;
}

要获得自己的花名册,根据RFC 6121 §2.1.3,您需要向自己的裸 JID 发送一个 iq。在您的代码中,您将 iq 发送到服务器的 JID。

- (void)queryRoster {
    NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:roster"];
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    XMPPJID *myJID = self.stream.myJID;
    // Don't add a "from" attribute, it is not necessary
    [iq addAttributeWithName:@"to" stringValue:@"admin@127.0.0.1"];
    // Probably better:
    // [iq addAttributeWithName:@"to" stringValue:[myJID bare]];
    [iq addAttributeWithName:@"id" stringValue:@"1993"];
    [iq addAttributeWithName:@"type" stringValue:@"get"];
    [iq addChild:query];
    [self.stream sendElement:iq];
}

不允许为其他人检索花名册,因此出现“未授权”错误。