/// <summary> /// Sends an image file to Twitter to replace user image. /// </summary> /// <param name="image">byte array of image to upload</param> /// <param name="fileName">name to pass to Twitter for the file</param> /// <param name="imageType">type of image: must be one of jpg, gif, or png</param> /// <param name="includeEntities">Set to false to not include entities. (default: true)</param> /// <param name="skipStatus">Don't include status with response.</param> /// <returns>User with new image info</returns> public async Task <User> UpdateAccountImageAsync(byte[] image, string fileName, string imageType, bool includeEntities, bool skipStatus, CancellationToken cancelToken = default(CancellationToken)) { var accountUrl = BaseUrl + "account/update_profile_image.json"; if (image == null || image.Length == 0) { throw new ArgumentException("image is required.", "image"); } if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentException("fileName is required.", "fileName"); } if (string.IsNullOrWhiteSpace(imageType)) { throw new ArgumentException("imageType is required.", "imageType"); } var reqProc = new UserRequestProcessor <User>(); var parameters = new Dictionary <string, string> { { "include_entities", includeEntities.ToString().ToLower() }, { "skip_status", skipStatus.ToString().ToLower() } }; string name = "image"; string imageMimeType = "image/" + imageType; RawResult = await TwitterExecutor.PostMediaAsync(accountUrl, parameters, image, name, fileName, imageMimeType, cancelToken).ConfigureAwait(false); return(reqProc.ProcessActionResult(RawResult, UserAction.SingleUser)); }
/// <summary> /// Uploads a media (e.g. media) to be attached to a subsequent tweet. /// </summary> /// <param name="media">Media to upload</param> /// <param name="mediaType">Type of media. e.g. image/jpg, image/png, or video/mp4.</param> /// <param name="additionalOwners">User IDs of accounts that can used the returned media IDs</param> /// <param name="cancelToken">Allows you to cancel async operation</param> /// <returns>Status containing new reply</returns> public virtual async Task <Media> UploadMediaAsync(byte[] media, string mediaType, IEnumerable <ulong> additionalOwners, CancellationToken cancelToken = default(CancellationToken)) { if (media == null || media.Length == 0) { throw new ArgumentNullException("image", "You must provide a byte[] of image data."); } string updateUrl = UploadUrl + "media/upload.json"; string name = "media"; string randomUnusedFileName = new Random().Next(100, 999).ToString(); var parameters = new Dictionary <string, string>(); if (additionalOwners != null && additionalOwners.Any()) { parameters.Add("additional_owners", string.Join(",", additionalOwners)); } var reqProc = new StatusRequestProcessor <Status>(); RawResult = await TwitterExecutor.PostMediaAsync( updateUrl, new Dictionary <string, string>(), media, name, randomUnusedFileName, mediaType, cancelToken) .ConfigureAwait(false); Status status = reqProc.ProcessActionResult(RawResult, StatusAction.MediaUpload); return(status.Media); }
/// <summary> /// Uploads a media (e.g. media) to be attached to a subsequent tweet. /// </summary> /// <param name="media">Media to upload</param> /// <param name="cancelToken">Allows you to cancel async operation</param> /// <returns>Status containing new reply</returns> public async Task <Media> UploadMediaAsync(byte[] media, CancellationToken cancelToken = default(CancellationToken)) { if (media == null || media.Length == 0) { throw new ArgumentNullException("image", "You must provide a byte[] of image data."); } string updateUrl = UploadUrl + "media/upload.json"; string imageType = "application/octet-stream"; string name = "media"; string randomUnusedFileName = new Random().Next(100, 999).ToString(); var reqProc = new StatusRequestProcessor <Status>(); RawResult = await TwitterExecutor.PostMediaAsync( updateUrl, new Dictionary <string, string>(), media, name, randomUnusedFileName, imageType, cancelToken) .ConfigureAwait(false); Status status = reqProc.ProcessActionResult(RawResult, StatusAction.MediaUpload); return(status.Media); }
public async Task <Status> ReplyWithMediaAsync(ulong inReplyToStatusID, string status, bool possiblySensitive, decimal latitude, decimal longitude, string placeID, bool displayCoordinates, byte[] image, CancellationToken cancelToken = default(CancellationToken)) { if (string.IsNullOrWhiteSpace(status)) { throw new ArgumentNullException("status", "status is a required parameter."); } if (image == null || image.Length == 0) { throw new ArgumentNullException("image", "You must provide a byte[] of image data."); } string updateUrl = BaseUrl + "statuses/update_with_media.json"; string imageType = "application/octet-stream"; string name = "media[]"; string randomUnusedFileName = new Random().Next(100, 999).ToString(); var reqProc = new StatusRequestProcessor <Status>(); RawResult = await TwitterExecutor.PostMediaAsync( updateUrl, new Dictionary <string, string> { { "status", status }, { "possibly_sensitive", possiblySensitive ? true.ToString() : null }, { "lat", latitude == NoCoordinate ? null : latitude.ToString(Culture.US) }, { "long", longitude == NoCoordinate ? null : longitude.ToString(Culture.US) }, { "place_id", string.IsNullOrWhiteSpace(placeID) ? null : placeID }, { "display_coordinates", displayCoordinates ? true.ToString() : null }, { "in_reply_to_status_id", inReplyToStatusID == NoReply ? null : inReplyToStatusID.ToString(CultureInfo.InvariantCulture) } }, image, name, randomUnusedFileName, imageType, cancelToken) .ConfigureAwait(false); return(reqProc.ProcessActionResult(RawResult, StatusAction.SingleStatus)); }
/// <summary> /// Sends an image to Twitter to be placed as the user's profile banner. /// </summary> /// <param name="banner">byte[] containing image data.</param> /// <param name="fileName">Name of file.</param> /// <param name="imageType">Type of file (e.g. png or jpg)</param> /// <param name="width">Pixel width to clip image.</param> /// <param name="height">Pixel height to clip image.</param> /// <param name="offsetLeft">Pixels to offset start of image from the left.</param> /// <param name="offsetTop">Pixels to offset start of image from the top.</param> /// <returns> /// Account of authenticated user who's profile banner will be updated. /// Url of new banner will appear in ProfileBannerUrl property. /// </returns> public async Task <User> UpdateProfileBannerAsync(byte[] banner, string fileName, string imageType, int width, int height, int offsetLeft, int offsetTop, CancellationToken cancelToken = default(CancellationToken)) { var accountUrl = BaseUrl + "account/update_profile_banner.json"; if (banner == null || banner.Length == 0) { throw new ArgumentException("banner is required.", "banner"); } if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentException("fileName is required.", "fileName"); } if (string.IsNullOrWhiteSpace(imageType)) { throw new ArgumentException("imageType is required.", "imageType"); } var parameters = new Dictionary <string, string> { { "width", width.ToString() }, { "height", height.ToString() }, { "offset_left", offsetLeft.ToString() }, { "offset_top", offsetTop.ToString() }, //{ "banner", "FILE_DATA" } }; var reqProc = new UserRequestProcessor <User>(); string name = "banner"; string imageMimeType = "image/" + imageType; string mediaCategory = "tweet_image"; RawResult = await TwitterExecutor.PostMediaAsync(accountUrl, parameters, banner, name, fileName, imageMimeType, mediaCategory, shared : false, cancelToken : cancelToken).ConfigureAwait(false); return(reqProc.ProcessActionResult(RawResult, UserAction.SingleUser)); }
/// <summary> /// Sends an image file to Twitter to replace background image. /// </summary> /// <param name="image">full path to file, including file name</param> /// <param name="mediaID">ID of media to use (posted via UploadMediaAsync)</param> /// <param name="fileName">name to pass to Twitter for the file</param> /// <param name="imageType">type of image: must be one of jpg, gif, or png</param> /// <param name="tile">Tile image across background.</param> /// <param name="includeEntities">Set to false to not include entities. (default: true)</param> /// <param name="skipStatus">Don't include status with response.</param> /// <returns>User with new image info</returns> public async Task <User> UpdateAccountBackgroundImageAsync(byte[] image, ulong mediaID, string fileName, string imageType, bool tile, bool includeEntities, bool skipStatus, CancellationToken cancelToken = default(CancellationToken)) { var accountUrl = BaseUrl + "account/update_profile_background_image.json"; if (mediaID == NoMediaID && image == NoImage) { throw new ArgumentException("Either image or mediaID must be provided, but you haven't specified one.", nameof(image) + "Or" + nameof(mediaID)); } if (mediaID == NoMediaID && image.Length == 0) { throw new ArgumentException("Invalid image", nameof(image)); } if (image != NoImage && mediaID != NoMediaID) { throw new ArgumentException("Either image or mediaID must be provided, but not both", nameof(image) + "Or" + nameof(mediaID)); } if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentException("fileName is required.", nameof(fileName)); } if (string.IsNullOrWhiteSpace(imageType)) { throw new ArgumentException("imageType is required.", nameof(imageType)); } var parameters = new Dictionary <string, string> { { "include_entities", includeEntities.ToString().ToLower() }, { "skip_status", skipStatus.ToString().ToLower() } }; if (tile) { parameters.Add("tile", true.ToString().ToLower()); } if (mediaID != NoMediaID) { parameters.Add("media_id", mediaID.ToString()); } var reqProc = new UserRequestProcessor <User>(); string name = "image"; string imageMimeType = "image/" + imageType; string mediaCategory = "tweet_image"; if (image != NoImage) { RawResult = await TwitterExecutor.PostMediaAsync(accountUrl, parameters, image, name, fileName, imageMimeType, mediaCategory, shared : false, cancelToken : cancelToken).ConfigureAwait(false); } else { RawResult = await TwitterExecutor.PostFormUrlEncodedToTwitterAsync <User>(HttpMethod.Post.ToString(), accountUrl, parameters, cancelToken).ConfigureAwait(false); } return(reqProc.ProcessActionResult(RawResult, UserAction.SingleUser)); }