Hello everyone, I was working one of iOS app in which need to upload some content on tumblr. However believe me it was not usually and easy task, which we do some like in facebook and twitter. First we need to find Tumblr Access token and secret key using OAuth Library. I have tried couple of resources, however none of them was working correctly, Finally after lots of hard work, i found out one solution for getting values. So i decided to share with every developer so others can also take benefit of it. Below are the steps which you need to follow. This a three step tutorial, On Part 1 you will find the access token and secret key, On Part Second you will find user details like number of blogs and On Part Three posting images on tumblr api.
Right now please enjoy the part one, i will update rest of the part very soon.
1) Fire up XCode -> File -> NewProject -> SingleView Application, Give the app name and click on save
2) Drag and drop OAuthConsumer Folder into project
3) Create a new subclass of NSObject, i have named my class TumblrConnect and save the files into you project. Once you save, it will create two files TumblrConnect.h and TumblrConnect.m
4) Click on TumblrConnect.h and add the following lines of code
#import “OAConsumer.h”
#import “OAToken.h”
#import “OAMutableURLRequest.h”
#import “OADataFetcher.h”
static NSString* kMyApplicationConsumerKey = @””; // ADD YOUR TUMBLR CONSUMER KEY
static NSString* kMyApplicationConsumerSecret = @””; // ADD YOUR TUMBLR CONSUMER SECRET KEY
@interface TumblrConnect : NSObject <UIWebViewDelegate> {
OAConsumer* consumer;
OAToken* requestToken;
OAToken* accessToken;
}
-(void)connectTumblr;
5) Now click on TumblrConnect.m and add the following lines of code
-(void)connectTumblr {
consumer = [[OAConsumer alloc] initWithKey:kMyApplicationConsumerKey secret:kMyApplicationConsumerSecret];
NSURL* requestTokenUrl = [NSURL URLWithString:@”http://www.tumblr.com/oauth/request_token”%5D;
OAMutableURLRequest* requestTokenRequest = [[[OAMutableURLRequest alloc] initWithURL:requestTokenUrl
consumer:consumer
token:nil
realm:nil
signatureProvider:nil] autorelease];
OARequestParameter* callbackParam = [[[OARequestParameter alloc] initWithName:@”oauth_callback” value:@”tumblr://authorized”] autorelease];
[requestTokenRequest setHTTPMethod:@”POST”];
[requestTokenRequest setParameters:[NSArray arrayWithObject:callbackParam]];
OADataFetcher* dataFetcher = [[[OADataFetcher alloc] init] autorelease];
[dataFetcher fetchDataWithRequest:requestTokenRequest
delegate:self
didFinishSelector:@selector(didReceiveRequestToken:data:)
didFailSelector:@selector(didFailOAuth:error:)];
}
– (void)didReceiveRequestToken:(OAServiceTicket*)ticket data:(NSData*)data {
NSString* httpBody = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
requestToken = [[OAToken alloc] initWithHTTPResponseBody:httpBody];
NSURL* authorizeUrl = [NSURL URLWithString:@”https://www.tumblr.com/oauth/authorize”%5D;
OAMutableURLRequest* authorizeRequest = [[[OAMutableURLRequest alloc] initWithURL:authorizeUrl
consumer:nil
token:nil
realm:nil
signatureProvider:nil] autorelease];
NSString* oauthToken = requestToken.key;
OARequestParameter* oauthTokenParam = [[[OARequestParameter alloc] initWithName:@”oauth_token” value:oauthToken] autorelease];
[authorizeRequest setParameters:[NSArray arrayWithObject:oauthTokenParam]];
UIWebView* webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
webView.scalesPageToFit = YES;
[[[UIApplication sharedApplication] keyWindow] addSubview:webView];
[webView release];
webView.delegate = self;
[webView loadRequest:authorizeRequest];
}
– (void)didReceiveAccessToken:(OAServiceTicket*)ticket data:(NSData*)data {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString* httpBody = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
accessToken = [[OAToken alloc] initWithHTTPResponseBody:httpBody];
NSString *OAuthKey = accessToken.key; // HERE YOU WILL GET ACCESS TOKEN
NSString *OAuthSecret = accessToken.secret; //HERE YOU WILL GET SECRET TOKEN
}
– (void)didFailOAuth:(OAServiceTicket*)ticket error:(NSError*)error {
// ERROR!
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark UIWebViewDelegate
– (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] scheme] isEqualToString:@”tumblr”]) {
// Extract oauth_verifier from URL query
NSString* verifier = nil;
NSArray* urlParams = [[[request URL] query] componentsSeparatedByString:@”&”];
for (NSString* param in urlParams) {
NSArray* keyValue = [param componentsSeparatedByString:@”=”];
NSString* key = [keyValue objectAtIndex:0];
if ([key isEqualToString:@”oauth_verifier”]) {
verifier = [keyValue objectAtIndex:1];
break;
}
}
if (verifier) {
NSURL* accessTokenUrl = [NSURL URLWithString:@”https://www.tumblr.com/oauth/access_token”%5D;
OAMutableURLRequest* accessTokenRequest = [[[OAMutableURLRequest alloc] initWithURL:accessTokenUrl
consumer:consumer
token:requestToken
realm:nil
signatureProvider:nil] autorelease];
OARequestParameter* verifierParam = [[[OARequestParameter alloc] initWithName:@”oauth_verifier” value:verifier] autorelease];
[accessTokenRequest setHTTPMethod:@”POST”];
[accessTokenRequest setParameters:[NSArray arrayWithObject:verifierParam]];
OADataFetcher* dataFetcher = [[[OADataFetcher alloc] init] autorelease];
[dataFetcher fetchDataWithRequest:accessTokenRequest
delegate:self
didFinishSelector:@selector(didReceiveAccessToken:data:)
didFailSelector:@selector(didFailOAuth:error:)];
} else {
// ERROR!
}
[webView removeFromSuperview]
return NO;
}
return YES;
}
– (void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error {
// ERROR!
}
6) Now we just need to call the above function, so click on ViewController.m file and please add the following lines of code in ViewDidLoad method
TumblrConnect *tumConect = [[TumblrConnect alloc] init];
[tumConect connectTumblr];
Thats the only thing we need to do and you will get the values, you can use that values for posting images or text in Tumblr. If there is any issue please let me know, Feedback are always welcome. I will also share the source code very soon.
Cheers, Hope you will enjoy this.
This is a great tutorial,I was searching for this for the last 2 months.
Appreciating your helping mentality.
I have one doubt and a question
I can print the array “url params”-it give two values “oauth_token=0ZHXzW6wSys527JrKhI9huk8aFsfgu7aSppydstoCeTRIfvLJu”,
“oauth_verifier=A9iotW86O4ldk1YwsYPDrEOhBMA4aS36gXpqpmruitSnDjxboX”
Are the enough to post in our account.
And how to clear the access token while logout ??
This is a great tutorial and everything works pretty fine…but that was jus the login part…
but i still don’t get how to clear the access token while logout…please reply as soon as possible…
I have not used logout functionality and clear access token values, i need to check on this and will let you know very soon.
hi , this is very good tutorial , i get some issue`s here when i run the program my uiwebview not loading.. plz help me out
Hello what issues are you facing, I have run the code on latest XCode and it didn’t gave any kind of error.
kashifjilani , i dint any error , webview not loading!!! i put some breakpoints but method not called
Thanks for the nice tutorial 🙂
I got two things while printing array “urlParams” – are they enough to post ?
How to check the session is allready opened,and how to clear the access token (just like facebook authentication)
Once you got the values for access token and secret key, you can pass that values using tumblr api to post anything and it will work. I have used both the keys for finding user details as well as post images on user blogs.
When you will login for first time, it will ask for your permission, as soon as you click on Allow in web view, it will retrieve access and secret key values.
When I tried to load this,it crashes and error shows
-[TumblrConnect performSelector:withObject:withObject:]: message sent to deallocated instance 0x13d75030
Please let me know where it is crashing, once you login or when clicking allow for the app
Oh I found that,My view was ARC-I changed it into nonARC,then its worked
when i logged out my tumblr account from safari and go from app after logged out,it asks for the login again.But it is not working for tumblr app in iphone,if i logged out from my app.It doesnt affect in my work-How to make that fine?
(or Can i make that asks for login all the time ??)
I have checked only in iPhone app, not in safari, it only ask for login one time in that and after successful authentication, it only ask for permission.
Yea thats right.But if a user need to login with another Tumblr account,what to do ?
In this case they will need to logout from current app and then another user can login, i have not checked with logout functionality. i need to work on this, very soon i will update it.
Thanks…Thank you so much…..:)Its really a great tutorial.
Pingback: Post Images on Tumblr | iPhone Blog
Hello, I’m using the exact same code you are but I get a error thrown before -[OADataFetcher connectionDidFinishLoading:]. The only thing the error says is this “Thread 1: EXC_BAD_ACCESS (code=1, address=0xc)” I’m not sure if I’m using the wrong OAuthConsumer Library. Would you possibly be able to inform me which one you’re using?
Hello, I am using OAuth Library written by Jon Crosby, and it worked perfectly fine without any crashing. Here is the current link https://code.google.com/p/oauthconsumer/wiki/UsingOAuthConsumer. Please let me know if this works, or will update source code.
What should be the callbakc URI when registering apps for tumblr ?
Hello Ammad,
I have given Default Callback URL as my company name and it worked for me.
i am getting this error “oauth_signature does not match expected value”
where to give oauth_signature and how to give?
hi i don’t get it ! there’s a problem i did everything like mentioned in your interesting article (thanks by the way for the effort) I also put the oauthConsumer and tumblrconnect to noArc … I implemented everything but when i call tumble connect to login i have
[TumblrConnect performSelector:withObject:withObject:] message sent to deallocated i did make some break points but i don’t get where it happens …
(iOS 7 xcode 5.1.1)
THANKS
Well that problem might be of calling object which have been released from memory.
Hi did anyone found solution for log-out and clearing access token which i am badly facing as an issue with Release in 2 days. Please help out.
I have stored Access token in nsuserdefaults when logging out you can clear the cache. When access token will be nil in local then user will be logout by default.