parseを使ってtwitterに画像付きの投稿を行う

めも

- (void)p_postTwitterMedia
{
    NSData* data = UIImageJPEGRepresentation(image, 1.0);
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    NSMutableURLRequest *tweetRequest = [manager.requestSerializer multipartFormRequestWithMethod:@"POST"
                                                                                        URLString:@"https://upload.twitter.com/1.1/media/upload.json"
                                                                                       parameters:nil
                                                                        constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
                                                                            [formData appendPartWithFormData:data name:@"media"];
                                                                        }
                                                                                            error:nil];
    
    [[PFTwitterUtils twitter] signRequest:tweetRequest];
    
    AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:tweetRequest success:^(AFHTTPRequestOperation* operation, id responseObject){
        NSLog(@"success!");
        [self p_postTwitterStatus:responseObject[@"media_id_string"]];
    }failure:^(AFHTTPRequestOperation* operation, NSError* error){
        NSLog(@"operation=%@",operation);
        NSLog(@"eror=%@",error);
    }];
    
    [manager.operationQueue addOperation:operation];
}

- (void)p_postTwitterStatus:(NSString *)mediaId
{
    NSMutableURLRequest *tweetRequest = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST"
                                                                                      URLString:@"https://api.twitter.com/1.1/statuses/update.json"
                                                                                     parameters:@{@"status":@"hogehoge", @"media_ids":mediaId}.mutableCopy
                                                                                          error:nil];

    [[PFTwitterUtils twitter] signRequest:tweetRequest];
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:tweetRequest];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
        NSLog(@"success!");
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        NSLog(@"fail!");
        NSLog(@"operation=%@",operation);
        NSLog(@"error=%@",error);
        
    }];
    
    [[NSOperationQueue mainQueue] addOperation:operation];
}

まとめ

「statuses/update_with_media」を使うサンプルがよくあるけど、こいつdeprecatedになっている。なので、画像を一旦上げてから帰ってきたmediaIdを使ってつぶやきを投稿することでひも付けされる。