0

我正在使用此代码将推文从我的应用程序发送到用户 Twitter 帐户。在 Twitter 设备设置中至少选择一个帐户之前,我没有问题。如果我从设置中删除所有帐户,我的推文将失败。

 ACAccountStore *accountStore=[[ACAccountStore alloc] init];
   ACAccountType *twitterType =
   [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

   SLRequestHandler requestHandler =
   ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
      if (responseData) {
         NSInteger statusCode = urlResponse.statusCode;
         if (statusCode >= 200 && statusCode < 300) {
            NSDictionary *postResponseData =
            [NSJSONSerialization JSONObjectWithData:responseData
                                            options:NSJSONReadingMutableContainers
                                              error:NULL];
            NSLog(@"[SUCCESS!] Created Tweet with ID: %@", postResponseData[@"id_str"]);
         }
         else {
            NSLog(@"[ERROR] Server responded: status code %d %@", statusCode,
                  [NSHTTPURLResponse localizedStringForStatusCode:statusCode]);
         }
      }
      else {
         NSLog(@"[ERROR] An error occurred while posting: %@", [error localizedDescription]);
      }
   };

   ACAccountStoreRequestAccessCompletionHandler accountStoreHandler =
   ^(BOOL granted, NSError *error) {
      if (granted) {
         NSArray *accounts = [accountStore accountsWithAccountType:twitterType];
         NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"
                       @"/1.1/statuses/update_with_media.json"];
         NSDictionary *params = @{@"status" : @"status"};
         SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                 requestMethod:SLRequestMethodPOST
                                                           URL:url
                                                    parameters:params];
         NSData *imageData = UIImageJPEGRepresentation(Photo, 1.f);
         [request addMultipartData:imageData
                          withName:@"media[]"
                              type:@"image/jpeg"
                          filename:@"image.jpg"];
         [request setAccount:[accounts lastObject]];
         [request performRequestWithHandler:requestHandler];
      }
      else {
         NSLog(@"[ERROR] An error occurred while asking for user authorization: %@",
               [error localizedDescription]);
      }
   };

   [accountStore requestAccessToAccountsWithType:twitterType
                                              options:NULL
                                           completion:accountStoreHandler];

当推特设备设置中没有帐户时,有办法发送推文吗?也许完美的解决方案应该是调用一个 api 并使用用户名和密码作为参数进行登录请求。有办法做到这一点吗?还是有更好的方法?

4

1 回答 1

0

您要做的是尝试仅使用帐户框架发送推文。您还没有编写代码来使用普通的 twitter API 发送推文。

如果您可以使用帐户框架发送推文,请先做一件事验证。如果不是,则继续进行正常的 OAuth 身份验证

您可以使用验证 Twitter 帐户

if ([TWTweetComposeViewController canSendTweet])
{
    TWTweetComposeViewController *tweetSheet = 
        [[TWTweetComposeViewController alloc] init];
    [tweetSheet setInitialText:@"Initial Tweet Text!"];
    [self presentModalViewController:tweetSheet animated:YES];
}

如果此条件失败,则使用 OAuth API 发布甜蜜。最简单的方法是使用此控件

https://github.com/malcommac/DMTwitterOAuth

于 2013-09-06T16:44:58.743 回答