我正在编写一个从Yelp请求数据的iOS应用程序。
我目前有代码管理Yelp请求/响应并解析内部返回的JSON数据。当前代码使用Jon Crosby的OAuthConsumer为其请求构建OAuth参数。
我昨天来到RestKit,发现它非常有吸引力。它可能会消除我正在进行的大量请求提交和响应解析。
但是我遇到了障碍,因为我无法弄清楚如何使用RestKit生成Yelp所需的OAuth参数。我在http://www.raywenderlich.com/13097/intro-to-restkit-tutorial处理了Ray Wenderlich RestKit教程,但它使用了客户端ID和客户端密钥(根据Foursquare的要求)。
Yelp请求需要具有使用者密钥,令牌,签名方法,签名,时间戳和随机数。我一直无法找到可以生成这组特定OAuth参数的RestKit插件。
我现在使用由Matt Thompson开发的AFOAuth1Client生成我的RestKit GET请求。现在,当我发送请求时,Yelp API返回了无效签名错误。
我很困惑,因为我检查了HTTP Authorization标题中的字段,看起来是正确的。该错误似乎表明Yelp想要URL中的oauth参数,但API文档说可以在授权标头中发送它们。
以下是我收到的无效签名错误:
2013-08-26 15:34:54.806 RestKitYelpGroupon [2157:400b] E restkit.network:RKObjectRequestOperation.m:575对象请求失败:基础HTTP请求操作失败,错误:错误Domain = org.restkit.RestKit。 ErrorDomain Code = -1011“(200-299)中的预期状态代码,得到400”UserInfo = 0xa876190 {NSLocalizedRecoverySuggestion = {“error”:{“text”:“签名无效”,“id”:“INVALID_SIGNATURE”,“说明“:”无效签名。预期签名基本字符串:GET \ u0026http%3A%2F%2Fapi.yelp.com%2Fv2%2Fsearch \ u0026ll%3D32.893282%252C-117.195083%26oauth_consumer_key%3D(我的消费者密钥)%26oauth_nonce %3DC0F25D91-B473-4059-B5F6-2D850A144A1D%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1377556409%26oauth_token%3D(MY OAUTH TOKEN)%26oauth_version%3D1.0%26term%3Dsushi“}},AFNetworkingOperationFailingURLRequestErrorKey = http:// api .yelp.com / v2 / search?ll = 32.893282%2C-117.195083& term = sushi&gt ;, NSErrorFailingURLKey = http://api.yelp.com/v2/search?ll=32.893282%2C-117.195083&term=sushi,NSLocalizedDescription =预期状态代码(200-299) ,获得400,AFNetworkingOperationFailingURLResponseErrorKey =}
以下是我用来生成请求的代码:
NSURL *baseURL = [NSURL URLWithString:@"http://api.yelp.com/v2"];
AFOAuth1Client *oauth1Client = [[AFOAuth1Client alloc] initWithBaseURL:baseURL key:consumerKey secret:consumerSecret];
oauth1Client.accessToken = [[AFOAuth1Token alloc] initWithKey:tokenKey
secret:tokenSecret
session:nil
expiration:[NSDate distantFuture]
renewable:NO];
[oauth1Client registerHTTPOperationClass:[AFJSONRequestOperation class]];
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[oauth1Client setDefaultHeader:@"Accept" value:@"application/json"];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:oauth1Client];
RKObjectMapping *couponMapping = [RKObjectMapping mappingForClass:[Coupon class]];
[couponMapping addAttributeMappingsFromDictionary:@{@"id" : @"id"}];
RKObjectMapping *businessMapping = [RKObjectMapping mappingForClass:[Business class]];
[businessMapping addAttributeMappingsFromDictionary:@{@"name" : @"name"}];
[businessMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"deals" toKeyPath:@"location" withMapping:couponMapping]];
RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor
responseDescriptorWithMapping:businessMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:@"response.venues"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
[objectManager getObjectsAtPath:@"http://api.yelp.com/v2/search"
parameters:queryParams
success:^(RKObjectRequestOperation * operaton, RKMappingResult *mappingResult)
非常感谢任何进一步的帮助!