public async Task PublishVieoAdWithCustomCoverAsync() { var request = new AdCreativeCreatingRequest { ObjectStorySpec = new ObjectStorySpec { PageId = TestsBase.PageId, VideoData = new VideoData { Call2Action = new Call2Action { Type = Call2ActionType.LEARN_MORE, Value = new Call2ActionValue { Link = TestsBase.Link } }, Headline = "Headline of Video Ad With Custom Cover", Message = Guid.NewGuid().ToString("N") } } }; var response = await AdCreativePublisher.PublishVideoAdAsync(TestsBase.TestVideoPath, TestsBase.TestPicturePath, request, TestsBase.AdAccountId, TestsBase.AccessToken, TestsBase.PageAccessToken, true); Assert.IsTrue(response.Code == ResponseCode.SUCCESS, response.ReasonPhrase ?? String.Empty); var jobj = JObject.Parse(response.Data); Assert.IsTrue(jobj["success"] != null && Boolean.Parse(jobj["success"].ToString()) == true); }
public async Task PublishPhotoAdAsync() { var request = new AdCreativeCreatingRequest { ObjectStorySpec = new ObjectStorySpec { PageId = TestsBase.PageId, LinkData = new LinkData { Call2Action = new Call2Action { Type = Call2ActionType.LEARN_MORE }, Caption = "LARY.ME", Description = "Description of photo ad.", Headline = "Headline of Photo Ad", Link = TestsBase.Link, Message = Guid.NewGuid().ToString("N") } } }; var response = await AdCreativePublisher.PublishPhotoAdAsync(TestsBase.TestPicturePath, request, TestsBase.AdAccountId, TestsBase.AccessToken, TestsBase.PageAccessToken, true); Assert.IsTrue(response.Code == ResponseCode.SUCCESS, response.ReasonPhrase ?? String.Empty); var jobj = JObject.Parse(response.Data); Assert.IsTrue(jobj["success"] != null && Boolean.Parse(jobj["success"].ToString()) == true); }
public async Task DraftVideoAdAsync() { var request = new AdCreativeCreatingRequest { ObjectStorySpec = new ObjectStorySpec { PageId = TestsBase.PageId, VideoData = new VideoData { Call2Action = new Call2Action { Type = Call2ActionType.LEARN_MORE, Value = new Call2ActionValue { Link = TestsBase.Link } }, Headline = "Headline of Video Ad With default Cover", Message = Guid.NewGuid().ToString("N") } } }; var response = await AdCreativePublisher.PublishVideoAdAsync(TestsBase.TestVideoPath, request, TestsBase.AdAccountId, TestsBase.AccessToken, TestsBase.PageAccessToken, false); Assert.IsTrue(response.Code == ResponseCode.SUCCESS, response.ReasonPhrase ?? String.Empty); var jobj = JObject.Parse(response.Data); Assert.IsTrue((jobj["video_id"] != null && !String.IsNullOrEmpty(jobj["video_id"].ToString())) || (jobj["id"] != null && !String.IsNullOrEmpty(jobj["id"].ToString()))); }
/// <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); }
/// <summary> /// Posts an ad creative creating request to facebook with user's ad account id as an asynchronous /// operation. /// </summary> /// <param name="request"> /// An object of <see cref="AdCreativeCreatingRequest"/>. /// </param> /// <param name="adAccountId"> /// The ad account id of user. /// </param> /// <param name="accessToken"> /// User access token. /// </param> /// <returns> /// The task object representing the asynchronous operation. /// </returns> private static async Task <(string AdCreativeId, ResponseMessage <string> AdCreativeResponse)> PostAdCreativeAsync(AdCreativeCreatingRequest request, string adAccountId, string accessToken) { var adCreativeId = String.Empty; ResponseMessage <string> response = null; JObject jobj = null; response = await request.PostAsync(adAccountId, accessToken); if (response.Code == ResponseCode.SUCCESS) { jobj = JObject.Parse(response.Data); if (jobj["id"] != null && !String.IsNullOrEmpty(jobj["id"].ToString())) { adCreativeId = jobj["id"].ToString(); } else { response = new ResponseMessage <string> { Code = ResponseCode.UNKNOWN_ERROR, ReasonPhrase = $"Failed to post ad creative creating request." }; } } return(adCreativeId, 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); }
/// <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="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, AdCreativeCreatingRequest request, string adAccountId, string accessToken, string pageAccessToken, bool publish, int threshold = 50) { return(await PublishVideoAdAsync(video, null, request, adAccountId, accessToken, pageAccessToken, publish, threshold)); }