2013-10-14 159 views
3

我正在編寫一個iOS應用程序,它可以提取Withings數據並使用simple-oauth1中的代碼(我以前曾成功地使用過與Fitbit API交談的代碼)。我成功獲取了一個請求令牌,對其進行了身份驗證,然後獲得了一個訪問令牌。然後,我繼續確保通過向http://wbsapi.withings.net/once?action=probe發送經過驗證的GET請求來設置訪問受保護資源的基礎。我得到了一個錯誤代碼0(一切似乎都在正常工作......) 但是,當我嘗試進行任何其他調用http://wbsapi.withings.net,我遇到了錯誤250(「提供的用戶標識和/或Oauth憑證不匹配」)。Withings API - 訪問受保護的資源

下面是代碼的樣子:

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

我敢肯定,我有正確的用戶標識符(在這裏編輯爲「1234567」),但我不確定爲什麼我不能用它來提取數據。我有一種感覺,這就是代碼打破的地方。請幫忙。

+0

任何機會,你可以發佈你使用simple-oauth1成功驗證Withings的代碼嗎?我可以讓登錄頁面出現,但是當我嘗試輸入我的憑據時,它只是重新加載登錄頁面,而不是將我帶到授權頁面。 – bmueller

回答