I'm writing an iOS app that pulls Withings data and am using code from simple-oauth1 (which I previously used successfully to talk to the Fitbit API). I successfully obtained a request token, authenticated it, and then obtained an access token. Then I proceeded to make sure the groundwork was set for accessing protected resources by sending an oauthenticated GET request to http://wbsapi.withings.net/once?action=probe I got an error code 0 (everything seems to be working...) However when I try to make any other calls to http://wbsapi.withings.net, I'm confronted with error 250 ("The provided userid and/or Oauth credentials do not match").
Here is what the code looks like:
- (void)getUserInfo
{
NSString *path = @"measure";
NSMutableDictionary *moreParams = [[NSMutableDictionary alloc] init];
[moreParams setValue:@"getmeas" forKey:@"action"];
[moreParams setValue:@"1234567" forKey:@"userid"];
NSURLRequest *preparedRequest = [OAuth1Controller preparedRequestForPath:path
parameters:moreParams
HTTPmethod:@"GET"
oauthToken:self.oauthToken
oauthSecret:self.oauthTokenSecret];
[NSURLConnection sendAsynchronousRequest:preparedRequest
queue:NSOperationQueue.mainQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) NSLog(@"Error in API request: %@", error.localizedDescription);
});
}];
}
This is how the request is being formatted (Christian's code in OAuth1Controller.m):
+ (NSURLRequest *)preparedRequestForPath:(NSString *)path
parameters:(NSDictionary *)queryParameters
HTTPmethod:(NSString *)HTTPmethod
oauthToken:(NSString *)oauth_token
oauthSecret:(NSString *)oauth_token_secret
{
if (!HTTPmethod
|| !oauth_token) return nil;
NSMutableDictionary *allParameters = [self standardOauthParameters].mutableCopy;
allParameters[@"oauth_token"] = oauth_token;
if (queryParameters) {
[allParameters addEntriesFromDictionary:queryParameters];
}
NSString *parametersString = CHQueryStringFromParametersWithEncoding(allParameters, NSUTF8StringEncoding);
NSString *request_url = API_URL;
if (path) request_url = [request_url stringByAppendingString:path];
NSString *oauth_consumer_secret = CONSUMER_SECRET;
NSString *baseString = [HTTPmethod stringByAppendingFormat:@"&%@&%@", request_url.utf8AndURLEncode, parametersString.utf8AndURLEncode];
NSString *secretString = [oauth_consumer_secret.utf8AndURLEncode stringByAppendingFormat:@"&%@", oauth_token_secret.utf8AndURLEncode];
NSString *oauth_signature = [self.class signClearText:baseString withSecret:secretString];
allParameters[@"oauth_signature"] = oauth_signature;
allParameters[@"oauth_signature_method"] = @"HMAC-SHA1";
NSString *queryString;
if (queryParameters) {
queryString = CHQueryStringFromParametersWithEncoding(queryParameters, NSUTF8StringEncoding);
}
if (queryString) {
request_url = [request_url stringByAppendingFormat:@"?%@", queryString];
}
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:request_url]];
request.HTTPMethod = HTTPmethod;
NSMutableArray *parameterPairs = [NSMutableArray array];
[allParameters removeObjectsForKeys:queryParameters.allKeys];
for (NSString *name in allParameters) {
NSString *aPair = [name stringByAppendingFormat:@"=\"%@\"", [allParameters[name] utf8AndURLEncode]];
[parameterPairs addObject:aPair];
}
NSString *oAuthHeader = [@"OAuth " stringByAppendingFormat:@"%@", [parameterPairs componentsJoinedByString:@","]];
[request setValue:oAuthHeader forHTTPHeaderField:@"Authorization"];
return request;
}
I'm sure I have the correct userid (redacted here as "1234567") but I'm not sure why I can't use it to pull data. I have a feeling this is where the code is breaking. Please help.