Upload videos to YouTube using AFNetworking 2.0

I am trying to upload a video to YouTube using the v3 API using AFNetworking 2.0. The download itself works fine, and I can see the video on my channel. I have a problem: parameters ( video resource ) to indicate the title, description, etc., which I should place as the request body (together with the video itself) Here is the code that I use.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"Bearer #_token_goes_here#" forHTTPHeaderField:@"Authorization"];
NSDictionary *parameters = @{@"snippet" : @{@"title" : @"random_title",
                                                                   @"description" : @"random_description"}};
NSURL *filePath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video" ofType:@"mov"]];
[manager POST:@"https://www.googleapis.com/upload/youtube/v3/videos?part=snippet,status" parameters:parameters  constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileURL:filePath name:@"video" fileName:@"video.mov" mimeType:@"video/*" error:NULL];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

How to properly configure JSON parameters for a request? Thank.

+4
source share
2 answers

, , - , , , - PUT, .

+2

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject: parameters options:NSJSONWritingPrettyPrinted error:NULL];
    NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
    [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"", @"snippet"] forKey:@"Content-Disposition"];
    [mutableHeaders setValue:@"application/json" forKey:@"Content-Type"];
    [formData appendPartWithHeaders:mutableHeaders body:jsonData];

    [formData appendPartWithFileURL:filePath name:@"video" fileName:@"video.mov" mimeType:@"video/*" error:NULL];

.

+1

Source: https://habr.com/ru/post/1532873/


All Articles