在没有作曲家的情况下使用 Fabric API 在 Twitter 上分享视频 iOS
Share video on Twitter with Fabric API without composer iOS
1 月份可以通过 REST API 为 Twitter 上传视频,但 Fabric 框架不可用:
link!
根据 documentation 需要使用以下命令进行 3 次调用:INIT、APPEND 和 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);
}
}];
}
}
很好的答案,把它转换成 swift 很有趣,我花了一些时间所以这里是给遇到同样问题的其他人的:
var video: NSData!
let strUploadUrl = "https://upload.twitter.com/1.1/media/upload.json"
let strStatusUrl = "https://api.twitter.com/1.1/statuses/update.json"
func postVideo() {
var client = Twitter.sharedInstance().APIClient
var text: String = "Testing Video"
var videoLength: String = "\(self.video.length)"
var mediaID: String = ""
var initError: NSError?
var message = ["status": text, "command" : "INIT", "media_type" : "video/m4v", "total_bytes" : videoLength]
var preparedRequest: NSURLRequest = client.URLRequestWithMethod("POST", URL: self.strUploadUrl, parameters: message, error: &initError)
client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in
if error == nil {
var jsonError: NSError?
var json: NSDictionary = (NSJSONSerialization.JSONObjectWithData(responseData!, options: nil, error: &jsonError) as? NSDictionary)!
println(json)
var mediaID = json.objectForKey("media_id_string") as! String
client = Twitter.sharedInstance().APIClient
var uploadError: NSError?
var videoString = self.video.base64EncodedStringWithOptions(nil)
message = ["command" : "APPEND", "media_id" : mediaID, "segment_index" : "0", "media" : videoString]
var preparedRequest = client.URLRequestWithMethod("POST", URL: self.strUploadUrl, parameters: message, error: &uploadError)
client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in
if error == nil {
client = Twitter.sharedInstance().APIClient
var finalizeError: NSError?
message = ["command":"FINALIZE", "media_id": mediaID]
var preparedRequest = client.URLRequestWithMethod("POST", URL: self.strUploadUrl, parameters: message, error: &finalizeError)
client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in
if error == nil {
client = Twitter.sharedInstance().APIClient
var sendError: NSError?
var message = ["status": text, "wrap_links": "true", "media_ids": mediaID]
var updateMessage = NSMutableDictionary(dictionary: message)
var preparedRequest = client.URLRequestWithMethod("POST", URL: self.strStatusUrl, parameters: message , error: &sendError)
client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in
})
} else {
println("Command FINALIZE failed \n \(error!)")
}
})
} else {
println("Command APPEND failed")
}
})
} else {
println("Command INIT failed")
}
})
}
1 月份可以通过 REST API 为 Twitter 上传视频,但 Fabric 框架不可用: link!
根据 documentation 需要使用以下命令进行 3 次调用:INIT、APPEND 和 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);
}
}];
}
}
很好的答案,把它转换成 swift 很有趣,我花了一些时间所以这里是给遇到同样问题的其他人的:
var video: NSData!
let strUploadUrl = "https://upload.twitter.com/1.1/media/upload.json"
let strStatusUrl = "https://api.twitter.com/1.1/statuses/update.json"
func postVideo() {
var client = Twitter.sharedInstance().APIClient
var text: String = "Testing Video"
var videoLength: String = "\(self.video.length)"
var mediaID: String = ""
var initError: NSError?
var message = ["status": text, "command" : "INIT", "media_type" : "video/m4v", "total_bytes" : videoLength]
var preparedRequest: NSURLRequest = client.URLRequestWithMethod("POST", URL: self.strUploadUrl, parameters: message, error: &initError)
client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in
if error == nil {
var jsonError: NSError?
var json: NSDictionary = (NSJSONSerialization.JSONObjectWithData(responseData!, options: nil, error: &jsonError) as? NSDictionary)!
println(json)
var mediaID = json.objectForKey("media_id_string") as! String
client = Twitter.sharedInstance().APIClient
var uploadError: NSError?
var videoString = self.video.base64EncodedStringWithOptions(nil)
message = ["command" : "APPEND", "media_id" : mediaID, "segment_index" : "0", "media" : videoString]
var preparedRequest = client.URLRequestWithMethod("POST", URL: self.strUploadUrl, parameters: message, error: &uploadError)
client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in
if error == nil {
client = Twitter.sharedInstance().APIClient
var finalizeError: NSError?
message = ["command":"FINALIZE", "media_id": mediaID]
var preparedRequest = client.URLRequestWithMethod("POST", URL: self.strUploadUrl, parameters: message, error: &finalizeError)
client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in
if error == nil {
client = Twitter.sharedInstance().APIClient
var sendError: NSError?
var message = ["status": text, "wrap_links": "true", "media_ids": mediaID]
var updateMessage = NSMutableDictionary(dictionary: message)
var preparedRequest = client.URLRequestWithMethod("POST", URL: self.strStatusUrl, parameters: message , error: &sendError)
client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in
})
} else {
println("Command FINALIZE failed \n \(error!)")
}
})
} else {
println("Command APPEND failed")
}
})
} else {
println("Command INIT failed")
}
})
}