我正在尝试创建一个连接到OAUth 1的Web服务的应用程序,我遇到了一些麻烦。这是我的获取请求的方法:
- (void)loadUserProfile
{
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[baseUrl]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
path:[NSString stringWithFormat:@"/1/user/%@/profile.json", [SSKeychain passwordForService:@"[service]" account:@"encoded_user_id"]]
parameters:nil];
NSString *postString = [NSString stringWithFormat:@"oauth_consumer_key=%@&oauth_token=%@&oauth_nonce=%@&oauth_signature_method=%@&oauth_timestamp=%@&oauth_version=%@", self.auth.consumerKey, [SSKeychain passwordForService:@"[service]" account:@"oauth_token"], self.auth.nonce, self.auth.signatureMethod, self.auth.timestamp, self.auth.version];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Print the response body in text
NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation start];
}
当我调用此方法时,我收到以下错误:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x8efa7f0 {NSErrorFailingURLStringKey=https://api.fitbit.com/1/user/[id]/profile.json, NSErrorFailingURLKey=https://api.fitbit.com/1/user/[id]/profile.json, NSLocalizedDescription=The network connection was lost., NSUnderlyingError=0x8bdb2c0 "The network connection was lost."}
我想要获得的资源在此处记录:https://wiki.fitbit.com/display/API/OAuth+Authentication+in+the+Fitbit+API#OAuthAuthenticationintheFitbitAPI-tokenCredentials。
之前我没有使用过OAuth 1
,我之前从未见过这个错误,所以我不知道如何解决它。有什么想法吗?
答案 0 :(得分:1)
我使用Simple Oauth1解决了这个问题。像这样:
NSString *path = [NSString stringWithFormat:@"/1/user/%@/profile.json", [SSKeychain passwordForService:@"Fitbit" account:@"fitbit_encoded_user_id"]];
NSURLRequest *preparedRequest = [OAuth1Controller preparedRequestForPath:path
parameters:nil
HTTPmethod:@"GET"
oauthToken:[SSKeychain passwordForService:@"Fitbit" account:@"fitbit_oauth_token"]
oauthSecret:[SSKeychain passwordForService:@"Fitbit" account:@"fitbit_oauth_token_secret"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:preparedRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id jsonObject) {
NSLog(@"JSON: %@", jsonObject);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation start];
答案 1 :(得分:0)
问题在于AFHTTPClient在方法-loadUserProfile
结束时内存不足。
您需要重构此代码,以便在请求后客户端仍保留在内存中。
一种方法是使用AFNetworking中示例代码中所示的单例模式。
https://github.com/AFNetworking/AFNetworking/blob/master/Example/Classes/AFAppDotNetAPIClient.m#L31
答案 2 :(得分:0)
就我而言,解决方案是重启Xcode和模拟器。
该网址在桌面浏览器中使用curl
。意识到这有点暗示环境可能有问题。
FWIW,Xcode版本为6.0 beta,版本为6A279r。