在iOS中使用新API(1.1)发布推文 [英] Posting tweet with new api (1.1) in iOS

查看:69
本文介绍了在iOS中使用新API(1.1)发布推文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于更改了twitter api,因此我的应用程序不再发布tweet.根据新的API,一切都运转良好,并且只有请求模式应该更改吗?初始化引擎和其余部分的所有其他内容应该是相同的吗?

Since the twitter api is changed my app no longer posts tweet. Every thing was working fine before and according to new API only request pattern should change ? All the other stuff initialising the engine and rest should be same ?

应该修改共享方法,因此我对其进行了修改,但获得了身份验证错误215".我从twitter本身生成的身份验证标头中获得了所有令牌信息和其他内容:

And the method for sharing should be modified, so I modified it but getting "Bad Authentication 215". All the token info and other things I got it from the authentication header generated from twitter itself:

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

推荐答案

Twitter在其新文档中解释了从身份验证到大量琐碎事情的所有内容,但对于使用1.0 api的以前的应用程序而言,如何发布推文或升级.

Twitter explained every thing in its new documentation from Authentication to loads of trivial things but no easy way for previous apps using 1.0 api, how to post a tweet or to upgrade.

只需进行一些更改即可使用(适用于OAuth):

Few changes to make it work (works for OAuth):

打开MGTwitterEngine.m>

Open the 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被调用.但是很容易检查是否成功调用,而不是在失败方法中什么也不做.

You will be surprised that - (void) requestSucceeded: (NSString *) requestIdentifier and - (void) requestFailed: (NSString *) requestIdentifier withError: (NSError *) error will be called. But it is pretty easy to put a check that if succeeded is called than don't do anything in failed method.

这篇关于在iOS中使用新API(1.1)发布推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆