質問

I'm trying to upload a video to YouTube using v3 API using AFNetworking 2.0. The upload itself is working fine and I am able to see a video at my channel. What I'm having a problem is the parameters (the video resource) to specify the title, description etc which I have to put as my request body (along with the video itself) Here is the code I am using.

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 do I properly set the JSON parameters for the request? Thanks.

役に立ちましたか?

解決

Ok, so if anyone is interested, the only solution I found for this is to send the second PUT update request to set the proper title and description.

他のヒント

You have to append the snippet to the multipart form data

    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];

One request, and only need the permissions to upload.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top