FatSecret Platform API, fatsecretplatform
At this stage, the catering APP is developing very fast, especially on campus. Students only need a cell phone to buy the food they want. However, if we want to complete an app independently, we need to have corresponding data support. Here we will introduce a foreign development API, FatSecret Platform API, which contains a lot of food information. Based on this information, we can request our data for independent app development.
1. api address
Http://platform.fatsecret.com/api/Default.aspx? Screen = rapih
2. Authentication
It is important to note that Authentication is difficult and important. Next we will study how to perform Authentication.
Note that you must be signed up as a developer, and agree to our Terms of Service in order to obtain you Consumer Key andShared Secret, which you'll need to send requests to the REST API.
As mentioned in the Api, if you need to use the api, you must first register as a developer and obtainConsumer KeyAndShared Secret,These two items. Now, we can get the following data after registering the website:
With this, we can proceed to the next step. Continue to browse API Authentication
When we see this, we will find that the request api must obtain a signature, and the steps above are provided for us. Okay, let's proceed.
Step 1. Creating a Signature Base String
That is to say, we need to splice the fields and methods in the following order, and we use RFC3986 for transcoding.
POST & http%3A%2F%2Fplatform.fatsecret.com%2Frest%2Fserver.api &a%3Dbar%26%26oauth_consumer_key%3Ddemo%26oauth_nonce%3Dabc%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D12345678%26oauth_version%3D1.0%26z%3Dbar
Step 2. Calculating the Signature value (oauth_signature)
This means that further RFC2104, RFC2045, and RFC3986 transcoding is required.
Step 3. Sending the Request
Send all the parameters used to generate the Signature Base String via the HTTP method specified in the Signature Base String, with the inclusion of the oauth_signature.
That's it! We will hopefully be able to generate the same oauth_signature from our end and confirm that it is indeed you
Okay. You must be confused here. It doesn't matter. We have code to help you understand. The above api steps are translated as follows:
/*** <# Description #> ** @ param url request address * @ param method Request method * @ param body data * @ param _ oAuthConsumerKey applied for key *@ param _ oAuthConsumerSecret applied for Secret * @ param _ oAuthToken temporarily does not use * @ param _ oAuthTokenSecret * @ return <# return value description #> */NSString * OAuthorizationHeader (NSURL * url, NSString * method, NSData * body, NSString * _ oAuthConsumerKey, NSString * _ token, NSString * _ oAuthToken, NSString * _ oAuthTokenSecret) {NSString * _ oAuthNonce = [NSString AB _GUID]; NSString * _ oAuthTimestamp = [NSString stringWithFormat: @ "% d", (int) [[[NSDate date] timeIntervalSince1970]; NSString * _ oAuthSignatureMethod = @ "HMAC-SHA1 "; NSString * _ oAuthVersion = @ "1.0"; optional * oAuthAuthorizationParameters = [dictionary]; [oAuthAuthorizationParameters setObject: _ oAuthNonce forKey: @ "oauth_nonce"]; [oAuthAuthorizationParameters setObject: _ oAuthTimestamp forKey: @ "oauth_timestamp"]; [oAuthAuthorizationParameters setObject: _ oAuthSignatureMethod forKey: @ "signature"]; [kerbersetobject: _ oAuthVersion forKey: @ "oauth_version"]; [oAuthAuthorizationParameters setObject: _ oAuthConsumerKey forKey: @ "signature"]; if (_ oAuthToken) [oAuthAuthorizationParameters setObject: _ oAuthToken forKey: @ "oauth_token"]; // get query and body parameters NSDictionary * additionalQueryParameters = [NSURL AB _parseURLQueryString: [url query]; NSDictionary * additionalBodyParameters = nil; if (body) {NSString * string = [[NSString alloc] initWithData: body encoding: NSUTF8StringEncoding]; if (string) {additionalBodyParameters = [NSURL AB _parseURLQueryString: string];} // combine all parameters NSMutableDictionary * parameters = [oAuthAuthorizationParameters mutableCopy]; if (additionalQueryParameters) [parameters: additionalQueryParameters]; if (parameters) [parameters: parameters]; //-> UTF-8-> RFC3986 NSMutableDictionary * encodedParameters = [NSMutableDictionary dictionary]; for (NSString * key in parameters) {NSString * value = [parameters objectForKey: key]; [encodedParameters setObject: [value pair] forKey: [key pair];} NSArray * sortedKeys = [[encodedParameters allKeys] sortedArrayUsingFunction: SortParameter context :( _ bridge void *) (encodedParameters)]; NSMutableArray * parameterArray = [NSMutableArray array]; for (NSString * key in sortedKeys) {[parameterArray addObject: [NSString stringWithFormat: @ "% @", key, [encodedParameters objectForKey: key];} NSString * normalizedParameterString = [parameterArray componentsJoinedByString: @ "&"]; NSString * normalizedURLString; if ([url port] = nil) {normalizedURLString = [NSString stringWithFormat: @ "% @: // % @", [url scheme], [url host], [url path];} else {normalizedURLString = [NSString stringWithFormat: @ "% @: // % @: % @", [url scheme], [url host], [url port], [url path];} NSString * signatureBaseString = [NSString stringWithFormat: @ "% @ & % @", [method AB _RFC3986EncodedString], [normalizedURLString delimiter], [normalizedParameterString secret]; NSString * key = [NSString stringWithFormat: @ "% @ & % @", [_ oAuthConsumerSecret AB _RFC3986EncodedString], [_ oAuthTokenSecret secret]; NSData * signature = HMAC_SHA1 (signatureBaseString, key); NSString * base64Signature = [signature base64EncodedString]; // parker change: changed to parameters * authorizationHeaderDictionary = [parameters mutableCopy]; [Export setObject: base64Signature forKey: @ "oauth_signature"]; NSMutableArray * Records = [NSMutableArray array]; for (NSString * key in authorizationHeaderDictionary) {NSString * value = [Export objectForKey: key]; // parker change: removed quotes that surrounded each value [authorizationHeaderItems addObject: [NSString stringWithFormat: @ "% %@", [key AB _RFC3986EncodedString], [value AB _RFC3986EncodedString];} // PARKER CHANGE: changed concatentation string from "," to "&" NSString * authorizationHeaderString = [authorizationHeaderItems componentsJoinedByString: @ "&"]; // authorizationHeaderString = [NSString stringWithFormat: @ "OAuth % @", authorizationHeaderString]; return authorizationHeaderString ;}
The usage is as follows:
# Pragma mark-Request Method-(void) connentSign {// set the food ID NSDictionary * params ={ @ "food_id": @ "33690 "}; // set the request parameter and method name [self makeRequestWithMethod: @ "food. get "parameters: params completion: ^ (NSDictionary * data) {}];} // start sending a request-(void) makeRequestWithMethod :( NSString *) method parameters :( NSDictionary *) params completion :( void (^) (NSDictionary * data) completionBlock {NSMutableDictionary * parameters = [params MutableCopy]; [parameters addEntriesFromDictionary: [self defaultParameters]; [parameters addEntriesFromDictionary :@{@ "method": method}]; NSString * queryString = [self queryStringFromDictionary: parameters]; NSData * data = [NSData dataWithBytes: [queryString UTF8String] length: queryString. length]; NSString * authHeader = OAuthorizationHeader ([NSURL URLWithString: FAT_SECRET_API_ENDPOINT], @ "GET ", Data, @ "success", @ "f8fa1d96494046c69159099ab153ea1e", nil, @ ""); [self. manager GET: [FAT_SECRET_API_ENDPOINT stringByAppendingFormat :@"? % @ ", AuthHeader] parameters: nil success: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSLog (@" % @ ", responseObject);} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {}];}-(NSDictionary *) defaultParameters {return @ {@ "format": @ "json "};} -(NSString *) queryStringFromDictionary :( NSDictionary *) dict {NSMutableArray * entries = [@ [] mutableCopy]; for (NSString * key in dict) {NSString * value = [dict objectForKey: key]; [entries addObject: [NSString stringWithFormat: @ "% =%@", key, value];} return [entries componentsJoinedByString: @ "&"];}
Then we can Happy programming!
For more information, clickView Source CodeRun the test in person.
For questions or technical exchanges, please join the official QQ group: (452379712)
Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
The copyright of this article belongs to Yantai Jerry Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the author's consent and provide the original article connection on the article page, otherwise, you are entitled to pursue legal liability.