Reputation: 2941
I'm trying to make an app that connects to a web service with OAUth 1, and I'm having some trouble. Here is my method with my get request:
- (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];
}
When I call this method I get the following error:
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."}
The resource I'm trying to get is documented here: https://wiki.fitbit.com/display/API/OAuth+Authentication+in+the+Fitbit+API#OAuthAuthenticationintheFitbitAPI-tokenCredentials.
I havn't used OAuth 1
before, I have never seen this error before, so I'm not sure how to solve it. Any ideas?
Upvotes: 0
Views: 3175
Reputation: 4522
In my case, the solution was to reboot Xcode and the simulator.
The URL did work in a desktop browser and with curl
. Realising this gave a little hint that something might be wrong with the environment.
FWIW, the Xcode version was 6.0 beta, build 6A279r.
Upvotes: 0
Reputation: 2941
I solved it by using Simple Oauth1. Like this:
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];
Upvotes: 1
Reputation: 1066
The problem here is that AFHTTPClient is going out of memory at the end of your method -loadUserProfile
.
You need to refactor this code so that the client is still retained in memory after your request.
One way to do this is to use the singleton pattern as shown in the example code in AFNetworking.
https://github.com/AFNetworking/AFNetworking/blob/master/Example/Classes/AFAppDotNetAPIClient.m#L31
Upvotes: 0