public string Create(string accountId, AdCreative model) { _results = new List <ValidationResult>(); _validationContext = new ValidationContext(model); var isValid = Validator.TryValidateObject(model, _validationContext, _results); ResponseShared response = null; if (!isValid) { throw new Exception("The AdCreative is invalid model, more inner exception", new Exception(GetErrorsMesages())); } accountId = GetAccount(accountId); //Valid Rules for Create Campaigns based in Facebook Api. if (model != null && !string.IsNullOrEmpty(accountId)) { try { response = ((string)_client.Post($"{accountId}/{ENDPOINT}", model)).JsonToObject <ResponseShared>(ResponseType.Other); if (response == null || string.IsNullOrEmpty(response.Id)) { throw new Exception("Error to trying saved AdCreative in Facebook"); } } catch (Exception e) { throw e; } } return(response.Id); }
/// <summary> /// Publishes photo ad to facebook as an asynchronous operation. /// </summary> /// <param name="photo"> /// The url or filepath of the ad photo. /// </param> /// <param name="request"> /// An object of <see cref="AdCreativeCreatingRequest"/>, which contains most of publish infomation. /// </param> /// <param name="adAccountId"> /// The ad account id of user. /// </param> /// <param name="accessToken"> /// User access token. /// </param> /// <param name="pageAccessToken"> /// Page access token. /// </param> /// <param name="publish"> /// If true, the photo ad will be published to a facebook page immediately. /// </param> /// <returns> /// The task object representing the asynchronous operation. /// </returns> /// <exception cref="FileNotFoundException"> /// Throw if the target photo file doesn't exist. /// </exception> public static async Task <ResponseMessage <string> > PublishPhotoAdAsync(string photo, AdCreativeCreatingRequest request, string adAccountId, string accessToken, string pageAccessToken, bool publish) { ResponseMessage <string> response = null; var imgUrl = String.Empty; var adCreative = new AdCreative(); // 1. Gets the url of image. #pragma warning disable SA1008 var(AdPhotoUrl, AdPhotoResponse) = await GetAdPhotoUrlAsync(photo, adAccountId, accessToken); #pragma warning restore SA1008 if (String.IsNullOrEmpty(AdPhotoUrl)) { return(AdPhotoResponse); } imgUrl = AdPhotoUrl; #if DEBUG System.Diagnostics.Debug.WriteLine($"Got image_url: {imgUrl}"); #endif // 2. Posts ad creative creating request. request.ObjectStorySpec.LinkData.Picture = imgUrl; #pragma warning disable SA1008 var(AdCreativeId, AdCreativeResponse) = await PostAdCreativeAsync(request, adAccountId, accessToken); #pragma warning restore SA1008 if (String.IsNullOrEmpty(AdCreativeId)) { return(AdCreativeResponse); } adCreative.Id = AdCreativeId; #if DEBUG System.Diagnostics.Debug.WriteLine($"Got ad_creative_id: {AdCreativeId}"); #endif if (publish) { // 3. Publishes ad creative. response = await adCreative.PublishAsync(accessToken, pageAccessToken); } return(response); }
public void CreateAdCreative() { //Act IFacebookClient client = new FacebookClient("2.9", "EAANOERzv1jEBAMcBC4tDSqpbb1AWfYZAj3BZCoifcMgm3yOADpVWmonpa8drpMyiP6JPf3UAdYbpM4j6NmBIhzIBlF2NyAid1ecKvWWTNXvM8cNWCZBleZCZA2EONXXczk4nFdKtz99NB52POJARZAc2ArQtYEIi8ZD"); //Services IAdCreativeService adsetService = new AdCreativeService(client); AdCreative creative = new AdCreative(); creative.Name = "Test API POst"; //Este nombre puede ser dado posteriormente creative.ObjectStoryId = "732881306823664_1203458439765946"; //Post que se quiere crear debe ser el mismo que esta en el unpublish var response = adsetService.Create("10155310538728783", creative); Assert.IsNotNull(response); }
/// <summary> /// Publishes video ad to facebook with a specified cover as an asynchronous operation. /// </summary> /// <param name="video"> /// The filepath of the ad video. /// </param> /// <param name="cover"> /// The url or filepath of the video cover. Sets to null or an empty string to use the thumbnail that /// generated by facebook as the cover. /// </param> /// <param name="request"> /// An object of <see cref="AdCreativeCreatingRequest"/>, which contains most of publish infomation. /// </param> /// <param name="adAccountId"> /// The ad account id of user. /// </param> /// <param name="accessToken"> /// User access token. /// </param> /// <param name="pageAccessToken"> /// Page access token. /// </param> /// <param name="publish"> /// If true, the photo ad will be published to a facebook page immediately. /// </param> /// <param name="threshold"> /// If the filesize (in MByte) of video exceeded this value, the video will be uploaded in resumable upload; /// otherwise, non-resumable upload. The default threshold is set to 50MB. /// </param> /// <returns> /// The task object representing the asynchronous operation. /// </returns> /// <exception cref="FileNotFoundException"> /// Throw if the target video or cover file doesn't exist. /// </exception> public static async Task <ResponseMessage <string> > PublishVideoAdAsync(string video, string cover, AdCreativeCreatingRequest request, string adAccountId, string accessToken, string pageAccessToken, bool publish, int threshold = 50) { ResponseMessage <string> response = null; JObject jobj = null; var videoId = String.Empty; var coverUrl = String.Empty; var adCreative = new AdCreative(); // 1. Gets the video_id. #region Uploads video to facebook. if (!File.Exists(video)) { throw new FileNotFoundException($"Cannot find file {video}"); } var videoInfo = new FileInfo(video); var videoSize = videoInfo.Length; var enableChunkedUpload = videoSize > (50L * 1024 * 1024); var videoCreatingRequest = new VideoCreatingRequest(videoInfo.Extension) { Name = videoInfo.Name, Title = Path.GetFileNameWithoutExtension(video) }; response = enableChunkedUpload ? await VideoUploader.UploadAsAdVideoInChunksAsync(video, adAccountId, accessToken, videoCreatingRequest) : await VideoUploader.UploadAsAdVideoAsync(video, adAccountId, accessToken, videoCreatingRequest); if (response.Code == ResponseCode.SUCCESS) { jobj = JObject.Parse(response.Data); if (enableChunkedUpload) { // Parses resumable upload result. if (jobj["video_id"] != null && !String.IsNullOrEmpty(jobj["video_id"].ToString())) { videoId = jobj["video_id"].ToString(); } } else { // Parses non-resumable upload result. if (jobj["id"] != null && !String.IsNullOrEmpty(jobj["id"].ToString())) { videoId = jobj["id"].ToString(); } } if (String.IsNullOrEmpty(videoId)) { return(new ResponseMessage <string> { Code = ResponseCode.UNKNOWN_ERROR, ReasonPhrase = $"Failed to upload video {video} to facebook." }); } } else { return(response); } #endregion #if DEBUG System.Diagnostics.Debug.WriteLine($"Got video_id: {videoId}"); #endif // 2. Gets the url of cover. if (!String.IsNullOrEmpty(cover)) { // Customizes cover. #pragma warning disable SA1008 var(AdPhotoUrl, AdPhotoResponse) = await GetAdPhotoUrlAsync(cover, adAccountId, accessToken); #pragma warning restore SA1008 if (String.IsNullOrEmpty(AdPhotoUrl)) { return(AdPhotoResponse); } coverUrl = AdPhotoUrl; } /* The thumbnail won't be generated until the video has been decoded by facebook successfully, * just wait for a while. * */ await Task.Delay(10_000); #pragma warning disable SA1008 var(ThumbnailUrl, ThumbnailResponse) = await GetVideoThumbnailAsync(videoId, accessToken, 20); #pragma warning restore SA1008 if (String.IsNullOrEmpty(cover)) { if (String.IsNullOrEmpty(ThumbnailUrl)) { return(ThumbnailResponse); } coverUrl = ThumbnailUrl; } #if DEBUG System.Diagnostics.Debug.WriteLine($"Got cover_url: {coverUrl}"); #endif // 3. Posts ad creative creating request. request.ObjectStorySpec.VideoData.VideoId = videoId; request.ObjectStorySpec.VideoData.Cover = coverUrl; #pragma warning disable SA1008 var(AdCreativeId, AdCreativeResponse) = await PostAdCreativeAsync(request, adAccountId, accessToken); #pragma warning restore SA1008 if (String.IsNullOrEmpty(AdCreativeId)) { return(AdCreativeResponse); } adCreative.Id = AdCreativeId; #if DEBUG System.Diagnostics.Debug.WriteLine($"Got ad_creative_id: {AdCreativeId}"); #endif if (publish) { // 4. Publishes ad creative. response = await adCreative.PublishAsync(accessToken, pageAccessToken); } return(response); }
public bool Update(string id, AdCreative model) { throw new NotImplementedException(); }