Skip to content
Browse files

Add getUserInfo feature for Douban and Add follow-me for Tencent

  • Loading branch information...
1 parent 3126283 commit 569f70d2fbbe87da1276f3de36866077c1aeb945 @kmshi committed May 3, 2012
View
2 Classes/ShareKit/Configuration/DefaultSHKConfigurator.h
@@ -30,7 +30,9 @@
------
To show ShareKit specific debug output in the console, define _SHKDebugShowLogs (uncomment next line).
*/
+#ifdef DEBUG
#define _SHKDebugShowLogs
+#endif
#ifdef _SHKDebugShowLogs
#define SHKDebugShowLogs 1
View
2 Classes/ShareKit/Core/Helpers/SHKXMLResponseParser.h
@@ -11,5 +11,5 @@
@interface SHKXMLResponseParser : NSObject <NSXMLParserDelegate>
+ (NSString *)getValueForElement:(NSString *)element fromResponse:(NSData *)data;
-
++ (NSDictionary*)objectFromXMLResponse:(NSData *)data;
@end
View
15 Classes/ShareKit/Core/Helpers/SHKXMLResponseParser.m
@@ -60,6 +60,21 @@ + (NSString *)getValueForElement:(NSString *)element fromResponse:(NSData *)data
return result;
}
++ (NSDictionary*)objectFromXMLResponse:(NSData *)data{
+ SHKXMLResponseParser *shkParser = [[SHKXMLResponseParser alloc] initWithData:data];
+ [shkParser parse];
+
+ NSDictionary* result;
+ if (shkParser.xmlParsedSuccessfully) {
+ result = shkParser.parsedResponse;
+ } else {
+ result = nil;
+ }
+ [shkParser release];
+
+ return result;
+}
+
- (NSString *)findRecursivelyValueForKey:(NSString *)searchedKey inDict:(NSDictionary *)dictionary {
__block NSString *result = nil;
View
96 Classes/ShareKit/Sharers/Services/Douban/SHKDouban.m
@@ -33,7 +33,7 @@
#import "SHKXMLResponseParser.h"
#import "NSMutableDictionary+NSNullsToEmptyStrings.h"
-//static NSString *const kSHKDoubanUserInfo = @"kSHKDoubanUserInfo";
+static NSString *const kSHKDoubanUserInfo = @"kSHKDoubanUserInfo";
@interface SHKDouban ()
@@ -49,10 +49,9 @@ - (void)sendStatus;
- (void)sendStatusTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data;
- (void)sendStatusTicket:(OAServiceTicket *)ticket didFailWithError:(NSError*)error;
-// TODO: Finish it below
-//- (void)sendUserInfo;
-//- (void)sendUserInfo:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data;
-//- (void)sendUserInfo:(OAServiceTicket *)ticket didFailWithError:(NSError*)error;
+- (void)sendUserInfo;
+- (void)sendUserInfoTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data;
+- (void)sendUserInfoTicket:(OAServiceTicket *)ticket didFailWithError:(NSError*)error;
- (BOOL)shortenURL;
- (void)shortenURLFinished:(SHKRequest *)aRequest;
@@ -100,10 +99,10 @@ + (BOOL)canShareText
return YES;
}
-//+ (BOOL)canGetUserInfo
-//{
-// return YES;
-//}
++ (BOOL)canGetUserInfo
+{
+ return YES;
+}
#pragma mark -
#pragma mark Configuration : Dynamic Enable
@@ -116,11 +115,11 @@ - (BOOL)shouldAutoShare
#pragma mark -
#pragma mark Authorization
-//+ (void)logout
-//{
-// [[NSUserDefaults standardUserDefaults] removeObjectForKey:kSHKDoubanUserInfo];
-// [super logout];
-//}
++ (void)logout
+{
+ [[NSUserDefaults standardUserDefaults] removeObjectForKey:kSHKDoubanUserInfo];
+ [super logout];
+}
#pragma mark -
#pragma mark UI Implementation
@@ -245,10 +244,10 @@ - (BOOL)send
switch (item.shareType)
{
-// case SHKShareTypeUserInfo:
-// [self sendUserInfo];
-// break;
-//
+ case SHKShareTypeUserInfo:
+ [self sendUserInfo];
+ break;
+
default:
[self sendStatus];
break;
@@ -303,6 +302,47 @@ - (void)sendStatusTicket:(OAServiceTicket *)ticket didFailWithError:(NSError*)er
[self sendDidFailWithError:error];
}
+- (void)sendUserInfo{
+ NSString* user_id = [[NSUserDefaults standardUserDefaults] objectForKey:kSHKDoubanUserInfo];
+ SHKLog(@"current user:%@",user_id);
+ if (![user_id isKindOfClass:[NSString class]]) { //when it is a NSDictionary
+ return;
+ }
+
+ OAMutableURLRequest *oRequest = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.douban.com/people/%@",user_id]]
+ consumer:consumer
+ token:accessToken
+ realm:nil
+ signatureProvider:nil];
+
+ [oRequest setHTTPMethod:@"GET"];
+
+ OAAsynchronousDataFetcher *fetcher = [OAAsynchronousDataFetcher asynchronousFetcherWithRequest:oRequest
+ delegate:self
+ didFinishSelector:@selector(sendUserInfoTicket:didFinishWithData:)
+ didFailSelector:@selector(sendUserInfoTicket:didFailWithError:)];
+
+ [fetcher start];
+ [oRequest release];
+}
+
+- (void)sendUserInfoTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data{
+ if (ticket.didSucceed) {
+ [[NSUserDefaults standardUserDefaults] setObject:[SHKXMLResponseParser objectFromXMLResponse:data] forKey:kSHKDoubanUserInfo];
+ SHKLog(@"%@",[SHKXMLResponseParser objectFromXMLResponse:data]);
+ [self sendDidFinish];
+ }
+ else
+ {
+ [self handleUnsuccessfulTicket:data];
+ }
+}
+
+- (void)sendUserInfoTicket:(OAServiceTicket *)ticket didFailWithError:(NSError*)error{
+ [self sendDidFailWithError:error];
+}
+
+
#pragma mark - Overrewrite parent method
- (void)tokenAuthorize
{
@@ -319,6 +359,22 @@ - (void)tokenAuthorize
[auth release];
}
+- (void)tokenAccessTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data {
+ NSString *responseBody = [[NSString alloc] initWithData:data
+ encoding:NSUTF8StringEncoding];
+ NSArray* array = [responseBody componentsSeparatedByString:@"&"];
+ for (NSString* str in array) {
+ NSRange range = [str rangeOfString:@"douban_user_id="];
+ if (range.location != NSNotFound) {
+ [[NSUserDefaults standardUserDefaults] setObject:[str substringFromIndex:(range.location+range.length)] forKey:kSHKDoubanUserInfo];
+ break;
+ }
+ }
+ SHKLog(@"douban_user_id:%@",[[NSUserDefaults standardUserDefaults] objectForKey:kSHKDoubanUserInfo]);
+
+ [super tokenAccessTicket:ticket didFinishWithData:data];
+}
+
#pragma mark -
- (void)handleUnsuccessfulTicket:(NSData *)data
@@ -349,15 +405,15 @@ - (void)handleUnsuccessfulTicket:(NSData *)data
// this is the error message for revoked access ...?... || removed app from Twitter
// TODO:Is it same with Douban?
if ([errorMessage isEqualToString:@"Invalid / used nonce"] || [errorMessage isEqualToString:@"Could not authenticate with OAuth."]) {
-
+ [[self class] logout];
[self shouldReloginWithPendingAction:SHKPendingSend];
return;
} else {
//when sharing image, and the user removed app permissions there is no JSON response expected above, but XML, which we need to parse. 401 is obsolete credentials -> need to relogin
if ([string rangeOfString:@"Signature does not match"].location != NSNotFound) {
-
+ [[self class] logout];
[self shouldReloginWithPendingAction:SHKPendingSend];
return;
}
View
48 Classes/ShareKit/Sharers/Services/Tencent Weibo/SHKTencentWeibo.m
@@ -2,6 +2,8 @@
// SHKTencentWeibo.m
// ShareKit
//
+// As Tencent OAuth is sluggish, you have to pass API_DOMAIN as realm to the OAMutalRequest
+// --It does not use OAuth header but url query, and nonce has 30 length limit.
// Created by kshi on 12-5-1.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
@@ -495,8 +497,7 @@ - (void)sendImageTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)da
NSDictionary* result = [dataString objectFromJSONString];
if ([[result valueForKey:@"ret"] intValue]==0) {
- NSString *urlString = [result valueForKey:@"imgurl"];
- SHKLog(@"imgurl: %@",urlString);
+ SHKLog(@"imgurl: %@",[result valueForKey:@"imgurl"]);
[self sendDidFinish];
}else {
[self handleUnsuccessfulTicket:data];
@@ -546,25 +547,30 @@ - (void)handleUnsuccessfulTicket:(NSData *)data
}
- (void)followMe
-{
- // remove it so in case of other failures this doesn't get hit again
-// [item setCustomValue:nil forKey:@"followMe"];
-//
-// OAMutableURLRequest *oRequest = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/friendships/create/%@.json", API_DOMAIN, SHKCONFIG(sinaWeiboUserID)]]
-// consumer:consumer
-// token:accessToken
-// realm:nil
-// signatureProvider:nil];
-//
-// [oRequest setHTTPMethod:@"POST"];
-//
-// OAAsynchronousDataFetcher *fetcher = [OAAsynchronousDataFetcher asynchronousFetcherWithRequest:oRequest
-// delegate:nil // Currently not doing any error handling here. If it fails, it's probably best not to bug the user to follow you again.
-// didFinishSelector:nil
-// didFailSelector:nil];
-//
-// [fetcher start];
-// [oRequest release];
+{
+ OAMutableURLRequest *oRequest = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://open.t.qq.com/api/friends/add"]
+ consumer:consumer
+ token:accessToken
+ realm:API_DOMAIN
+ signatureProvider:nil];
+
+ [oRequest setHTTPMethod:@"POST"];
+
+ OARequestParameter *formatParam =[[OARequestParameter alloc] initWithName:@"format" value:@"json"];
+ OARequestParameter *nameParam =[[OARequestParameter alloc] initWithName:@"name" value:SHKCONFIG(tencentWeiboUserID)];
+
+ NSArray *params = [NSArray arrayWithObjects:formatParam,nameParam, nil];
+ [oRequest setParameters:params];
+ [formatParam release];
+ [nameParam release];
+
+ OAAsynchronousDataFetcher *fetcher = [OAAsynchronousDataFetcher asynchronousFetcherWithRequest:oRequest
+ delegate:nil // Currently not doing any error handling here. If it fails, it's probably best not to bug the user to follow you again.
+ didFinishSelector:nil
+ didFailSelector:nil];
+
+ [fetcher start];
+ [oRequest release];
}
View
1 ShareKit.xcodeproj/project.pbxproj
@@ -1456,6 +1456,7 @@
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
GCC_C_LANGUAGE_STANDARD = c99;
+ GCC_PREPROCESSOR_DEFINITIONS = DEBUG;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 3.1;

0 comments on commit 569f70d

Please sign in to comment.
Something went wrong with that request. Please try again.