iPhone:无法使用Twitter + OAuth发送推文

时间:2011-07-15 04:53:16

标签: iphone objective-c cocoa-touch ios4 twitter-oauth

我在我的应用中使用Twitter + OAuth。

但是我的NSLog中出现了以下错误。

  Request E0B47D37-CE8C-406A-867E-2133E2469397 failed with error Error Domain=HTTP Code=401 "The operation couldn’t be completed. (HTTP error 401.)"

编码:.h文件

 #import "SA_OAuthTwitterEngine.h"
 #import "SA_OAuthTwitterController.h"

 @interface FirstViewController: UIViewController <SA_OAuthTwitterControllerDelegate,SA_OAuthTwitterEngineDelegate> 
 {
 SA_OAuthTwitterEngine *_engine;    
 }

编码:.m文件

#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);
    }

可能有什么问题?

3 个答案:

答案 0 :(得分:1)

到目前为止,你的编码部分一切都很好,除非你使用了你的实际消费者密钥,即使我遇到了同样的问题。我生成了不同的consumerKey和secretKey并且dat做了它

答案 1 :(得分:1)

生成新的cosumerKey和secretKey可能会解决您的问题。有时Twitter上的服务器也可能没有响应,因此twit可能无法发布

答案 2 :(得分:0)

您是否更改了.m文件顶部的#define语句,以使用您的实际消费者密钥和消费者密钥?