iPhone : Unable to send Tweets using Twitter+OAuth
I am using Twitter+OAuth in my app.
But I am getting following error in my NSLog.
Request E0B47D37-CE8C-406A-867E-2133E2469397 failed with error Error Domain=HTTP Code=401 "The operation couldn’t be completed. (HTTP error 401.)"
Coding : .h file
#import "SA_OAuthTwitterEngin开发者_开发技巧e.h"
#import "SA_OAuthTwitterController.h"
@interface FirstViewController: UIViewController <SA_OAuthTwitterControllerDelegate,SA_OAuthTwitterEngineDelegate>
{
SA_OAuthTwitterEngine *_engine;
}
Coding in : .m file
#define kOAuthConsumerKey @"consumer_key"
#define kOAuthConsumerSecret @"consumer_secrect"
-(void)viewWillAppear:(BOOL)animated
{
if(!_engine)
{
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];
_engine.consumerKey = kOAuthConsumerKey;
_engine.consumerSecret = kOAuthConsumerSecret;
}
}
-(IBAction) buttonPressed
{
if(![_engine isAuthorized])
{
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
if(controller)
{
[self presentModalViewController:controller animated:YES];
}
}
else
{
NSLog(@"twitter msg : %@",twitterMessage);
[_engine sendUpdate:twitterMessage];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Twitter" message:@"Post Successful" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
}
#pragma mark SA_OAuthTwitterEngineDelegate
-(void) storedCachedTwitterOAuthData: (NSString *)data forUsername: (NSString *)username
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"authData"];
[defaults synchronize];
}
-(NSString *) cachedTwitterOAuthDataForUsername: (NSString *)username
{
return [[NSUserDefaults standardUserDefaults] objectForKey:@"authData"];
}
#pragma mark TwitterEngineDelegate
-(void) requestSucceeded: (NSString *) requestIdentifier
{
NSLog(@"Request %@ succeeded", requestIdentifier);
}
-(void)requestFailed: (NSString *) requestIdentifier withError:(NSError *)error
{
NSLog(@"Request %@ failed with error %@",requestIdentifier,error);
}
What could be problem ?
Generating new cosumerKey and secretKey may solve your problem.And sometimes server at twitter may also don't respond so twit may not post
AS far your coding part is concerned everything is fine unless you used your actual consumer key,Even i faced the same problem.I generated different consumerKey and secretKey and dat did it
Did you change the #define statements at the top of your .m file to use your actual consumer key and consumer secret?
精彩评论