1

1) 我从这个链接Twitter with OAuth中找到了 Twitter rush 示例代码, 用于集成。

我添加了消费者密钥和密钥。但它从未授权该应用程序。

2)如果我使用共享工具包,它会给出错误,即从 twitter 请求访问时出现问题。
3)如果我使用社交框架,那么如果用户没有在设置中添加他/她的帐户,它会发出警报。

我需要该用户不应该离开应用程序进行 Twitter 登录。

有谁知道与 twitter 集成的最佳方式是什么?

请帮忙。

4

2 回答 2

3

我希望这能帮到您 。. .

1.在你的项目中添加以下类 GTMOAuthAuthentication.h/m GTMOAuthSignIn.h/m GTMHTTPFetcher.h/m GTMOAuthViewControllerTouch.h/m GTMOAuthViewTouch.xib

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

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

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

导入 GTMOAuthAuthentication.h 和 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.
    }
}

注意:如果您收到“无法验证 oauth 签名和令牌”之类的错误消息,请检查您的系统时间是否正确。

于 2013-09-12T10:22:30.840 回答
1

如果您希望该用户不应该外出,您可以使用 ACAccountStore Account.FrameworkSocial.frameworkiOS 6,

 NSUrl *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"];
  NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:twittername,@"screen_name",nil];

     account = [[ACAccountStore alloc] init];
    ACAccountType *twitterAccountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    NSArray *twitterAccounts = [account accountsWithAccountType:twitterAccountType];

    // Runing on iOS 6
    if (NSClassFromString(@"SLComposeViewController") && [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        [account requestAccessToAccountsWithType:twitterAccountType options:NULL completion:^(BOOL granted, NSError *error)
         {
             if (granted)
             {

                 SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url                                      parameters:params];

                 [request setAccount:[twitterAccounts lastObject]];

                 dispatch_async(dispatch_get_main_queue(), ^
                                {

                                    [NSURLConnection sendAsynchronousRequest:request.preparedURLRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response1, NSData *data, NSError *error)
                                     {
                                         dispatch_async(dispatch_get_main_queue(), ^
                                                        {
                                                            if (data)
                                                            {
//                                                                [self loadData:data];

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


                                                                NSLog(@"data:%@",newStr);
                                                            }
                                                        });
                                     }];
                                });
             }
         }];
    }
    else if (NSClassFromString(@"TWTweetComposeViewController") && [TWTweetComposeViewController canSendTweet]) // Runing on iOS 5
    {
        [account requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error)
         {
             if (granted)
             {
                 TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];
                 [request setAccount:[twitterAccounts lastObject]];

                 dispatch_async(dispatch_get_main_queue(), ^
                                {
                                    [NSURLConnection sendAsynchronousRequest:request.signedURLRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response1, NSData *data, NSError *error)
                                     {
                                         dispatch_async(dispatch_get_main_queue(), ^
                                                        {                             
                                                            if (data)                                 
                                                            {                                 
                                                                NSString* newStr = [[NSString alloc] initWithData:data
                                                                                                         encoding:NSUTF8StringEncoding];


                                                                NSLog(@"data:%@",newStr);                                                           }
                                                        });
                                     }];


                                });
             }
         }];
    }
}

您必须保留ACAccountStore:在 .h

@property (nonatomic, strong) ACAccountStore *account;
于 2013-09-12T09:50:33.053 回答