Created
July 16, 2012 15:41
-
-
Save t-kashima/3123404 to your computer and use it in GitHub Desktop.
OAuth For Tumblr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
#import "OAConsumer.h" | |
#import "OADataFetcher.h" | |
#import "OAMutableURLRequest.h" | |
@interface testViewController : UIViewController { | |
IBOutlet UIButton *btnLogin; | |
IBOutlet UIWebView *oauthWebView; | |
IBOutlet UILabel *lblUser; | |
IBOutlet UILabel *lblAccessToken; | |
IBOutlet UILabel *lblAccessTokenSecret; | |
IBOutlet UITextField *fieldPostContent; | |
IBOutlet UIButton *btnPost; | |
} | |
@property (strong, nonatomic) IBOutlet UIButton *btnLogin; | |
@property (strong, nonatomic) IBOutlet UIWebView *oauthWebView; | |
@property (strong, nonatomic) IBOutlet UILabel *lblUser; | |
@property (strong, nonatomic) IBOutlet UILabel *lblAccessToken; | |
@property (strong, nonatomic) IBOutlet UILabel *lblAccessTokenSecret; | |
@property (strong, nonatomic) IBOutlet UITextField *fieldPostContent; | |
@property (strong, nonatomic) IBOutlet UIButton *btnPost; | |
@property (retain, nonatomic) OAConsumer *consumer; | |
@property (retain, nonatomic) OAToken *accessToken; | |
- (IBAction)btnLoginClicked:(id)sender; | |
- (IBAction)btnPostClicked:(id)sender; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "testViewController.h" | |
@interface testViewController () | |
@end | |
@implementation testViewController | |
@synthesize btnLogin; | |
@synthesize oauthWebView; | |
@synthesize lblUser; | |
@synthesize lblAccessToken; | |
@synthesize lblAccessTokenSecret; | |
@synthesize fieldPostContent; | |
@synthesize btnPost; | |
@synthesize consumer, accessToken; | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
lblUser.text = @""; | |
lblAccessToken.text = @""; | |
lblAccessTokenSecret.text = @""; | |
//最初は認証用のブラウザを表示しない | |
oauthWebView.hidden = YES; | |
} | |
- (void)viewDidUnload | |
{ | |
btnLogin = nil; | |
oauthWebView = nil; | |
lblUser = nil; | |
lblAccessToken = nil; | |
lblAccessTokenSecret = nil; | |
[self setBtnLogin:nil]; | |
[self setOauthWebView:nil]; | |
[self setLblUser:nil]; | |
[self setLblAccessToken:nil]; | |
[self setLblAccessTokenSecret:nil]; | |
[self setBtnPost:nil]; | |
[self setFieldPostContent:nil]; | |
fieldPostContent = nil; | |
btnPost = nil; | |
[super viewDidUnload]; | |
// Release any retained subviews of the main view. | |
} | |
//認証開始 1.Request token(D) | |
-(void)startRequestToken { | |
//CONSUMER_KEYとCONSUMER_SECRETの設定。Twitterのアプリケーション登録時に取得したものをセットしてください。 | |
NSString *CONSUMER_KEY = [NSString stringWithString:@"consumer_key"]; | |
NSString *CONSUMER_SECRET = [NSString stringWithString:@"consumer_secret"]; | |
self.consumer = [[OAConsumer alloc] initWithKey:CONSUMER_KEY | |
secret:CONSUMER_SECRET]; | |
OADataFetcher *fetcher = [[[OADataFetcher alloc] init]autorelease]; | |
NSURL *url = [NSURL URLWithString:@"http://www.tumblr.com/oauth/request_token"]; | |
OAMutableURLRequest *request = [[[OAMutableURLRequest alloc] initWithURL:url | |
consumer:consumer | |
token:nil | |
realm:nil | |
signatureProvider:nil]autorelease]; | |
[request setHTTPMethod:@"POST"]; | |
[fetcher fetchDataWithRequest:request | |
delegate:self | |
didFinishSelector:@selector(requestTokenTicket:didFinishGetRequestToken:) | |
didFailSelector:@selector(requestTokenTicket:didFailSubmitterWithError:)]; | |
} | |
// 1.Request token(D) 正常時 2.Authorize(F) | |
- (void) requestTokenTicket:(OAServiceTicket *)ticket didFinishGetRequestToken:(NSData *)data { | |
if (ticket.didSucceed){ | |
NSString *responseBody = [[[NSString alloc] initWithData:data | |
encoding:NSUTF8StringEncoding]autorelease]; | |
self.accessToken = [[OAToken alloc] initWithHTTPResponseBody:responseBody]; | |
NSString *address = [NSString stringWithFormat: | |
@"http://www.tumblr.com/oauth/authorize?oauth_token=%@", | |
accessToken.key]; | |
NSURL *url = [NSURL URLWithString:address]; | |
//ブラウザ表示 | |
oauthWebView.hidden = NO; | |
[oauthWebView loadRequest:[NSURLRequest requestWithURL:url]]; | |
} else { | |
//エラー処理 | |
} | |
} | |
//エラー時 | |
- (void) requestTokenTicket:(OAServiceTicket *)ticket didFailSubmitterWithError:(NSError *)error { | |
//エラー処理 | |
} | |
// ブラウザから認証された場合 3.Access token(E) | |
- (void) backFromBrowser:(NSURL *)responseURL { | |
//クエリからaccess tokenを取得 | |
NSDictionary *query = [self queryAsDictionary:[responseURL query]]; | |
self.accessToken.verifier = [query objectForKey:@"oauth_verifier"]; | |
OADataFetcher *fetcher = [[[OADataFetcher alloc] init]autorelease]; | |
NSLog(@"secret: %@", accessToken.secret); | |
NSURL *url = [NSURL URLWithString:@"http://www.tumblr.com/oauth/access_token"]; | |
OAMutableURLRequest *request = [[[OAMutableURLRequest alloc] initWithURL:url | |
consumer:consumer | |
token:accessToken | |
realm:nil | |
signatureProvider:nil]autorelease]; | |
[request setHTTPMethod:@"POST"]; | |
[fetcher fetchDataWithRequest:request | |
delegate:self | |
didFinishSelector:@selector(requestTokenTicket:didFinishfetchAccessToken:) | |
didFailSelector:@selector(requestTokenTicket:didFailSubmitterWithError:)]; | |
} | |
- (void) requestTokenTicket:(OAServiceTicket *)ticket didFinishfetchAccessToken:(NSData *)data { | |
NSLog(@"didSucceed: %d", ticket.didSucceed); | |
if (ticket.didSucceed){ | |
NSString *responseBody = [[[NSString alloc] initWithData:data | |
encoding:NSUTF8StringEncoding]autorelease]; | |
self.accessToken = [self.accessToken initWithHTTPResponseBody:responseBody]; | |
//取得できた値を画面に表示する | |
NSLog(@"accessToken: %@", self.accessToken); | |
// lblUser.text = self.accessToken.screen_name; | |
lblAccessToken.text = self.accessToken.key; | |
lblAccessTokenSecret.text = self.accessToken.secret; | |
} else { | |
//エラー処理 | |
} | |
} | |
// UIWebViewの内容がロード完了した時 | |
- (void)webViewDidFinishLoad:(UIWebView *)webView { | |
NSURL* url = [NSURL URLWithString:[webView stringByEvaluatingJavaScriptFromString:@"document.URL"]]; | |
//認証了の場合にコールバックされるURL Twitterのアプリ登録時に設定したURLです。 | |
NSURL *kanryoURL = [NSURL URLWithString:@"http://team-takoyaki.com/"]; | |
//ロードされたURLと、コールバックされるURLのhostが同じなら、承認されたと見なす | |
if ([[url host] isEqualToString:[kanryoURL host]]) { | |
//次の処理へ | |
[self performSelector:@selector(backFromBrowser:) withObject:url]; | |
//webViewを隠す | |
oauthWebView.hidden = YES; | |
} | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
{ | |
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); | |
} | |
- (IBAction)btnLoginClicked:(id)sender { | |
[self performSelector:@selector(startRequestToken)]; | |
} | |
- (IBAction)btnPostClicked:(id)sender { | |
self.accessToken = [[[OAToken alloc] initWithKey:lblAccessToken.text | |
secret:lblAccessTokenSecret.text] autorelease]; | |
NSURL *url = [NSURL URLWithString:@"http://api.tumblr.com/v2/blog/t-kashima.tumblr.com/post"]; | |
OAMutableURLRequest* request = [[[OAMutableURLRequest alloc] initWithURL:url | |
consumer:consumer | |
token:accessToken | |
realm:nil | |
signatureProvider:nil] autorelease]; | |
[request setHTTPMethod:@"POST"]; | |
OARequestParameter *x1 = [[OARequestParameter alloc] initWithName:@"type" value:@"text"]; | |
OARequestParameter *x2 = [[OARequestParameter alloc] initWithName:@"body" value:fieldPostContent.text]; | |
NSArray *params = [NSArray arrayWithObjects:x1, x2, nil]; | |
[request setParameters:params]; | |
OADataFetcher *fetcher = [[[OADataFetcher alloc] init] autorelease]; | |
[fetcher fetchDataWithRequest:request | |
delegate:self | |
didFinishSelector:@selector(ticket:didFinishWithPost:) | |
didFailSelector:@selector(ticket:didFailWithPostError:)]; | |
[x1 release]; | |
[x2 release]; | |
} | |
- (void)ticket:(OAServiceTicket *)ticket didFinishWithPost:(NSData *)data { | |
fieldPostContent.text=@""; | |
//ツイート完了を知らせる | |
} | |
- (void)ticket:(OAServiceTicket *)ticket didFailWithPostError:(NSError *)error { | |
//エラー処理 | |
} | |
- (NSDictionary *)queryAsDictionary: (NSString *)query{ | |
NSArray *components = [query componentsSeparatedByString:@"&"]; | |
NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; | |
for (NSString *component in components) { | |
NSArray *keyAndValues = [component componentsSeparatedByString:@"="]; | |
[parameters setObject:[keyAndValues objectAtIndex:1] forKey:[keyAndValues objectAtIndex:0]]; | |
} | |
return parameters; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment