3

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.

Free Mason
  • 107
  • 5
  • Any chance you can post the code you used to successfully authenticate with Withings using simple-oauth1? I'm able to get the login page to appear, but when I try to enter my credentials, it just reloads the login page instead of taking me to the authorization page. – bmueller Feb 04 '14 at 07:55

1 Answers1

0

In this code section send "allParameters" instead of "queryParameters" in the time of creating the "queryString".Also before creating queryString remove @"OAuth-callback" key and its object from allParameters if added previously.check this link,hope it will work. http://integratingwithings.blogspot.in/2014/05/withings-api-declassified-ios.html

iSankha007
  • 385
  • 15
  • 21