在iOS中发布带有新api(1.1)的推文

时间:2013-06-20 05:34:55

标签: iphone ios twitter-oauth

由于Twitter api已更改,我的应用不再发布推文。以前每件事都运行正常,根据新API,只有请求模式应该改变?初始化引擎和休息的所有其他东西应该是相同的吗?

应该修改共享方法,所以我修改了它但得到了“Bad Authentication 215”。我从twitter本身生成的身份验证标题中获取了所有令牌信息和其他内容:

- (void) shareOnTwitter
{         
    NSString *urlString = [NSString stringWithFormat:@"https://api.twitter.com/1.1/statuses/update.json"];

    NSURL *url = [NSURL URLWithString:urlString];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    [request setHTTPMethod:@"POST"];

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

    [dict setValue:@"check Check CHECK" forKey:@"status"];

    [dict setValue:@"-- - - - - -" forKey:@"oauth_consumer_key"];

    [dict setValue:@"- - - - - - -" forKey:@"oauth_nonce"];

    [dict setValue:@"- - - - - - -" forKey:@"oauth_signature"];

    [dict setValue:@"HMAC-SHA1" forKey:@"oauth_signature_method"];

    [dict setValue:@"- - - - - - -" forKey:@"oauth_timestamp"];

    [dict setValue:@"- - - - - - -" forKey:@"oauth_token"];

    [dict setValue:@"1.0" forKey:@"oauth_version"];

    NSString *jsonString = [dict JSONRepresentation];

    NSData *jsonData = [NSData dataWithBytes:[jsonString UTF8String] length:jsonString.length];

    [request setHTTPBody:jsonData];

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    NSLog(@"My request...%@", jsonString);

    NSData *urlData;

    NSURLResponse *response1;

    NSError *error = nil;

    urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response1 error:&error];

    if(error)
    {
        NSLog(@"Error %@",error);
    }

    if(!urlData)
    {
        NSLog(@"No connection!");
    }

    NSString *responseStr = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

    NSLog(@" Twitter : ... %@", responseStr);
}

2 个答案:

答案 0 :(得分:4)

尝试以下代码。它对我有用:

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
     {
         if (granted)
         {
             NSArray *accounts = [accountStore accountsWithAccountType:accountType];
             if (accounts.count)
             {
                 NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
                 [dict setValue:@"check Check CHECK" forKey:@"status"];

                 NSString *retweetString = [NSString stringWithFormat:@"https://api.twitter.com/1.1/statuses/update.json"];
                 NSURL *retweetURL = [NSURL URLWithString:retweetString];
                 TWRequest *request = [[TWRequest alloc] initWithURL:retweetURL parameters:dict requestMethod:TWRequestMethodPOST];
                 request.account = [accounts objectAtIndex:0];

                 [request performRequestWithHandler:^(NSData *responseData1, NSHTTPURLResponse *urlResponse, NSError *error)
                  {
                      if (responseData1)
                      {
                          NSError *error1 = nil;
                          id response = [NSJSONSerialization JSONObjectWithData:responseData1 options:NSJSONReadingMutableLeaves error:&error1];
                      }
                  }];
             }
         }

     }];

答案 1 :(得分:0)

Twitter解释了其新文档中的所有内容,从身份验证到大量微不足道的事情,但以前使用1.0 api的应用程序没有简单的方法,如何发布推文或升级。

几乎没有任何变化使其有效(适用于OAuth):

打开MGTwitterEngine.m>

确保宏的定义如下:

#define TWITTER_DOMAIN          @"api.twitter.com/1.1"
#define HTTP_POST_METHOD        @"POST"
#define TWITTER_SEARCH_DOMAIN   @"search.twitter.com"
#define DEFAULT_CLIENT_VERSION  @"1.0"


#if YAJL_AVAILABLE
    #define API_FORMAT @"xml"

    #import "MGTwitterStatusesYAJLParser.h"
    #import "MGTwitterMessagesYAJLParser.h"
    #import "MGTwitterUsersYAJLParser.h"
    #import "MGTwitterMiscYAJLParser.h"
    #import "MGTwitterSearchYAJLParser.h"
#else
    #define API_FORMAT @"json"

    #if USE_LIBXML
        #import "MGTwitterStatusesLibXMLParser.h"
        #import "MGTwitterMessagesLibXMLParser.h"
        #import "MGTwitterUsersLibXMLParser.h"
        #import "MGTwitterMiscLibXMLParser.h"
    #else
        #import "MGTwitterStatusesParser.h"
        #import "MGTwitterUsersParser.h"
        #import "MGTwitterMessagesParser.h"
        #import "MGTwitterMiscParser.h"
    #endif
#endif

您会惊讶地调用- (void) requestSucceeded: (NSString *) requestIdentifier- (void) requestFailed: (NSString *) requestIdentifier withError: (NSError *) error。但是,如果调用成功,则检查是否比在失败方法中不做任何事情都要容易。