【问题标题】:405 Method Not Allowed405 方法不允许
【发布时间】:2012-12-03 16:45:22
【问题描述】:

我试图访问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>}

【问题讨论】:

  • 这通常是由GETting 需要POSTed 的资源引起的,反之亦然。尝试将 HTTP 方法更改为 POST
  • 其实这对我不起作用:(我现在已经尝试过了,仍然是 405 Not Allowed
  • 如果 GET 和 POST 都不起作用,我会冒险猜测 PUT,因为它似乎是一个上传网站。
  • 在教程中它是 GET,所以我尝试过这种方式但没有成功。整个想法是从服务器获取数据

标签: objective-c ios box-api


【解决方案1】:

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

您可以 POST 到 https://api.box.com/2.0/files/content 来上传新文件,或者您可以 GET https://api.box.com/2.0/folders/0 来获取根文件夹

【讨论】:

  • 谢谢,祝你有一个美好的白天/夜晚或其他任何事情:)(这里是 10:15):D
猜你喜欢
  • 1970-01-01
  • 2021-12-20
  • 2023-03-23
  • 2011-06-21
  • 2011-04-05
  • 2014-05-23
  • 2011-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多