Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

objective c - Share video on Twitter with Fabric API without composer iOS

Video uploads via REST API for Twitter is available in january but no with Fabric framework: link!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the documentation necessary to make 3 calls with the commands: INIT, APPEND and FINALIZE.

-(void) shareOnTwitterWithVideo:(NSDictionary*) params{   
    NSString *text = params[@"text"];
    NSData* dataVideo = params[@"video"];
    NSString *lengthVideo = [NSString stringWithFormat:@"%d", [params[@"length"] intValue]];
    NSString* url = @"https://upload.twitter.com/1.1/media/upload.json";

    __block NSString *mediaID;

    if([[Twitter sharedInstance] session]){

        TWTRAPIClient *client = [[Twitter sharedInstance] APIClient];
        NSError *error;
        // First call with command INIT
        NSDictionary *message =  @{ @"status":text,
                                   @"command":@"INIT",
                                @"media_type":@"video/mp4",
                               @"total_bytes":lengthVideo};
        NSURLRequest *preparedRequest = [client URLRequestWithMethod:@"POST" URL:url parameters:message error:&error];

        [client sendTwitterRequest:preparedRequest completion:^(NSURLResponse *urlResponse, NSData *responseData, NSError *error){

            if(!error){
                NSError *jsonError;
                NSDictionary *json = [NSJSONSerialization
                                      JSONObjectWithData:responseData
                                      options:0
                                      error:&jsonError];

                mediaID = [json objectForKey:@"media_id_string"];
                client = [[Twitter sharedInstance] APIClient];
                NSError *error;
                NSString *videoString = [dataVideo base64EncodedStringWithOptions:0];
                // Second call with command APPEND
                message = @{@"command" : @"APPEND",
                           @"media_id" : mediaID,
                      @"segment_index" : @"0",
                              @"media" : videoString};

                NSURLRequest *preparedRequest = [client URLRequestWithMethod:@"POST" URL:url parameters:message error:&error];

                [client sendTwitterRequest:preparedRequest completion:^(NSURLResponse *urlResponse, NSData *responseData, NSError *error){

                    if(!error){
                        client = [[Twitter sharedInstance] APIClient];
                        NSError *error;
                        // Third call with command FINALIZE
                        message = @{@"command" : @"FINALIZE",
                                                  @"media_id" : mediaID};

                        NSURLRequest *preparedRequest = [client URLRequestWithMethod:@"POST" URL:url parameters:message error:&error];

                        [client sendTwitterRequest:preparedRequest completion:^(NSURLResponse *urlResponse, NSData *responseData, NSError *error){

                            if(!error){
                                client = [[Twitter sharedInstance] APIClient];
                                NSError *error;
                                // publish video with status
                                NSString *url = @"https://api.twitter.com/1.1/statuses/update.json";
                                NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:text,@"status",@"true",@"wrap_links",mediaID, @"media_ids", nil];
                                NSURLRequest *preparedRequest = [client URLRequestWithMethod:@"POST" URL:url parameters:message error:&error];

                                [client sendTwitterRequest:preparedRequest completion:^(NSURLResponse *urlResponse, NSData *responseData, NSError *error){
                                    if(!error){
                                        NSError *jsonError;
                                        NSDictionary *json = [NSJSONSerialization
                                                              JSONObjectWithData:responseData
                                                              options:0
                                                              error:&jsonError];
                                        NSLog(@"%@", json);
                                    }else{
                                        NSLog(@"Error: %@", error);
                                    }
                                }];
                            }else{
                                NSLog(@"Error command FINALIZE: %@", error);
                            }
                        }];

                    }else{
                        NSLog(@"Error command APPEND: %@", error);
                    }
                }];

            }else{
                NSLog(@"Error command INIT: %@", error);
            }

        }];
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...