First get login and Save Access token and Secret Key for Tumblr API in iOS
I got reference from:http://kashiftriffort.wordpress.com/2012/12/31/how-to-find-access-token-and-secret-key-for-tumblr-api-in-ios/
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”
#import “AppDelegate.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
delegate.OAuthKeyValue = [OAuthKey copy];
delegate.OAuthSecretValue = [OAuthSecret copy];
}
– (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) In AppDelegate files.
AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>{
NSString *OAuthKeyValue; // HERE YOU WILL GET ACCESS TOKEN
NSString *OAuthSecretValue; //HERE YOU WILL GET SECRET TOKEN
}
@property (nonatomic, copy) NSString *OAuthKeyValue;
@property (nonatomic, copy) NSString *OAuthSecretValue;
AppDelegate.m
@implementation AppDelegate
@synthesize OAuthKeyValue, OAuthSecretValue;
– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
OAuthKeyValue = [[[NSString alloc]init]autorelease];
OAuthSecretValue = [[[NSString alloc]init]autorelease];
}
7) 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
#import “TumblrConnect.h”
#import “AppDelegate.h”
TumblrConnect *tumConect = [[TumblrConnect alloc] init];
[tumConect connectTumblr];
Upload Images On Tumblr
1) Make TumblrUplodr Group in your project and add all TumblrUploder files in that group.
2) Download TumblrUploder code.
– Add your tumblrConsumerKey and tumblrConsumerSecret in TumblrUploadr.m File
3) In ViewController.h
#import “TumblrUploadr.h”
@interface ViewController : UIViewController<TumblrUploadrDelegate>
– (IBAction)uploadImage;
– (void) uploadFiles;
4) In ViewController.m file
– (IBAction)uploadImage {
[self uploadFiles];
}
#pragma mark – Tumblr Image Uploder lifecycle
– (void) uploadFiles {
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
NSString *str = [NSString stringWithFormat:@”oauth_token = %@ \n oauth_verifier = %@”,delegate.OAuthKeyValue, delegate.OAuthSecretValue];
NSLog(@”%@”,str);
NSData *data1 = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@”picture01″ ofType:@”jpg”]];
NSData *data2 = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@”picture02″ ofType:@”jpg”]];
NSArray *array = [NSArray arrayWithObjects:data1, data2, nil];
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
TumblrUploadr *tu = [[TumblrUploadr alloc] initWithNSDataForPhotos:array andBlogName:@”<YOUR BLOG NAME>.tumblr.com” andDelegate:self andCaption:@”Great Photos!”];
dispatch_async( dispatch_get_main_queue(), ^{
[tu signAndSendWithTokenKey:delegate.OAuthKeyValue andSecret:delegate.OAuthSecretValue];
});
});
}
– (void) tumblrUploadr:(TumblrUploadr *)tu didFailWithError:(NSError *)error {
NSLog(@”connection failed with error %@”,[error localizedDescription]);
[tu release];
}
– (void) tumblrUploadrDidSucceed:(TumblrUploadr *)tu withResponse:(NSString *)response {
NSLog(@”connection succeeded with response: %@”, response);
[tu release];
}
If there is any issue please let me know, Feedback are always welcome.
Good work, thanks