我正在编写一个可提取Withings数据的iOS应用,并正在使用来自simple-oauth1的代码(我之前已成功使用该代码与Fitbit API进行了交流)。我成功获取了请求 token ,对其进行了身份验证,然后获得了访问 token 。然后,我通过向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中的克里斯蒂安代码):
+ (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

关于ios - Withings API-访问 protected 资源,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19368687/

10-10 20:42