iOS-无法使用Twitter/Fabric新SDK上载媒体
我想从我的iOS应用程序向twitter发布一张照片。我可以在没有媒体的情况下发布推文,但当我尝试连接媒体时,它会抛出一个错误 我正在关注twitter文档,根据这一点,首先我需要将媒体上传到,然后我将能够使用media-id将其与tweet连接起来 这是我上传媒体的代码 应用程序在URLRequestWithMedthod调用时崩溃 帮我解决这个问题iOS-无法使用Twitter/Fabric新SDK上载媒体,ios,objective-c,twitter,twitter-rest-api,Ios,Objective C,Twitter,Twitter Rest Api,我想从我的iOS应用程序向twitter发布一张照片。我可以在没有媒体的情况下发布推文,但当我尝试连接媒体时,它会抛出一个错误 我正在关注twitter文档,根据这一点,首先我需要将媒体上传到,然后我将能够使用media-id将其与tweet连接起来 这是我上传媒体的代码 应用程序在URLRequestWithMedthod调用时崩溃 帮我解决这个问题 UIImage *image = [UIImage imageNamed:@"shareit.png"]; NSData *imageData
UIImage *image = [UIImage imageNamed:@"shareit.png"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.7);
NSString *statusesShowEndpoint = @"https://upload.twitter.com/1.1/media/upload.json";
NSDictionary *params = @{@"media" : imageData};
NSError *clientError;
NSURLRequest *request = [[[Twitter sharedInstance] APIClient]
URLRequestWithMethod:@"POST"
URL:statusesShowEndpoint
parameters:params
error:&clientError];
if (request) {
[[[Twitter sharedInstance] APIClient]
sendTwitterRequest:request
completion:^(NSURLResponse *response,
NSData *data,
NSError *connectionError) {
if (data) {
// handle the response data e.g.
NSError *jsonError;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:data
options:0
error:&jsonError];
NSLog(@"%@",json);
}
else {
NSLog(@"Error: %@", connectionError);
}
}];
}
else {
NSLog(@"Error: %@", clientError);
}
其实很简单。缺少的只是将imagedata转换为base64EncodedString。这是解决办法
NSString *media = @"https://upload.twitter.com/1.1/media/upload.json";
NSData *imageData = UIImageJPEGRepresentation(image, 0.9);
NSString *imageString = [imageData base64EncodedStringWithOptions:0];
NSError *error;
NSURLRequest *request = [[[Twitter sharedInstance] APIClient] URLRequestWithMethod:@"POST" URL:media parameters:@{@"media":imageString} error:&error];
[[[Twitter sharedInstance] APIClient] sendTwitterRequest:request completion:^(NSURLResponse *urlResponse, NSData *data, NSError *connectionError) {
NSError *jsonError;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:data
options:0
error:&jsonError];
NSLog(@"Media ID : %@",[json objectForKey:@"media_id_string"]);
// Post tweet With media_id
}];
完成萨尼的答案
// Post tweet With media_id
mediaID = [json objectForKey:@"media_id_string"];
client = [[Twitter sharedInstance] APIClient];
message = @{@"status": title, @"wrap_links": @"true", @"media_ids": mediaID};
NSURLRequest *request = [client URLRequestWithMethod:@"POST" URL:@"https://api.twitter.com/1.1/statuses/update.json" parameters:message error:&error];
[client sendTwitterRequest:request completion:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError) {
NSLog(@"error %@",[connectionError localizedDescription]);
} else {
NSError *jsonError;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
NSLog(@"json Finish! %@",json);
}
}];
如果有人仍在寻求帮助,请共享swift 2.2(Xcoe 7.3.2)代码
let client = TWTRAPIClient.init(userID:<Twitter_User_Id_Here>)
let imgData = UIImageJPEGRepresentation(UIImage(named:"Home")!, 0.7)
client.uploadMedia(imgData!, contentType: "image/jpeg", completion: {(media_ids,error) in
if error == nil{
//prints the media_ids in String.
//Pass them into the params of status update params ["media_ids":<MEDIA_ID>]
print(media_ids)
}
else{
print(error?.localizedDescription)
}
})
let client=TWTRAPIClient.init(userID:)
设imgData=uiImageJPEG表示法(UIImage(名为:“Home”)!,0.7)
client.uploadMedia(imgData!,contentType:“image/jpeg”,完成:{(media_id,error)in
如果错误==nil{
//以字符串形式打印媒体标识。
//将它们传递到状态更新参数[“媒体ID”:
打印(媒体标识)
}
否则{
打印(错误?.localizedDescription)
}
})
发送状态更新
let request = client.URLRequestWithMethod("POST", URL: "https://api.twitter.com/1.1/statuses/update.json", parameters: ["status":"Hello World","media_ids":<Media_Ids_Here>], error: nil)
client.sendTwitterRequest(request, completion: {
(response,data,error)in
if error == nil {
print(data)
}
else{
print("Error")
}
})
}
let request=client.URLRequestWithMethod(“POST”,URL:”https://api.twitter.com/1.1/statuses/update.json,参数:[“状态”:“你好世界”,“媒体ID”:],错误:无)
client.sendTwitterRequest(请求、完成:{
(响应、数据、错误)
如果错误==nil{
打印(数据)
}
否则{
打印(“错误”)
}
})
}
Swift 4最新版本
安装pod文件pod'TwitterKit'
import TwitterKit
这真的对你有用吗?我正在尝试这个,但503服务不可用。没有oauth_*params,你是如何让它工作的?@sun没有oauth_*params,它将无法工作。你需要在使用前登录。为我工作,谢谢!如何处理多个图像?你能帮我做这个吗
func PostTweetToTwitter() {
let twitter_USERID = UserDefaults.standard.string(forKey: Constants.Twitter.TWITTER_USER_ID)
let url = URL(string: "http://www.tinyeyeimage.com/picture/Photos/149802345.png")
let tweetImage = try? Data(contentsOf: url!)
let tweetString = "Welcome to the Twitter world!!"
let uploadUrl = "https://upload.twitter.com/1.1/media/upload.json"
let updateUrl = "https://api.twitter.com/1.1/statuses/update.json"
let imageString = tweetImage?.base64EncodedString(options: NSData.Base64EncodingOptions())
let client = TWTRAPIClient.init(userID: twitter_USERID)
let requestUploadUrl = client.urlRequest(withMethod: "POST", urlString: uploadUrl, parameters: ["media": imageString], error: nil)
client.sendTwitterRequest(requestUploadUrl) { (urlResponse, data, connectionError) -> Void in
if connectionError == nil {
if let mediaDict = self.dataToJSON(data: (data! as NSData) as Data) as? [String : Any] {
let media_id = mediaDict["media_id_string"] as! String
let message = ["status": tweetString, "media_ids": media_id]
let requestUpdateUrl = client.urlRequest(withMethod: "POST", urlString: updateUrl, parameters: message, error: nil)
client.sendTwitterRequest(requestUpdateUrl, completion: { (urlResponse, data, connectionError) -> Void in
if connectionError == nil {
if let _ = self.dataToJSON(data: (data! as NSData) as Data) as? [String : Any] {
print("Upload suceess to Twitter")
}
}
})
}
}
}
}