405方法不允许

时间:2012-12-03 16:45:23

标签: objective-c ios box-api

我尝试访问https://api.box.com/2.0/files但我在回复中收到Expected status code in (200-299), got 405(来自AFNetworking)。

在发送请求之前,我从服务器获取了auth_token。

代码

- (void)getFileListing:(NSString*)apiKey
{
    if(apiKey == nil) { apiKey = kBoxNetApiKey; }

    NSDictionary *boxAuth = [[NSUserDefaults standardUserDefaults] objectForKey:kBoxNetUserDefaultsKey];

    if([boxAuth objectForKey:@"auth_token"] != nil) {
        NSURL *url = [NSURL URLWithString:@"https://api.box.com/2.0/files"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"GET"];

        DLog(@"auth_token: %@", [boxAuth objectForKey:@"auth_token"]);
        DLog(@"apiKey: %@", apiKey);

        NSString *auth = [NSString stringWithFormat:@"BoxAuth api_key=%@&auth_token=%@", apiKey, [boxAuth objectForKey:@"auth_token"]];
        [request setValue:auth forHTTPHeaderField:@"Authorization"];

        AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

            DLog(@"JSON: %@", JSON);

        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

            DLog(@"error: %@", error);
            DLog(@"JSON: %@", JSON);

        }];

        [operation start];
    }
}

*的错误

__29-[BoxNetAuth getFileListing:]_block_invoke_081 [Line 75] error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 405" UserInfo=0xa0b8740 {NSLocalizedRecoverySuggestion={"type":"error","status":405,"code":"method_not_allowed","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Method Not Allowed","request_id":"183259878350bcd62a62f1b"}, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest https://api.box.com/2.0/files>, NSErrorFailingURLKey=https://api.box.com/2.0/files, NSLocalizedDescription=Expected status code in (200-299), got 405, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xa6c9840>}

1 个答案:

答案 0 :(得分:5)

您无法在https://api.box.com/2.0/files上执行GET,因为它需要以下资源ID: 获取https://api.box.com/2.0/files/12345

您可以发送到https://api.box.com/2.0/files/content上传新文件,也可以获取https://api.box.com/2.0/folders/0以获取根文件夹