3

我正在编写一个提取 Withings 数据的 iOS 应用程序,并使用来自 simple-oauth1 的代码(我之前成功地使用它与 Fitbit API 通信)。我成功获得了请求令牌,对其进行了身份验证,然后获得了访问令牌。然后我继续通过向http://wbsapi.withings.net/once?action=probe发送一个经过验证的 GET 请求来确保为访问受保护资源设置了基础, 我收到了一个错误代码 0(一切似乎都在工作.. .) 但是,当我尝试对http://wbsapi.withings.net进行任何其他调用时,我遇到了错误 250(“提供的用户 ID 和/或 Oauth 凭据不匹配”)。

代码如下所示:

- (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);
                           });
                       }];
}

这就是请求的格式(OAuth1Controller.m 中的Christian 代码):

+ (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;
}

我确定我有正确的用户 ID(此处编辑为“1234567”),但我不确定为什么我不能使用它来提取数据。我有一种感觉,这是代码被破坏的地方。请帮忙。

4

1 回答 1

0

在此代码部分中,在创建“queryString”时发送“allParameters”而不是“queryParameters”。此外,在创建 queryString 之前,如果之前添加,请从 allParameters 中删除 @“OAuth-callback”键及其对象。检查此链接,希望如此将工作。 http://integratingwithings.blogspot.in/2014/05/withings-api-declassified-ios.html

于 2014-06-03T08:52:03.070 回答