Ios 推特API允许我通过状态发布推特,但如果我想添加图像,则不上传媒体
在登录Twitter并通过身份验证后,我能够成功地将一条推文(只是消息)发布到更新url 常数Ios 推特API允许我通过状态发布推特,但如果我想添加图像,则不上传媒体,ios,swift,twitter,Ios,Swift,Twitter,在登录Twitter并通过身份验证后,我能够成功地将一条推文(只是消息)发布到更新url 常数 let kTwitterPOSTmethod = "POST" let kTwitterUpdateURL = "https://api.twitter.com/1.1/statuses/update.json" let kTwitterUploadURL = "https://upload.twitter.com/1.1/media/upload.json" Twitter客户端的东西 let
let kTwitterPOSTmethod = "POST"
let kTwitterUpdateURL = "https://api.twitter.com/1.1/statuses/update.json"
let kTwitterUploadURL = "https://upload.twitter.com/1.1/media/upload.json"
Twitter客户端的东西
let store = Twitter.sharedInstance().sessionStore
if let userid = store.session()?.userID {
let client = TWTRAPIClient(userID: userid) //from logInWithCompletion() in the previous VC
....
}
当base64Image字符串为“”时有效的函数
包含图像时不起作用的函数。我发布到上传url并尝试使用JSON序列化程序函数获取媒体id字符串,但它告诉我我未经授权
else {
let postImageWithStatusRequest = client.URLRequestWithMethod(kTwitterPOSTmethod, URL: kTwitterUploadURL, parameters: ["media": snapBase64String], error: nil)
client.sendTwitterRequest(postImageWithStatusRequest, completion: { (response, data, error) in
if error != nil {
print(error?.localizedDescription)
}
if let mediaDict = self.dataToJSON(data!) {
let message = ["status": withMessage, "media_ids": mediaDict["media_id_string"]]
let request = client.URLRequestWithMethod(kTwitterPOSTmethod,
URL: kTwitterUpdateURL, parameters: message as! [String : AnyObject], error:nil)
client.sendTwitterRequest(request, completion: { (response, data, connectionError) -> Void in
if connectionError == nil {
print("sendTwitterRequest(): \(response)")
complete(success: true)
}
else {
print("sendTwitterRequest(): \(connectionError)")
complete(success: false)
}
})
}
})
}
JSON转换函数
class func dataToJSON(data: NSData) -> AnyObject? {
do {
return try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers)
} catch let myJSONError {
print(myJSONError)
}
return nil
}
我收到的错误消息。。。我是否需要不同的身份验证才能发布到服务器的上载部分?我不知道为什么它允许我发布状态,但当我想发布媒体时,它不起作用
▿ 可选的
-Some:Error Domain=TwitterAPIErrorDomain Code=32“请求失败:未经授权(401)”
UserInfo={NSErrorFailingURLKey=,,
NSLocalizedDescription=请求失败:未经授权(401),
NSLocalizedFailureReason=Twitter API错误:无法进行身份验证
你(代码32)}
谢谢你的帮助 我弄明白了,这与身份验证无关,这个错误很容易引起误解 我所做的是改变了我编码base64字符串的方式。我只是将选项设置为nothing,而不是64CharacterLineLength,结果成功了
let base64String = mapSnapData.base64EncodedStringWithOptions([])
snapBase64String = base64String
我弄明白了,这与身份验证无关,错误很容易引起误解 我所做的是改变了我编码base64字符串的方式。我只是将选项设置为nothing,而不是64CharacterLineLength,结果成功了
let base64String = mapSnapData.base64EncodedStringWithOptions([])
snapBase64String = base64String
下面是我的工作代码
func shareToTwitter(){
let store = Twitter.sharedInstance().sessionStore
if let userid = store.session()?.userID {
let client = TWTRAPIClient(userID: userid)
let statusesShowEndpoint = "https://upload.twitter.com/1.1/media/upload.json"
let image = UIImage(named: "How_To_Say_TY")
let data = UIImagePNGRepresentation(image!)
let strImage = data?.base64EncodedStringWithOptions([])
let params : [String : AnyObject] = ["media": strImage!]
var clientError : NSError?
let request = client.URLRequestWithMethod("POST", URL: statusesShowEndpoint, parameters: params, error: &clientError)
client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
if (connectionError == nil) {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
print("test tweet :- \(json["media_id_string"])")
self.updateTwitterStatusWithMediaID("\(json["media_id_string"])")
} catch {
print("error serializing JSON: \(error)")
}
}
else {
print("Error: \(connectionError)")
}
}
}
}
func updateTwitterStatusWithMediaID( mediaID : String ){
let store = Twitter.sharedInstance().sessionStore
if let userid = store.session()?.userID {
let client = TWTRAPIClient(userID: userid)
let statusesShowEndpoint = "https://api.twitter.com/1.1/statuses/update.json"
let params = ["status": "Test by Carl.","media_ids":mediaID]
var clientError : NSError?
let request = client.URLRequestWithMethod("POST", URL: statusesShowEndpoint, parameters: params, error: &clientError)
client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
if (connectionError == nil) {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
print("test tweet :- \(json)")
} catch {
print("error serializing JSON: \(error)")
}
}
else {
print("Error: \(connectionError)")
}
}
}
}
下面是我的工作代码
func shareToTwitter(){
let store = Twitter.sharedInstance().sessionStore
if let userid = store.session()?.userID {
let client = TWTRAPIClient(userID: userid)
let statusesShowEndpoint = "https://upload.twitter.com/1.1/media/upload.json"
let image = UIImage(named: "How_To_Say_TY")
let data = UIImagePNGRepresentation(image!)
let strImage = data?.base64EncodedStringWithOptions([])
let params : [String : AnyObject] = ["media": strImage!]
var clientError : NSError?
let request = client.URLRequestWithMethod("POST", URL: statusesShowEndpoint, parameters: params, error: &clientError)
client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
if (connectionError == nil) {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
print("test tweet :- \(json["media_id_string"])")
self.updateTwitterStatusWithMediaID("\(json["media_id_string"])")
} catch {
print("error serializing JSON: \(error)")
}
}
else {
print("Error: \(connectionError)")
}
}
}
}
func updateTwitterStatusWithMediaID( mediaID : String ){
let store = Twitter.sharedInstance().sessionStore
if let userid = store.session()?.userID {
let client = TWTRAPIClient(userID: userid)
let statusesShowEndpoint = "https://api.twitter.com/1.1/statuses/update.json"
let params = ["status": "Test by Carl.","media_ids":mediaID]
var clientError : NSError?
let request = client.URLRequestWithMethod("POST", URL: statusesShowEndpoint, parameters: params, error: &clientError)
client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
if (connectionError == nil) {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
print("test tweet :- \(json)")
} catch {
print("error serializing JSON: \(error)")
}
}
else {
print("Error: \(connectionError)")
}
}
}
}
更新:我将参数“media”更改为“media\u data”,我认为它应该是,并且仍然得到相同的东西…更新:我将参数“media”更改为“media\u data”,我认为它应该是,并且仍然得到相同的东西。。。