objective c - How to make NSURLRequest to obtain a Twitter request token? -



objective c - How to make NSURLRequest to obtain a Twitter request token? -

i trying obtain request token twitter code:

nsmutableurlrequest *murlrequest = [[nsmutableurlrequest alloc]initwithurl:[nsurl urlwithstring:@"https://api.twitter.com/oauth/request_token"]]; murlrequest.httpmethod = @"post /oauth/request_token http/1.1"; [murlrequest setvalue:@"user-agent" forhttpheaderfield:@"coupled http"]; [murlrequest setvalue:@"oauth oauth_callback=\"http%3a%2f%2fbytolution.com\"" forhttpheaderfield:@"authorization"]; [murlrequest setvalue:@"api.twitter.com" forhttpheaderfield:@"host"]; [murlrequest setvalue:@"accept" forhttpheaderfield:@"*/*"]; nshttpurlresponse *urlresponse; nserror *error; nserror *serializationerror; nsdata *responsedata = [nsurlconnection sendsynchronousrequest:murlrequest returningresponse:&urlresponse error:&error]; nslog(@"data: %@, response:%@, error: %@", [nsjsonserialization jsonobjectwithdata:responsedata options:kniloptions error:&serializationerror], urlresponse.allheaderfields, error);

but is:

data: (null), response:{ connection = close; "content-length" = 0; }, error: (null)

here twitter`s documentation topic.

thanks help!

after 2 days of frustration , cursing twitter, managed this. here implementation. class used create request "bl_twitterrequest". obtaining twitter request token.

bl_twitterrequest.h:

#import <foundation/foundation.h> @interface bl_request : nsobject <nsurlconnectiondelegate> @property (nonatomic, strong) nsmutabledata *webdata; -(void) makerequest;

add next nsstring category @ top of implementation class (bl_twitterrequest.m). used url encoded version of nsstring.

@implementation nsstring (nsstring_extended) - (nsstring *)urlencode { nsmutablestring *output = [nsmutablestring string]; const unsigned char *source = (const unsigned char *)[self utf8string]; int sourcelen = strlen((const char *)source); (int = 0; < sourcelen; ++i) { const unsigned char thischar = source[i]; if (thischar == ' '){ [output appendstring:@"+"]; } else if (thischar == '.' || thischar == '-' || thischar == '_' || thischar == '~' || (thischar >= 'a' && thischar <= 'z') || (thischar >= 'a' && thischar <= 'z') || (thischar >= '0' && thischar <= '9')) { [output appendformat:@"%c", thischar]; } else { [output appendformat:@"%%%02x", thischar]; } } homecoming output; } @end

add below function in "bl_twitterrequest.m". used generate oauth nonce. function generates random string of specified length.

-(nsstring*) generaterandomstringoflength:(int)length { nsstring *letters = @"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"; nsmutablestring *randomstring = [nsmutablestring stringwithcapacity: length]; (int = 0; < length; i++) { [randomstring appendformat: @"%c", [letters characteratindex: arc4random() % [letters length]]]; } homecoming randomstring; }

include base64 library here. have drag "base64.h" , "base64.m" files project. add together next imports:

#include <commoncrypto/commondigest.h> #include <commoncrypto/commonhmac.h> #include <sys/types.h> #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #import "base64.h"

add function specified below. used hmac-sha1 value.

- (nsstring *)hmacsha1:(nsstring *)data secret:(nsstring *)key { const char *ckey = [key cstringusingencoding:nsasciistringencoding]; const char *cdata = [data cstringusingencoding:nsasciistringencoding]; unsigned char chmac[cc_sha1_digest_length]; cchmac(kcchmacalgsha1, ckey, strlen(ckey), cdata, strlen(cdata), chmac); nsdata *hmac = [[nsdata alloc] initwithbytes:chmac length:sizeof(chmac)]; nsstring *hash = [hmac base64encodedstring]; homecoming hash; }

now main function create request.

-(void) makerequest { _webdata = [[nsmutabledata alloc]init]; // used nsurlconnection nsstring *httpmethod = @"post"; nsstring *baseurl = @"https://api.twitter.com/oauth/request_token"; nsstring *oauthconsumerkey = @"your_consumer_key"; nsstring *oauthconsumersecret = @"your_consumer_secret"; nsstring *oauth_timestamp = [nsstring stringwithformat:@"%.f", [[nsdate date]timeintervalsince1970]]; nsstring *oauthnonce = [self generaterandomstringoflength:42]; nsstring *oauthsignaturemethod = @"hmac-sha1"; nsstring *oauthversion = @"1.0"; nsstring *oauthcallback = @"your_twitter_callback_url"; //1. percent code every key , value signed , // append key , value = , & nsmutablestring *parameterstring = [[nsmutablestring alloc]initwithformat:@""]; [parameterstring appendformat:@"oauth_callback=%@", [oauthcallback urlencode]]; [parameterstring appendformat:@"&oauth_consumer_key=%@", [oauthconsumerkey urlencode]]; [parameterstring appendformat:@"&oauth_nonce=%@", [oauthnonce urlencode]]; [parameterstring appendformat:@"&oauth_signature_method=%@", [oauthsignaturemethod urlencode]]; [parameterstring appendformat:@"&oauth_timestamp=%@", [oauth_timestamp urlencode]]; [parameterstring appendformat:@"&oauth_version=%@", [oauthversion urlencode]]; //2. create signature string http method , encoded base of operations url , parameter string nsstring *signaturebasestring = [nsstring stringwithformat:@"%@&%@&%@", httpmethod, [baseurl urlencode], [parameterstring urlencode]]; //3. signing key consumer secret nsstring *signingkey = [nsstring stringwithformat:@"%@&", [oauthconsumersecret urlencode]]; //4. output of hmac alogrithm nsstring *oauthsignature = [self hmacsha1:signaturebasestring secret:signingkey]; // time create phone call nsmutablestring *urlstring = [[nsmutablestring alloc]initwithformat:@""]; [urlstring appendformat:@"%@", baseurl]; // initialize authorization header nsmutablestring *authheader = [[nsmutablestring alloc]initwithformat:@""]; [authheader appendformat:@"oauth "]; // mind space after 'oauth' [authheader appendformat:@"oauth_nonce=\"%@\",", [oauthnonce urlencode]]; [authheader appendformat:@"oauth_callback=\"%@\",", [oauthcallback urlencode]]; [authheader appendformat:@"oauth_signature_method=\"%@\",", [oauthsignaturemethod urlencode]]; [authheader appendformat:@"oauth_timestamp=\"%@\",", [oauth_timestamp urlencode]]; [authheader appendformat:@"oauth_consumer_key=\"%@\",", [oauthconsumerkey urlencode]]; [authheader appendformat:@"oauth_signature=\"%@\",", [oauthsignature urlencode]]; [authheader appendformat:@"oauth_version=\"%@\"", [oauthversion urlencode]]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc]initwithurl:[nsurl urlwithstring:urlstring]] ; [request sethttpmethod:httpmethod]; [request setvalue:authheader forhttpheaderfield:@"authorization"]; nsurlconnection *connection = [[nsurlconnection alloc]initwithrequest:request delegate:self]; [connection start]; }

now implement nsurlconnection delegate methods response.

#pragma mark - connection delegate - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { [_webdata appenddata:data]; } - (void)connectiondidfinishloading:(nsurlconnection *)connection { nsstring *resultstring = [[nsstring alloc]initwithdata:_webdata encoding:nsutf8stringencoding]; nslog(@"result string : %@", resultstring); }

if goes 'resultstring' have oauth_token , oauth_token_secret.

to create phone call following:

bl_twitterrequest *twitterrequest = [[bl_twitterrequest alloc]init]; [twitterrequest makerequest];

remember missed spaces or commas can result in error.

objective-c http twitter nsurlrequest

Comments

Popular posts from this blog

javascript - mongodb won't find my schema method in nested container -

Hibernate criteria by a list of natural ids -

ios - Lagging ScrollView with UIWebview inside -