Objective-C
Objective-C
Configure OAuth 1.0a Authentication for REST
See more REST Examples
Demonstrates Rest.SetAuthOAuth1, which associates an OAuth1 provider so Chilkat computes the OAuth 1.0/1.0a signature for each request. The second argument selects whether OAuth parameters are placed in the query string or the Authorization header.
Background. OAuth 1.0a signs each request using the consumer key/secret and the access token/secret. Chilkat generates the nonce, timestamp, and signature automatically for every request sent through the object.
Chilkat Objective-C Downloads
#import <CkoRest.h>
#import <CkoOAuth1.h>
#import <NSString.h>
BOOL success = NO;
CkoRest *rest = [[CkoRest alloc] init];
BOOL bTls = YES;
BOOL bAutoReconnect = YES;
success = [rest Connect: @"example.com" port: [NSNumber numberWithInt: 443] tls: bTls autoReconnect: bAutoReconnect];
if (success == NO) {
NSLog(@"%@",rest.LastErrorText);
return;
}
// Configure OAuth 1.0a request signing. Provide the consumer credentials and the access token
// credentials; Chilkat computes the OAuth signature for each request.
CkoOAuth1 *oauth1 = [[CkoOAuth1 alloc] init];
// Normally you would not hard-code secrets in source. You should instead obtain them from an
// interactive prompt, environment variable, or a secrets vault.
oauth1.ConsumerKey = @"CONSUMER_KEY";
oauth1.ConsumerSecret = @"CONSUMER_SECRET";
oauth1.Token = @"ACCESS_TOKEN";
oauth1.TokenSecret = @"ACCESS_TOKEN_SECRET";
oauth1.SignatureMethod = @"HMAC-SHA1";
// The 2nd argument selects whether OAuth parameters are placed in the query string (true) or the
// Authorization header (false).
BOOL bUseQueryParams = NO;
success = [rest SetAuthOAuth1: oauth1 useQueryParams: bUseQueryParams];
if (success == NO) {
NSLog(@"%@",rest.LastErrorText);
return;
}
NSString *responseText = [rest FullRequestNoBody: @"GET" uriPath: @"/api/resource"];
if (rest.LastMethodSuccess == NO) {
NSLog(@"%@",rest.LastErrorText);
return;
}
NSLog(@"%@",responseText);