iOS - Twitter集成1.1 oauth

时间:2014-02-24 10:06:30

标签: ios twitter

我在我的应用中集成了twitter。我用谷歌搜索并搜索,找不到合适的步骤。 任何人都可以告诉我它是如何工作的细节。 谢谢。

2 个答案:

答案 0 :(得分:1)

我希望这会对你有所帮助。 。

1.将以下课程添加到您的项目中 GTMOAuthAuthentication.h /米
GTMOAuthSignIn.h /米
GTMHTTPFetcher.h /米
GTMOAuthViewControllerTouch.h /米
GTMOAuthViewTouch.xib

2。添加以下框架 Security.framework和SystemConfiguration.framework。

3.set -ObjC构建选项,用于应用程序目标的“其他链接器标志”。

4。然后是时候进行一些编码了。

import GTMOAuthAuthentication.h and GTMOAuthViewControllerTouch.h

    - (void)signInWithTwitter
    {
    NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"];
    NSURL *accessURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"];
    NSURL *authorizeURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/authorize"];
    NSString *scope = @"http://api.twitter.com/";

    GTMOAuthAuthentication *auth = [self authForTwitter];

    [auth setCallback:@"http://www.noop.com/OAuthCallback"];

    GTMOAuthViewControllerTouch *viewController;

    viewController = [[GTMOAuthViewControllerTouch alloc] initWithScope:scope
                                                                 language:nil
                                                          requestTokenURL:requestURL
                                                        authorizeTokenURL:authorizeURL
                                                           accessTokenURL:accessURL
                                                           authentication:auth
                                                           appServiceName:@"AppName : Twitter"
                                                                 delegate:self
    finishedSelector:@selector(viewController:finishedWithAuth:error:)];

    [appDelegate.navigationController pushViewController:viewController animated:YES];
    }

    - (GTMOAuthAuthentication *)authForTwitter {
    GTMOAuthAuthentication *auth = [[GTMOAuthAuthentication alloc] initWithSignatureMethod:kGTMOAuthSignatureMethodHMAC_SHA1
                consumerKey:TWITTER_CONSUMER_KEY
                 privateKey:TWITTER_CONSUMER_SECRET];

    [auth setServiceProvider:@"Twitter"];

    return auth;
    }

    - (void)viewController:(GTMOAuthViewControllerTouch *)viewController finishedWithAuth:(GTMOAuthAuthentication *)auth error:(NSError *)error {

        if(error)
        {
             //handle error
        }
        else
        {
            // do stuff as per your app.
        }
    }

NOte : if you get error message like "failed to validate oauth signature and token" then check you system time is correct or not .

答案 1 :(得分:1)

1)下载oauth库以进行ios的自动检测。 2)在Twitter上使用所有密钥创建帐户    Consumer Key,Consumer Secrete,访问密钥访问令牌    将int添加到.... 3) - (void)getList {

OAConsumer *con = [[OAConsumer alloc]initWithKey:@"TwitterConsumerKey" secret:@"TwitterConsumerSecret"];


OAToken *p=[[OAToken alloc]initWithKey:[@"TwitterAccessTokenKey" secret:@"TwitterAccessTokenSecret"];

int kPageCount = 10;        NSString * pro = [NSString stringWithFormat:@“%@ count =%i”,@“​​https://api.twitter.com/1.1/statuses/user_timeline.json?”,kPageCount];

    NSURL *tt=[NSURL URLWithString:pro];

    OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:tt
                                               consumer:con
                                                  token:p
                                                  realm:nil
                                                           signatureProvider:nil] ; 

    [request setHTTPMethod:@"GET"];
    fetcher = [[OADataFetcher alloc] init] ;        
    [fetcher fetchDataWithRequest:request
                         delegate:self
                didFinishSelector:@selector(requestTokenTicket:didFinishWithData:)
                  didFailSelector:@selector(requestTokenTicket:didFailWithError:)];

}

}

- (void)requestTokenTicket:(OAServiceTicket *)ticket didFailWithError:(NSError *)err {

if (err.domain==NSURLErrorDomain && err.code==-1001) {
    [self getList];
}

} - (void)requestTokenTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data {

NSString *responseBody = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];


NSLog(@"responseBody :%@",responseBody);

}