Skip to content

Commit

Permalink
roughly supported xAuth.
Browse files Browse the repository at this point in the history
  • Loading branch information
mootoh committed Sep 9, 2010
1 parent 7a2966f commit c447591
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 72 deletions.
4 changes: 4 additions & 0 deletions Private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//update these:
#define CONSUMER_KEY @""
#define CONSUMER_SECRET @""

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ unpack the Growl SDK and put *Growl.framework* into QSTweet/Frameworks dir.

### Build

set your secret info into TwitterPluginAction.m like this:
set your secret info into Private.h like this:

#define CONSUMER_KEY @"abc"
#define CONSUMER_SECRET @"def"
5 changes: 4 additions & 1 deletion TwitterPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
// License: revised BSD
//
#import <QSCore/QSObject.h>
#import "TwitterXAuth.h"

@class TwitterPluginGrowlHelper;

@interface TwitterPlugin : NSObject
{
}

@end
+ (TwitterXAuth *) theTwitterXAuth;
+ (void) didFinishAuthorize;

@end
77 changes: 74 additions & 3 deletions TwitterPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,91 @@
//
#import "TwitterPlugin.h"
#import "TwitterPluginGrowlHelper.h"
#import "TwitterXAuth.h"
#import "Private.h"

TwitterPluginGrowlHelper *growlHelper;
@interface XAuthDelegate : NSObject <TwitterXAuthDelegate>
@end

@implementation XAuthDelegate

- (void) twitterXAuthDidAuthorize:(TwitterXAuth *)txAuth
{
NSLog(@"authorization successful");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"TwitterPlugin.authorized"];

[TwitterPlugin didFinishAuthorize];
}

- (void) twitterXAuthDidTweet:(TwitterXAuth *)twitterXAuth;
{
NSLog(@"successfully tweeted");
}

@end

#pragma mark -

TwitterPluginGrowlHelper *s_growlHelper;
TwitterXAuth *s_twitterXAuth;
BOOL s_authorizing;
XAuthDelegate *s_xauthDelegate;

@implementation TwitterPlugin

+ (void) initializeXAuth
{
NSLog(@"initializeXAuth");
s_twitterXAuth = [[TwitterXAuth alloc] init];
s_xauthDelegate = [[XAuthDelegate alloc] init];
s_twitterXAuth.delegate = s_xauthDelegate;

s_twitterXAuth.consumerKey = CONSUMER_KEY;
s_twitterXAuth.consumerSecret = CONSUMER_SECRET;

id values = [[NSUserDefaultsController sharedUserDefaultsController] values];
NSString *screenName = [values valueForKey:@"TwitterPreference.screenName"];
NSString *password = [values valueForKey:@"TwitterPreference.password"];

s_twitterXAuth.username = screenName;
s_twitterXAuth.password = password;
}

+ (void) authorize
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:@"TwitterPlugin.authorized"]) {
s_authorizing = YES;
NSLog(@"go auth!");
[s_twitterXAuth authorize];
}
}

+ (void) load
{
growlHelper = [[TwitterPluginGrowlHelper alloc] init];
s_growlHelper = [[TwitterPluginGrowlHelper alloc] init];
[TwitterPlugin initializeXAuth];
[TwitterPlugin authorize];
}

- (void) dealloc
{
[growlHelper release];
NSLog(@"dealloc TwitterPlugin");
[s_twitterXAuth release];
[s_growlHelper release];
[super dealloc];
}

+ (void) didFinishAuthorize
{
NSLog(@"didFinishAuthorize");
s_authorizing = NO;
}

+ (TwitterXAuth *) theTwitterXAuth
{
NSLog(@"twitterXAuth = %@", s_twitterXAuth);
return s_twitterXAuth;
}
@end
2 changes: 2 additions & 0 deletions TwitterPlugin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
924D847112370728001334E1 /* NSData+Base64.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+Base64.m"; sourceTree = "<group>"; };
924D847212370728001334E1 /* TwitterXAuth.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TwitterXAuth.h; sourceTree = "<group>"; };
924D847312370728001334E1 /* TwitterXAuth.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TwitterXAuth.m; sourceTree = "<group>"; };
925E548A1238611B0081DF9A /* Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Private.h; path = ../Private.h; sourceTree = "<group>"; };
92ED6BCD10722BA9006DE47B /* Growl.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; name = Growl.framework; path = Frameworks/Growl.framework; sourceTree = "<group>"; };
92ED6CC11072521E006DE47B /* Growl Registration Ticket.growlRegDict */ = {isa = PBXFileReference; explicitFileType = text.plist; fileEncoding = 4; path = "Growl Registration Ticket.growlRegDict"; sourceTree = "<group>"; };
92ED6CC5107253C1006DE47B /* TwitterPluginGrowlHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TwitterPluginGrowlHelper.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -160,6 +161,7 @@
924D847112370728001334E1 /* NSData+Base64.m */,
924D847212370728001334E1 /* TwitterXAuth.h */,
924D847312370728001334E1 /* TwitterXAuth.m */,
925E548A1238611B0081DF9A /* Private.h */,
);
path = xAuth;
sourceTree = "<group>";
Expand Down
5 changes: 1 addition & 4 deletions TwitterPluginAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
#import <QSCore/QSObject.h>
#import <QSCore/QSActionProvider.h>
#import "TwitterPluginAction.h"
#import "TwitterXAuth.h"

#define kTwitterPluginAction @"TwitterPluginAction"

@interface TwitterPluginAction : QSActionProvider <TwitterXAuthDelegate>
@interface TwitterPluginAction : QSActionProvider
{
TwitterXAuth *twitterXAuth;
BOOL authorized;
}
@end

86 changes: 23 additions & 63 deletions TwitterPluginAction.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,32 @@
// Created by mootoh on 2007.12.12
// License: revised BSD
//
#import "TwitterPlugin.h"
#import "TwitterPluginAction.h"
#import "TwitterPluginShared.h"
#import <QSCore/QSTextProxy.h>
#import <Growl/Growl.h>
#import "TwitterXAuth.h"

//update these:
#define CONSUMER_KEY @""
#define CONSUMER_SECRET @""
#define TWITTER_USERNAME @""
#define TWITTER_PASSWORD @""

@implementation TwitterPluginAction

- (void) initializeXAuth
{
twitterXAuth = [[TwitterXAuth alloc] init];

twitterXAuth.consumerKey = CONSUMER_KEY;
twitterXAuth.consumerSecret = CONSUMER_SECRET;
twitterXAuth.username = TWITTER_USERNAME;
twitterXAuth.password = TWITTER_PASSWORD;
twitterXAuth.delegate = self;

[twitterXAuth authorize];
}

- (void) twitterXAuthDidAuthorize:(TwitterXAuth *)txAuth
{
NSLog(@"authorization successful");
NSLog(@"sending tweet");
[txAuth tweet:@"test tweet"];
}

- (void) twitterXAuthDidTweet:(TwitterXAuth *)twitterXAuth;
{
NSLog(@"successfully tweeted");
exit(0);
}

- (QSObject *) post:(QSObject *)dObject to:(QSObject *)ind
{
[self initializeXAuth];

/*
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:@"TwitterPlugin.authorized"]) {
NSError *err = nil;
[GrowlApplicationBridge
notifyWithTitle:@"QSTweet"
description:err ? [err localizedDescription] : @"not logged in yet"
notificationName:@"NotAuthorized"
iconData:nil
priority:0
isSticky:NO
clickContext:nil];
return dObject;
}
*/
NSString *optional = @"";
if (ind) {
NSString *fr = [ind stringValue];
Expand All @@ -59,37 +41,15 @@ - (QSObject *) post:(QSObject *)dObject to:(QSObject *)ind

// construct request body
NSString *content = [dObject stringValue];
content = [content stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
content = [content stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
content = [content stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
content = [NSString stringWithFormat:@"source=QSTweet&status=%@%@", optional, content];

// get screenName/password from PreferencePane
id values = [[NSUserDefaultsController sharedUserDefaultsController] values];
NSString *screenName = [values valueForKey:@"TwitterPreference.screenName"];
NSString *password = [values valueForKey:@"TwitterPreference.password"];

// construct request
NSString *urlString = [NSString stringWithFormat:
@"http://%@:%@@twitter.com/statuses/update.json", screenName, password];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[content dataUsingEncoding:NSUTF8StringEncoding]];


NSURLResponse *response;
NSError *err = nil;
// connect it
NSData *ret = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&err];
// content = [content stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// content = [content stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
// content = [content stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
// content = [content stringByAppendingString:optional];

#if 0
NSString *result = [[[NSString alloc] initWithData:ret encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"NSURLConnection result = %@", result);
#endif // 0
[[TwitterPlugin theTwitterXAuth] tweet:content];

NSError *err = nil;
[GrowlApplicationBridge
notifyWithTitle:@"QSTwitter"
description:err ? [err localizedDescription] : @"tweeted ♪"
Expand Down

0 comments on commit c447591

Please sign in to comment.