iOS Sharepoint App登录错误

时间:2012-12-18 19:14:53

标签: ios sharepoint request asihttprequest

我正在开发一个iOS iPad应用程序来连接到Sharepoint。我正在使用ASIHTTPRequest框架执行此操作。我的代码如下所示:

NSURL *url = [NSURL URLWithString:@"https://SHAREPOINTURL"];

NSString *soapMessage = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n"
"<soap12:Body>\n"
"<GetListCollection xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\" />\n"
"</soap12:Body>\n"
"</soap12:Envelope>\n";

asiRequest = [ASIHTTPRequest requestWithURL:url];

[asiRequest setUsername:@"USERNAME"];
[asiRequest setPassword:@"PASSWORD"];

[asiRequest setDelegate:self];
[asiRequest appendPostData:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[asiRequest addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
[asiRequest startAsynchronous];

到目前为止,这么好,但是当我调试它时,我得到403 FORBIDDEN错误。为什么? 在我看来,我不认为错误是用户名和密码。也许请求有问题?

2 个答案:

答案 0 :(得分:0)

由于您没有发布您的网址,我不得不问,您是否正在发帖_vti_bin/authentication.asmx

如果你已经,我只能建议这个,因为我不知道ASI是否正确输入了登录信息。尝试将其手动放入SOAP消息中:(来自here

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <Login xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <username>USERNAME</username>
      <password>PASSWORD</password>
    </Login>
  </soap:Body>
</soap:Envelope>

这应该返回您使用其余Web服务所需的登录cookie。

编辑:还有一件事,您需要将SP服务器设置为使用表单身份验证而不是NTLM身份验证(这是默认设置)。

答案 1 :(得分:0)

查看您的SOAP信封和错误消息我认为您缺少请求中的SOAPAction标题。

下面的代码显示了如何创建获取Sharepoint实例中所有列表的请求的示例。我正在使用AFNetworking,某些部分可能不适用于ASIHTTPRequest框架。

...

NSMutableURLRequest *request = [self requestWithMethod:@"POST"
                                                  path:@"_vti_bin/Lists.asmx"
                                            parameters:nil];

NSString *soapEnvelope = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
  @"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
  @"<soap:Body>"
  @"<GetListCollection xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\" />"
  @"</soap:Body>"
  @"</soap:Envelope>";

// Set headers
[request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"http://schemas.microsoft.com/sharepoint/soap/GetListCollection" forHTTPHeaderField:@"SOAPAction"];

// Content length
NSString *contentLength = [NSString stringWithFormat:@"%d", soapEnvelope.length];
[request setValue:contentLength forHTTPHeaderField:@"Content-Length"];

// Set body
[request setHTTPBody:[soapEnvelope dataUsingEncoding:NSUTF8StringEncoding]];

...

但正如Tim所说,在使用此方法之前,您必须经过身份验证。

有几种身份验证机制,如Tim或NTLM(以及其他一些)所述的Forms声明。如果无法控制服务器的配置,则必须检查Sharepoint实例的配置方式,或者可能支持多种机制。