0

我正在尝试使用 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];
}

非常感谢!

4

2 回答 2

2

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

于 2012-09-23T17:06:14.630 回答
1

OAuth 协商很难运行,所以如果您有兴趣,我开源了我们公司的解决方案。

https://github.com/mysterioustrousers/FatSecretKit

于 2012-11-29T21:14:47.083 回答