iOS - Unable to Upload media with Twitter/Fabric New SDK

后端 未结 4 1723
面向向阳花
面向向阳花 2021-02-06 12:52

I want to post a photo to twitter from my iOS app. I can post a tweet without media but when i am trying to attach media it throws an error.

I am following twitter docu

相关标签:
4条回答
  • 2021-02-06 13:14

    Well it was pretty simple. All was missing is conversion of imagedata into base64EncodedString. Here is the solution.

       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
        }];
    
    0 讨论(0)
  • 2021-02-06 13:20

    Swift 4 latest

    install pod file pod 'TwitterKit'

    import TwitterKit
    

    https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-init.html

    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")
                                }
                            }
    
                        })
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2021-02-06 13:24

    Completing the Saani's answer

    // 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);
        }
    }];
    
    0 讨论(0)
  • 2021-02-06 13:38

    Sharing the swift 2.2 (Xcoe 7.3.2) code if anyone is still looking for help.

     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)
            }
        })
    

    To Send with status update.

       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")
            }
        })
    }
    
    0 讨论(0)
提交回复
热议问题