Permalink
Browse files

新增发推

  • Loading branch information...
1 parent cd9167f commit 1cbe96d7ba4585285d50ab9f615413c4cb3e4fc7 @qdvictory committed Aug 11, 2011
View
Binary file not shown.
View
Binary file not shown.
@@ -15,6 +15,9 @@
#define TXAPPKEY @"527e334faf9c4eef86ad6bb1bb412163" //设置tx appkey
#define TXAPPSECRET @"0f4d7aae73b73e7ee7077d49e2e7140e"
+#define TWITTERAPPKEY @"9CZRoVzLGQerndRrJJWM0w" //设置twitter appkey
+#define TWITTERAPPSECRET @"pdyafzGseNvsBpy9H8i9nUDzUsuQrIMjmGGn7ITAogU"
+
//豆瓣
#define DOUBANRequestURL @"http://www.douban.com/service/auth/request_token" //获取未授权request token
#define DOUBANAuthorizeURL @"http://www.douban.com/service/auth/authorize" //获取授权request token
@@ -30,4 +33,9 @@
#define TXAuthorizeURL @"https://open.t.qq.com/cgi-bin/authorize" //获取授权request token
#define TXAccessURL @"https://open.t.qq.com/cgi-bin/access_token" //获取access token
+//TWITTER
+#define TWITTERRequestURL @"https://api.twitter.com/oauth/request_token" //获取未授权request token
+#define TWITTERAuthorizeURL @"https://api.twitter.com/oauth/authorize" //获取授权request token
+#define TWITTERAccessURL @"https://api.twitter.com/oauth/access_token" //获取access token
+
#define CallBackURL @"oauth://minroad.com" //回调url
@@ -23,18 +23,22 @@
#pragma mark sina
- (void)startSinaOauthWithSelector:(SEL)_sSel withFailedSelector:(SEL)_eSel;
-- (void)startSinaAccessWithVerifier:(NSString *)_ver;
+//- (void)startSinaAccessWithVerifier:(NSString *)_ver;
- (void)sendSinaRecordWithStatus:(NSString *)_status lat:(double)_lat lng:(double)_lng delegate:(id)_delegate successSelector:(SEL)_sSel failSelector:(SEL)_eSel;
- (void)sendSinaPhotoWithStatus:(NSString *)_status lat:(double)_lat lng:(double)_lng path:(NSString *)_path delegate:(id)_delegate successSelector:(SEL)_sSel failSelector:(SEL)_eSel;
#pragma mark douban
- (void)startDoubanOauthWithSelector:(SEL)_sSel withFailedSelector:(SEL)_eSel;
-- (void)startDoubanAccess;
+//- (void)startDoubanAccess;
- (void)sendDoubanShuoWithStatus:(NSString *)_status delegate:(id)_delegate successSelector:(SEL)_sSel failSelector:(SEL)_eSel;
#pragma mark tx
- (void)startTxOauthWithSelector:(SEL)_sSel withFailedSelector:(SEL)_eSel;
-- (void)startTxAccessWithVerifier:(NSString *)_ver;
+//- (void)startTxAccessWithVerifier:(NSString *)_ver;
- (void)sendTxRecordWithStatus:(NSString *)_status lat:(double)_lat lng:(double)_lng format:(NSString *)_format delegate:(id)_delegate successSelector:(SEL)_sSel failSelector:(SEL)_eSel;
- (void)sendTxRecordWithStatus:(NSString *)_status lat:(double)_lat lng:(double)_lng format:(NSString *)_format path:(NSString *)_path delegate:(id)_delegate successSelector:(SEL)_sSel failSelector:(SEL)_eSel;
+
+#pragma mark twitter
+- (void)startTwitterOauthWithSelector:(SEL)_sSel withFailedSelector:(SEL)_eSel;
+- (void)sendTwitterWithStatus:(NSString *)_status lat:(double)_lat lng:(double)_lng delegate:(id)_delegate successSelector:(SEL)_sSel failSelector:(SEL)_eSel;
@end
@@ -196,6 +196,52 @@ - (OAMutableURLRequest *)txRequestWithURL:(NSString *)_url dic:(NSDictionary *)_
}
#pragma mark -
+- (OAMutableURLRequest *)twitterRequestWithURL:(NSString *)_url dic:(NSDictionary *)_dic method:(NSString *)_method
+{
+ OAHMAC_SHA1SignatureProvider *hmacSha1Provider = [[OAHMAC_SHA1SignatureProvider alloc] init];
+ OAConsumer *consumer = [[OAConsumer alloc] initWithKey:TWITTERAPPKEY secret:TWITTERAPPSECRET];
+
+ OAToken *token = nil;
+
+ NSUserDefaults *info = [NSUserDefaults standardUserDefaults];
+ NSString *strAccess = [info valueForKey:@"WBShareKit_twitterToken"];
+
+ if (nil != strAccess) {
+ token = [[[OAToken alloc] initWithHTTPResponseBody:strAccess] autorelease];
+ //NSLog(@"%@,%@",token.secret,token.key);
+ }
+
+ OAMutableURLRequest *hmacSha1Request = [[[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:_url]
+ consumer:consumer
+ token:token
+ realm:NULL
+ signatureProvider:hmacSha1Provider
+ nonce:[self _generateNonce]
+ timestamp:[self _generateTimestamp]] autorelease];
+
+ // OARequestParameter *pa1 = [[OARequestParameter alloc] initWithName:@"x_auth_username" value:strUserName];
+ // OARequestParameter *pa2 = [[OARequestParameter alloc] initWithName:@"x_auth_password" value:strUserPwd];
+ // OARequestParameter *pa3 = [[OARequestParameter alloc] initWithName:@"x_auth_mode" value:@"client_auth"];
+ if (nil != _dic) {
+ for (NSString *key in [_dic allKeys]) {
+ [hmacSha1Request setOAuthParameterName:key withValue:[_dic valueForKey:key]];
+ }
+ }
+
+ // [hmacSha1Request setOAuthParameterName:@"oauth_verifier" withValue:[info valueForKey:@"WBShareKit_ver"]];
+
+ //[hmacSha1Request1 setParameters:[NSArray arrayWithObjects:pa1,pa2,pa3,nil]];
+ if (nil != _method) {
+ [hmacSha1Request setHTTPMethod:_method];
+ }
+
+ [hmacSha1Provider release];
+ [consumer release];
+ //[hmacSha1Request1 release];
+ return hmacSha1Request;
+}
+
+#pragma mark -
#pragma mark sina
- (void)startSinaOauthWithSelector:(SEL)_sSel withFailedSelector:(SEL)_eSel
{
@@ -818,6 +864,148 @@ - (void)sendTxRecordWithStatus:(NSString *)_status lat:(double)_lat lng:(double)
[postbody release];
}
+#pragma mark -
+#pragma mark twitter
+- (void)startTwitterOauthWithSelector:(SEL)_sSel withFailedSelector:(SEL)_eSel
+{
+ NSString *strSSel = NSStringFromSelector(_sSel);
+ NSString *strESel = NSStringFromSelector(_eSel);
+
+ NSUserDefaults *info = [NSUserDefaults standardUserDefaults];
+ [info setValue:strSSel forKey:@"WBShareKit_SSel"];
+ [info setValue:strESel forKey:@"WBShareKit_ESel"];
+ [info synchronize];
+
+ OAConsumer *consumer = [[OAConsumer alloc] initWithKey:TWITTERAPPKEY secret:TWITTERAPPSECRET];
+
+ OAHMAC_SHA1SignatureProvider *hmacSha1Provider = [[OAHMAC_SHA1SignatureProvider alloc] init];
+ OAMutableURLRequest *hmacSha1Request = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:TWITTERRequestURL]
+ consumer:consumer
+ token:NULL
+ realm:NULL
+ signatureProvider:hmacSha1Provider
+ nonce:[self _generateNonce]
+ timestamp:[self _generateTimestamp]];
+ [hmacSha1Request setHTTPMethod:@"GET"];
+ [hmacSha1Request setParameters:[NSArray arrayWithObjects:[[OARequestParameter alloc] initWithName:@"oauth_callback" value:@"oauth://minroad.com"],nil]];
+
+ [hmacSha1Request prepare];
+
+ OAAsynchronousDataFetcher *fetcher = [[OAAsynchronousDataFetcher alloc] initWithRequest:hmacSha1Request delegate:self didFinishSelector:@selector(twitterRequestTokenTicket:finishedWithData:) didFailSelector:@selector(twitterRequestTokenTicket:failedWithError:)];
+ [fetcher start];
+ [fetcher release];
+}
+
+- (void)twitterRequestTokenTicket:(OAServiceTicket *)ticket failedWithError:(NSError *)error {
+ NSLog(@"twitter 获取未授权token失败 错误:%@",error);
+}
+
+
+- (void)twitterRequestTokenTicket:(OAServiceTicket *)ticket finishedWithData:(NSMutableData *)data {
+
+ NSString *responseBody = [[NSString alloc] initWithData:data
+ encoding:NSUTF8StringEncoding];
+ NSLog(@"获得未授权的KEY:%@",responseBody);
+
+ OAToken *token = [[OAToken alloc] initWithHTTPResponseBody:responseBody];
+
+ NSString *tt = [token.key URLEncodedString];
+ NSString *url = [NSString stringWithFormat:@"%@?oauth_token=%@&oauth_callback=%@",TWITTERAuthorizeURL,tt,CallBackURL];
+ [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
+
+ NSUserDefaults *info = [NSUserDefaults standardUserDefaults];
+ [info setValue:responseBody forKey:@"WBShareKit_responseBody"];
+ [info setValue:@"twitter" forKey:@"WBShareKit_type"];
+ [info synchronize];
+}
+
+- (void)startTwitterAccess
+{
+ NSUserDefaults *info = [NSUserDefaults standardUserDefaults];
+ OAConsumer *consumer = [[OAConsumer alloc] initWithKey:TWITTERAPPKEY secret:TWITTERAPPSECRET];
+ OAToken *token = [[OAToken alloc] initWithHTTPResponseBody:[info valueForKey:@"WBShareKit_responseBody"]];
+ OAHMAC_SHA1SignatureProvider *hmacSha1Provider = [[OAHMAC_SHA1SignatureProvider alloc] init];
+
+ OAMutableURLRequest *hmacSha1Request = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",TWITTERAccessURL]]
+ consumer:consumer
+ token:token
+ realm:NULL
+ signatureProvider:hmacSha1Provider
+ nonce:[self _generateNonce]
+ timestamp:[self _generateTimestamp]];
+ [hmacSha1Request setHTTPMethod:@"GET"];
+
+ [hmacSha1Request prepare];
+
+ OAAsynchronousDataFetcher *fetcher = [[OAAsynchronousDataFetcher alloc] initWithRequest:hmacSha1Request delegate:self didFinishSelector:@selector(twitterAccessTokenTicket:finishedWithData:) didFailSelector:@selector(twitterAccessTokenTicket:failedWithError:)];
+ [fetcher start];
+ [fetcher release];
+}
+
+- (void)twitterAccessTokenTicket:(OAServiceTicket *)ticket failedWithError:(NSError *)error {
+ NSLog(@"twitter 获取access token失败 错误:%@",error);
+
+ NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"WBShareKit.delegate"];
+ id delegate = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
+ NSUserDefaults *info = [NSUserDefaults standardUserDefaults];
+ [delegate performSelector:NSSelectorFromString([info valueForKey:@"WBShareKit_ESel"]) withObject:error];
+}
+
+
+- (void)twitterAccessTokenTicket:(OAServiceTicket *)ticket finishedWithData:(NSMutableData *)data {
+ NSString *responseBody = [[[NSString alloc] initWithData:data
+ encoding:NSUTF8StringEncoding] autorelease];
+ NSUserDefaults *info = [NSUserDefaults standardUserDefaults];
+ [info setValue:responseBody forKey:@"WBShareKit_twitterToken"];
+ [info synchronize];
+
+ NSLog(@"获取access token:%@",responseBody);
+
+ NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"WBShareKit.delegate"];
+ id delegate = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
+ [delegate performSelector:NSSelectorFromString([info valueForKey:@"WBShareKit_SSel"]) withObject:data];
+}
+
+#pragma mark -
+- (void)sendTwitterWithStatus:(NSString *)_status lat:(double)_lat lng:(double)_lng delegate:(id)_delegate successSelector:(SEL)_sSel failSelector:(SEL)_eSel
+{
+ _successSEL = _sSel;
+ _failSEL = _eSel;
+
+ NSMutableString *url = [NSMutableString stringWithFormat:@"http://api.twitter.com/1/statuses/update.json",TWITTERAPPKEY];
+ NSMutableString *body = [NSMutableString stringWithString:@""];
+ if ([_status length] != 0) {
+ [body appendFormat:@"status=%@",[[self replaceURLPlus:_status] URLEncodedString]];
+ }
+ if (_lat != 0) {
+ [body appendFormat:@"&lat=%f",_lat];
+ }
+ if (_lng != 0) {
+ [body appendFormat:@"&long=%f",_lng];
+ }
+
+ OAMutableURLRequest *request = [self twitterRequestWithURL:url
+ dic:nil
+ method:@"POST"];
+
+ [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
+
+ int contentLength = [body lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
+
+ [request setValue:[NSString stringWithFormat:@"%d", contentLength] forHTTPHeaderField:@"Content-Length"];
+
+ [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
+
+ [request prepare];
+
+ OAAsynchronousDataFetcher *addRecordFetcher = [[[OAAsynchronousDataFetcher alloc] init] autorelease];
+ [addRecordFetcher initWithRequest:request
+ delegate:_delegate
+ didFinishSelector:_sSel
+ didFailSelector:_eSel];
+ [addRecordFetcher start];
+}
+
#pragma mark app delegate
- (void)handleOpenURL:(NSURL *)url
{
@@ -846,6 +1034,10 @@ - (void)handleOpenURL:(NSURL *)url
[info synchronize];
[self startTxAccessWithVerifier:string];
}
+ else if ([type isEqualToString:@"twitter"])
+ {
+ [self startTwitterAccess];
+ }
}
@@ -18,4 +18,6 @@
- (IBAction)StartTX:(id)sender;
- (IBAction)StartTXWeibo:(id)sender;
- (IBAction)StartTXPhotoWeibo:(id)sender;
+- (IBAction)StartTwitter:(id)sender;
+- (IBAction)StartSendTwitter:(id)sender;
@end
@@ -77,6 +77,15 @@ - (IBAction)StartTXPhotoWeibo:(id)sender {
[[WBShareKit mainShare] sendTxRecordWithStatus:@"WBShareKit Photo test" lat:0 lng:0 format:@"json" path:[[NSBundle mainBundle] pathForResource:@"WBShareKit" ofType:@"png"] delegate:self successSelector:@selector(sendRecordTicket:finishedWithData:) failSelector:@selector(sendRecordTicket:failedWithError:)];
}
+- (IBAction)StartTwitter:(id)sender {
+ [[WBShareKit mainShare] setDelegate:self];
+ [[WBShareKit mainShare] startTwitterOauthWithSelector:@selector(twitterSuccess:) withFailedSelector:@selector(twitterError:)];
+}
+
+- (IBAction)StartSendTwitter:(id)sender {
+ [[WBShareKit mainShare] sendTwitterWithStatus:@"WBShareKit test" lat:0 lng:0 delegate:self successSelector:@selector(sendRecordTicket:finishedWithData:) failSelector:@selector(sendRecordTicket:failedWithError:)];
+}
+
#pragma mark sina delegate
- (void)sinaSuccess:(NSData *)_data
@@ -128,7 +137,6 @@ - (void)sendDoubanShuoTicket:(OAServiceTicket *)ticket failedWithError:(NSError
}
#pragma mark tx delegate
-#pragma mark sina delegate
- (void)txSuccess:(NSData *)_data
{
NSLog(@"tx ok:%@",_data);
@@ -139,4 +147,15 @@ - (void)txError:(NSError *)_error
NSLog(@"tx error:%@",_error);
}
+#pragma mark twitter delegate
+- (void)twitterSuccess:(NSData *)_data
+{
+ NSLog(@"twitter ok:%@",_data);
+}
+
+- (void)twitterError:(NSError *)_error
+{
+ NSLog(@"twitter error:%@",_error);
+}
+
@end
Oops, something went wrong.

0 comments on commit 1cbe96d

Please sign in to comment.