我正在编写一个提取 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”),但我不确定为什么我不能使用它来提取数据。我有一种感觉,这就是代码被破坏的地方。请帮忙。
在此代码部分中,在创建“queryString”时发送“allParameters”而不是“queryParameters”。此外,在创建 queryString 之前,请从 allParameters 中删除@“OAuth-callback”键及其对象(如果之前已添加)。请检查此链接,希望它会起作用的。 http://integratingwithings.blogspot.in/2014/05/withings-api-declassified-ios.html