我試圖訪問https://api.box.com/2.0/files
,但我收到我的迴應(來自AFNetworking)Expected status code in (200-299), got 405
。405方法不允許
在發送請求之前,我從服務器獲取了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>}
通常,這是由'GET'ting資源引起的,需要進行POST操作,反之亦然。嘗試將HTTP方法更改爲「POST」。 – Kevin
其實這對我沒有用:(我已經試過了,現在仍然是405不允許 – h4cky
如果GET和POST都不起作用,我會冒着對PUT的猜測,因爲它似乎是一個上傳網站。 –