2010-08-19
Objective-C向けOAuthライブラリ"OAuthCore"を使ってTwitter APIを叩いてみる
Sabotter for iPhoneのアップデートに向けてTwitterのOAuth認証の部分の実装をどうするか検討していて。
oauth - API needz authorized? - Google Project Hosting にあるライブラリだとなんだか巨大すぎて、もっと軽く扱えるもの無いのかなーとTwitter Libraries | Twitter Developersをみてたらこんなのがあるらしい。
atebits / OAuthCore / overview — Bitbucket
たったの6ファイル! これだけでOAuthのための基本的なことはできるようだ。試しにxAuthでtokenを取得してそれを使ってTweetする、というのを書いてみた。簡単にするため非同期じゃなくHTTPリクエスト処理してます
#import "Hoge.h" #import "OAuthCore.h" #import "OAuth+Additions.h" #define CONSUMER_KEY @"***************************" #define CONSUMER_KEY_SECRET @"***************************" @implementation Hoge + (void)authenticateAndTweet:(NSString *)status username:(NSString *)username password:(NSString *)password { // xAuth NSData *xauth_response = [Hoge request:[NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"] method:@"POST" body:[[NSString stringWithFormat:@"x_auth_username=%@&x_auth_password=%@&x_auth_mode=client_auth", username, password] dataUsingEncoding:NSUTF8StringEncoding] oauth_token:@"" oauth_token_secret:@""]; NSDictionary *dict = [NSURL ab_parseURLQueryString:[[[NSString alloc] initWithData:xauth_response encoding:NSUTF8StringEncoding] autorelease]]; NSString *oauth_token = [dict objectForKey:@"oauth_token"]; NSString *oauth_token_secret = [dict objectForKey:@"oauth_token_secret"]; // xAuthで得たtokenを利用してTweet NSData *tweet_response = [Hoge request:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] method:@"POST" body:[[NSString stringWithFormat:@"status=%@", status] dataUsingEncoding:NSUTF8StringEncoding] oauth_token:oauth_token oauth_token_secret:oauth_token_secret]; NSLog(@"response: %@", [[[NSString alloc] initWithData:tweet_response encoding:NSUTF8StringEncoding] autorelease]); } + (NSData *)request:(NSURL *)url method:(NSString *)method body:(NSData *)body oauth_token:(NSString *)oauth_token oauth_token_secret:(NSString *)oauth_token_secret { NSString *header = OAuthorizationHeader(url, method, body, CONSUMER_KEY, CONSUMER_KEY_SECRET, oauth_token, oauth_token_secret); NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:method]; [request setValue:header forHTTPHeaderField:@"Authorization"]; [request setHTTPBody:body]; NSURLResponse *response = nil; NSError *error = nil; return [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; } @end
ちゃんとTweetできた!(もちろんxAuthはメールで申請済み)
うーん、これはお手軽でいいなぁ。
トラックバック - http://d.hatena.ne.jp/sugyan/20100819/1282156751
リンク元
- 22 http://pipes.yahoo.com/pipes/pipe.info?_id=49361476efd1fe9761d9bbd6c3a05f4d
- 17 http://wiki.15cc.net/index.php?Twitter Botの作り方まとめ
- 12 http://www.google.co.jp/search?hl=ja&lr=lang_ja&tbs=lr:lang_1ja&q=twitter+streaming+api&aq=f&aqi=g10&aql=&oq=&gs_rfai=
- 12 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rls=GGLG,GGLG:2006-17,GGLG:ja&q=iphone+twitter+api
- 10 http://www.google.com/search?hl=ja&lr=lang_ja&ie=UTF-8&oe=UTF-8&q=java+StringBuilder +初期化&num=50
- 8 http://reader.livedoor.com/reader/
- 8 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja-JP-mac:official&q=iphine+navigationBar 高さ 変更&aq=f&aqi=&aql=&oq=&gs_rfai=
- 8 http://www.google.co.jp/search?hl=ja&safe=off&client=firefox-a&hs=o2y&rls=org.mozilla:ja-JP-mac:official&q=terminal.app+meta&aq=f&aqi=&aql=&oq=&gs_rfai=
- 7 http://practical-scheme.net/wiliki/rssmix.cgi
- 7 http://www.google.co.jp/search?sourceid=chrome&ie=UTF-8&q=すぎゃーんメモ+twitter+bot
