与GTMOAuth的FatSecret REST KIT问题

时间:2012-09-23 15:02:34

标签: objective-c ios rest oauth restkit

我正在尝试使用FatSecret Rest API和GTMOAuth,但我不知道什么是错的。 有经验可以告诉我我做错了吗?

我是Objective-C的新手,所以请大胆。

- (void)sendRequests4
{
    NSString *consumerKey = @"my consumer key";
    NSString *sharedSecret = @"my shared secret key";

    RKClient *client = [RKClient sharedClient];

    GTMOAuthAuthentication *auth = [[GTMOAuthAuthentication alloc]
                                    initWithSignatureMethod:kGTMOAuthSignatureMethodHMAC_SHA1
                                    consumerKey:client.OAuth1ConsumerKey
                                    privateKey:client.OAuth1ConsumerSecret];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:client.baseURL];

    auth.shouldUseParamsToAuthorize = YES;
    [auth addAuthorizeTokenParamsToRequest:request];
    [auth addAuthorizeTokenHeaderToRequest:request];

    // Perform a simple HTTP GET and call me back with the results
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setValue:consumerKey forKey:@"oauth_consumer_key"];
    [params setValue:kGTMOAuthSignatureMethodHMAC_SHA1 forKey:@"oauth_signature_method"];
    [params setValue:auth.timestamp forKey:@"oauth_timestamp"];
    [params setValue:auth.nonce forKey:@"oauth_nonce"];
    [params setValue:@"1.0" forKey:@"oauth_version"];
    [params setValue:@"??????" forKey:@"oauth_signature"];
    [params setValue:@"foods.get_most_eaten" forKey:@"method"];
    [params setValue:@"??????" forKey:@"oauth_token"];

    NSURL *url = [NSURL URLWithString:@"http://platform.fatsecret.com/rest/server.api"];

    NSString *sigSecret = [NSString stringWithFormat:@"%@&", sharedSecret];

    NSString *sig = [OAHMAC_SHA1SignatureProvider signature:params
                                                     AndURL:url
                                              AndHTTPMethod:@"POST"
                                         AndSignatureSecret:sigSecret];

    NSString *encodedSignature = [sig urlEncodeUsingEncoding:NSUTF8StringEncoding];

    [params setValue:encodedSignature forKey:@"oauth_signature"];

    [client post:@"foods.get_most_eaten" params:params delegate:self];
}

非常感谢!

2 个答案:

答案 0 :(得分:2)

这似乎是两个独立的OAuth 1库,OAuthConsumer和GTM OAuth的随机混合,它们并不打算一起使用。

答案 1 :(得分:1)

OAuth谈判很难开始运作,所以如果您有兴趣,我会开源我们公司的解决方案。

https://github.com/mysterioustrousers/FatSecretKit