【发布时间】:2016-08-16 03:59:31
【问题描述】:
我已经被困了将近一个星期,我想使用他们提供的其余 api 写入 SharePoint 列表。 api 看起来像这样,http://site/_api/lists,从这里我可以根据我附加到我的 url 的内容进行读写,我可以毫无问题地从列表中读取,但是当我必须编写时我会遇到问题。
当我写入列表时,我应该发送 Content-Type、Accept、X-requestDigest 标头和帖子正文。我的代码
NSString *deviceToken = [self getDeviceTokenFromCoreData];
NSString *postData = [NSString stringWithFormat:@"{ \"__metadata\": { \"type\": \"SP.Data.TestListItem\" }, \"Title\": \"Test Title\" }"];
NSData *methodBodyData = [postData dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSData *jsonString = [NSJSONSerialization JSONObjectWithData:methodBodyData options:0 error:&error];
NSString *acceptType = @"application/json;data=verbose";
NSString *requestDigest = _requestDigest;
NSURL *subscribeURL = [[NSURL alloc] initWithString:subscribeUrlString];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL:subscribeURL];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:jsonString];
[theRequest setValue:acceptType forHTTPHeaderField:@"Accept"];
[theRequest setValue:acceptType forHTTPHeaderField:@"Content-Type"];
[theRequest setValue:requestDigest forHTTPHeaderField:@"X-RequestDigest"];
这是我为请求构建标头的地方。这就是我处理请求发送的方式
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];
[operation setWillSendRequestForAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
// if (challenge.previousFailureCount == 0) {
NSLog(@"%@", challenge.protectionSpace);
NSURLCredential *creds = [NSURLCredential credentialWithUser:userName
password:userPass
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:creds forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//Handle Success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//Handle failure
}];
[operation start];
}
这发生在我对 sharepoint 进行身份验证后,我在调试过程中注意到 setWillSendRequestForAuthenticationChallengeBlock 再也不会被调用,看起来我现在需要通过标头发送身份验证信息,这就是我认为请求摘要的用途,但是这无济于事,因为我仍然没有通过。
我从服务器得到的错误信息是
<?xml version="1.0" encoding="utf-8"?>
<m:error
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code>-2130575251, Microsoft.SharePoint.SPException</m:code>
<m:message xml:lang="en-US">The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again.</m:message>
</m:error>
提前致谢:)
【问题讨论】:
-
请求摘要每 30 分钟左右过期一次。您可以通过从任何 POST 调用的响应标头中获取一个新的。
-
您能否发布有关如何获取请求摘要的代码?当我尝试调用 site/_api/contextinfo 时,我得到一个 403 禁止的空正文
标签: ios objective-c rest sharepoint-2013