将Objective-C与ActiveCollab 3 Beta API集成的问题

时间:2012-07-04 06:29:49

标签: iphone objective-c activecollab

我正在尝试实现Objective C程序以与activeCollab 3 beta接口,并且遇到了一些问题。我可以输入NSLog在浏览器中输出的URL,它可以很好地拉动所有项目的xml,当我尝试通过这个程序访问它时它不想为我工作,它给了我一个HTTP 403错误。我是Objective C的新手,我这样做是为了学习经验,所以有些代码可能是多余的。在此先感谢您的帮助。导入包含在尖括号中,但会导致它隐藏在StackOverflow上,所以我将它放在引号中

#import "Foundation/Foundation.h"

int main (int argc, const char * argv[]) {

    NSString *token = @"my-token"; 
    NSString *path_info = @"projects";
    NSString *url = @"http://my-site/api.php?";

    NSString *post = [[NSString alloc] initWithFormat:@"path_info=%@&auth_api_token=%@",path_info, token];
    NSLog(@"Post: %@", post);

    NSString *newRequest;
    newRequest = [url stringByAppendingString:post];

    NSLog(@"Path: %@", newRequest);

    NSData *postData = [newRequest dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    [request setURL:[NSURL URLWithString:newRequest]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

    NSString *returnData = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"%@", returnData);

    //printf("Print line");
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的请求标头限制且不必要,对于AC API,您需要GET请求而不是POST

int main (int argc, const char * argv[]) 
{

    NSString *requestString = [[NSString alloc] initWithFormat:@"path_info=%@&auth_api_token=%@", path_info, token];
    NSLog(@"Post: %@", requestString);

    NSString *newRequest;
    newRequest = [url stringByAppendingString: requestString];

    NSLog(@"Path: %@", newRequest);

    NSData *postData = [newRequest dataUsingEncoding: NSASCIIStringEncoding allowLossyConversion: YES];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

    NSLog(@"Data: %@", postData);

    [request setURL: [NSURL URLWithString:newRequest]];
    [request setHTTPMethod: @"GET"];

    NSURLResponse *response;

    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &err];
    NSString *returnData = [[[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding] autorelease];
    NSLog(@"Return: %@", returnData);

    //printf("Print line");
    return 0;
}