Skip to content

Instantly share code, notes, and snippets.

@carlinyuen
Created October 30, 2013 21:11
Show Gist options
  • Save carlinyuen/7240335 to your computer and use it in GitHub Desktop.
Save carlinyuen/7240335 to your computer and use it in GitHub Desktop.
To send an OAuth authenticated message using hardcoded consumer key / secret and access token / secret in iOS -- should be used judiciously, this may be a security risk -- we use the GTMOAuth library (https://code.google.com/p/gtm-oauth/).
#import "GTMOAuthAuthentication.h"
GTMOAuthAuthentication *auth = [[GTMOAuthAuthentication alloc]
initWithSignatureMethod:kGTMOAuthSignatureMethodHMAC_SHA1
consumerKey:@"..."
privateKey:@"..."];
auth.accessToken = @"...";
auth.tokenSecret = @"...";
[auth setHasAccessToken:true];
// Send Request
if ([auth canAuthorize])
{
// Setup request
NSString *message = @"Hello world!";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL URLWithString:[NSString
stringWithFormat:@"https://api.twitter.com/1.1/statuses/update.json?status=%@",
(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL, (CFStringRef)message,
NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8))]]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:5.0];
[request setHTTPMethod:@"POST"];
// Sign & authorize request
if (![auth authorizeRequest:request]) {
NSLog(@"Error: could not authorize request.");
}
else // Authorized and ready to go
{
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@", returnString);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment