【问题标题】:iOS - Twitter authentication and tweetiOS - Twitter 身份验证和推文
【发布时间】:2014-07-02 10:18:20
【问题描述】:

有什么简单的方法可以验证身份并将推文发布到 twitter?

我已经找到了post方法:

- (void)postImage:(UIImage *)image withStatus:(NSString *)status
{
    ACAccountType *twitterType = [self.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 = [self.accountStore accountsWithAccountType:twitterType];
            NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"
                          @"/1.1/statuses/update_with_media.json"];
//            NSDictionary *params = @{@"status" : status};
            NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                           status, @"status",
                                           nil];
            SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                    requestMethod:SLRequestMethodPOST
                                                              URL:url
                                                       parameters:params];
            NSData *imageData = UIImageJPEGRepresentation(image, 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]);
        }
    };

    [self.accountStore requestAccessToAccountsWithType:twitterType
                                               options:NULL
                                            completion:accountStoreHandler];
}

但是我遇到了一个错误

:[ERROR] 服务器响应:状态码 401 未授权

如何实现认证??

我已经在 twitter 上注册了我的应用,并且获得了两个 api 密钥(普通密钥和秘密密钥)。

【问题讨论】:

    标签: ios twitter


    【解决方案1】:

    这是我用来制作 Twitter 帖子的工具。您可能需要声明以下使用的属性:

    // ------------------------------------------------------------------------------
    // Example usage
    // ------------------------------------------------------------------------------
    
    NSString *initialText = @"Test";
    UIImage *backgroundImage = [UIImage imageNamed:@"background.png"];
    NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
    
    [self beginSocialNetworkPostForType:SLServiceTypeTwitter 
                            initialText:initialText
                              withImage:backgroundImage
                                 andURL:url];
    
    // ------------------------------------------------------------------------------
    // Method definition
    // ------------------------------------------------------------------------------
    -(void)beginSocialNetworkPostForType:(NSString *)serviceType
                             initialText:(NSString *)initialText
                               withImage:(UIImage *)postImage
                                     andURL:(NSURL *)postURL
    {
        if([SLComposeViewController isAvailableForServiceType:serviceType])
        {
            self.socialNetworkComposeVC = [SLComposeViewController composeViewControllerForServiceType:serviceType];
    
            if(postImage)
            {
                [self.socialNetworkComposeVC addImage:postImage];
            }
    
            if(initialText)
            {
                [self.socialNetworkComposeVC setInitialText:initialText];
            }
    
            if(postURL)
            {
                [self.socialNetworkComposeVC addURL:postURL];
            }
    
            [self presentViewController:self.socialNetworkComposeVC animated:YES completion:nil];
    
        }
    
        [self.socialNetworkComposeVC setCompletionHandler:^(SLComposeViewControllerResult result) {
    
            NSString *output;
    
            switch (result)
            {
                case SLComposeViewControllerResultCancelled:
                {
                    output = @"Action Cancelled";
                    break;
                }
                case SLComposeViewControllerResultDone:
                {
                    output = @"Your post was completed successfully";
    
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Post Complete" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                    [alert show];
    
                    break;
                }
                default:
                    break;
            }
        }];
    }
    

    补充说明:

    不确定这是否对您有帮助,但我也使用了 Sean Cook 的 Twitter 反向身份验证库:

    https://github.com/seancook/TWReverseAuthExample

    它有一个在 TWSignedRequest.m 文件中构建签名请求的方法,也许对你有帮助

    【讨论】:

      猜你喜欢
      • 2013-07-05
      • 2011-10-12
      • 2012-08-06
      • 2013-03-08
      • 2011-04-16
      • 2014-11-02
      • 2013-07-21
      • 2019-05-20
      • 2011-09-03
      相关资源
      最近更新 更多