Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After two days of frustration and cursing Twitter, I finally managed to do this. Here is my implementation. The class which will be used to make the request is "BL_TwitterRequest". This is only for obtaining the twitter request token. </p> <p>BL_TwitterRequest.h:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface BL_Request : NSObject &lt;NSURLConnectionDelegate&gt; @property (nonatomic, strong) NSMutableData *webData; -(void) makeRequest; </code></pre> <p>Add the following NSString category at the top of the implementation class (BL_TwitterRequest.m). This will be used to get the URL encoded version of NSString.</p> <pre><code>@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); for (int i = 0; i &lt; sourceLen; ++i) { const unsigned char thisChar = source[i]; if (thisChar == ' '){ [output appendString:@"+"]; } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || (thisChar &gt;= 'a' &amp;&amp; thisChar &lt;= 'z') || (thisChar &gt;= 'A' &amp;&amp; thisChar &lt;= 'Z') || (thisChar &gt;= '0' &amp;&amp; thisChar &lt;= '9')) { [output appendFormat:@"%c", thisChar]; } else { [output appendFormat:@"%%%02X", thisChar]; } } return output; } @end </code></pre> <p>Add the below function in "BL_TwitterRequest.m". It will be used to generate the oAuth nonce. The function basically generates a random string of a specified length.</p> <pre><code>-(NSString*) generateRandomStringOfLength:(int)length { NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; NSMutableString *randomString = [NSMutableString stringWithCapacity: length]; for (int i = 0; i &lt; length; i++) { [randomString appendFormat: @"%C", [letters characterAtIndex: arc4random() % [letters length]]]; } return randomString; } </code></pre> <p>Include the Base64 library from <a href="https://github.com/nicklockwood/Base64" rel="nofollow">here</a>. All you have to do is drag the "Base64.h" and "Base64.m" files into your project. Add the following imports:</p> <pre><code>#include &lt;CommonCrypto/CommonDigest.h&gt; #include &lt;CommonCrypto/CommonHMAC.h&gt; #include &lt;sys/types.h&gt; #include &lt;errno.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #import "Base64.h" </code></pre> <p>Add another function as specified below. This will be used to get the HMAC-SHA1 value. </p> <pre><code>- (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]; return hash; } </code></pre> <p>Now for the main function that will make the request. </p> <pre><code>-(void) makeRequest { _webData = [[NSMutableData alloc]init]; // WILL BE USED BY 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 AND VALUE THAT WILL BE SIGNED AND // APPEND KEY AND VALUE WITH = AND &amp; NSMutableString *parameterString = [[NSMutableString alloc]initWithFormat:@""]; [parameterString appendFormat:@"oauth_callback=%@", [oauthCallback urlencode]]; [parameterString appendFormat:@"&amp;oauth_consumer_key=%@", [oauthConsumerKey urlencode]]; [parameterString appendFormat:@"&amp;oauth_nonce=%@", [oauthNonce urlencode]]; [parameterString appendFormat:@"&amp;oauth_signature_method=%@", [oauthSignatureMethod urlencode]]; [parameterString appendFormat:@"&amp;oauth_timestamp=%@", [oauth_timestamp urlencode]]; [parameterString appendFormat:@"&amp;oauth_version=%@", [oauthVersion urlencode]]; //2. CREATE SIGNATURE STRING WITH HTTP METHOD AND ENCODED BASE URL AND PARAMETER STRING NSString *signatureBaseString = [NSString stringWithFormat:@"%@&amp;%@&amp;%@", httpMethod, [baseURL urlencode], [parameterString urlencode]]; //3. GET THE SIGNING KEY NOW FROM CONSUMER SECRET NSString *signingKey = [NSString stringWithFormat:@"%@&amp;", [oauthConsumerSecret urlencode]]; //4. GET THE OUTPUT OF THE HMAC ALOGRITHM NSString *oauthSignature = [self hmacsha1:signatureBaseString secret:signingKey]; // TIME TO MAKE THE CALL NOW NSMutableString *urlString = [[NSMutableString alloc]initWithFormat:@""]; [urlString appendFormat:@"%@", baseURL]; // INITIALIZE AUTHORIZATION HEADER NSMutableString *authHeader = [[NSMutableString alloc]initWithFormat:@""]; [authHeader appendFormat:@"OAuth "]; // MIND THE 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]; } </code></pre> <p>Now just implement the NSURLConnection delegate methods to get the response.</p> <pre><code>#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); } </code></pre> <p>If everything goes well the 'resultString' will have the oauth_token and oauth_token_secret. </p> <p>To make the call just do the following:</p> <pre><code> BL_TwitterRequest *twitterRequest = [[BL_TwitterRequest alloc]init]; [twitterRequest makeRequest]; </code></pre> <p>Remember any missed spaces or commas can result in an error. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload