For a more in depth and other programming language samples, download the Sample Client Install
C# | ![]() |
---|---|
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.IO; using System.Net; using System.Net.Http; using System.Text; namespace FaxWSConsoleApplication { class SendFaxBroadcastJsonClass { const string oauthUrl = "https://login.concord.net/v1/connect/token"; const string faxWSUrl = "https://ws.concordfax.com/fax/v5"; public void SendFaxBroadcastJson() { string methodName = "SendFaxBroadcast"; Console.WriteLine($"Starting call to {methodName}"); #region General Settings // Fill in with username string username = "c00XXXXXXXX"; // FB Id. // Fill in with password string password = ""; // Fill in with fax number string faxNumber = ""; // Fill in path to tiff file to send string tiffFileToSend = "c:\\concord-test.tif"; #endregion General Settings #region OAuth 2.0 Settings string grantType = "password"; string scope = "FaxWS"; string faxWSClientId = "A9F528F9-0000-4B70-0000-078C23030424-ConcordApiFaxWS"; string faxWSClientSecret = ""; #endregion OAuth 2.0 Setup try { #region OAuth 2.0 Authentication AccessTokenRequest accessTokenRequest = null; AccessTokenResponse accessTokenResponse = null; TokenManager tokenManager = new TokenManager(oauthUrl); accessTokenRequest = new AccessTokenRequest( clientId: faxWSClientId, clientSecret: faxWSClientSecret, username: username, password: password, grantType: grantType, scope: scope, redirectUri: null, code: null, refreshToken: null, state: null); Console.WriteLine("Getting access token"); // Get an access token from Cache OR request one from the Identity Server accessTokenResponse = tokenManager.GetAccessToken(accessTokenRequest); if(accessTokenResponse == null) { Console.WriteLine("Failed to get an AccessToken, returned null"); return; } // Check to see if we failed to get an Access token via a Refresh token that has expired or been revoked, if so we need to re-authenticate if(accessTokenResponse.HttpStatusCode == HttpStatusCode.BadRequest && accessTokenResponse.Error == "invalid_grant") { Console.WriteLine("Refresh token expired, attempting to get a new Access Token"); accessTokenResponse = tokenManager.GetAccessToken(accessTokenRequest, ignoreCache: true); } if(accessTokenResponse.HttpStatusCode != HttpStatusCode.OK) { Console.WriteLine("AccessToken not valid, Error Code: {0}, Error Description: {1}", accessTokenResponse.Error, accessTokenResponse.ErrorDescription); return; } Console.WriteLine("Access token retrieved sucessfully"); #endregion OAuth 2.0 Authentication #region Request Setup // Sample Request //{ // "SendFaxBroadcast": { // "UserID": "", // "strPIN": "", // "Recipients": [{ // "RecipFaxNumber": "", // "RecipCompany": "", // "RecipTitle": "", // "RecipName": "", // "RecipSecureCSID": "", // "RecipFirstName": "", // "RecipLastName": "", // "RecipAddress1": "", // "RecipAddress2": "", // "RecipCity": "", // "RecipState": "", // "RecipZipcode": "", // "RecipCountry": "", // "RecipVoiceNumber": "", // "RecipField1": "", // "RecipField2": "", // "RecipField3": "", // "RecipField4": "", // "RecipField5": "", // "RecipField6": "", // "RecipField7": "", // "RecipField8": "", // "RecipField9": "", // "RecipField10": "", // "RecipField11": "", // "RecipField12": "" // }], // "FaxJobFiles": [{ // "FileIndex": 0, // "FileTypeId": 0, // "FileData": "BASE64ENCODEDFILEDATA" // }], // "JobDetails": { // "JobScheduleStartType": 0, // "JobScheduleStartDate": "", // "JobExpiryDate": "", // "SenderCSID": "", // "SenderName": "", // "SenderCompany": "", // "SenderPhone": "", // "SenderAddress1": "", // "SenderAddress2": "", // "SenderCity": "", // "SenderState": "", // "SenderZip": "", // "SenderCountry": "", // "CoverText": "", // "CoverName": "", // "CoverSubject": "", // "Resolution": "", // "SenderFax": "", // "SoftwareClient": "", // "ExceptionDestination": "", // "UserField1": "", // "UserField2": "", // "UserField3": "", // "UserField4": "", // "UserField5": "", // "UserField6": "", // "UserField7": "", // "UserField8": "", // "UserField9": "", // "UserField10": "", // "UserField11": "", // "UserField12": "" // } // } //} // Create JSON request var json = new { SendFaxBroadcast = new { UserID = username, strPIN = "", // Do not set password as using OAUTH Recipients = new[] { new { RecipFaxNumber = faxNumber } }, FaxJobFiles = new[] { new { FileIndex = 1, FileTypeId = 1, // 1: Tiff See documentation for different file types FileData = File.ReadAllBytes(tiffFileToSend) } }, JobDetails = new { JobScheduleStartType = 1 } } }; // Serialize to JSON string {MethodName:{}} string content = JsonConvert.SerializeObject(json); // Create HttpContent object from JSON content and set content-type to application/json HttpContent httpContent = new StringContent(content, Encoding.UTF8, "application/json"); #endregion Request Setup #region API Call using(var client = new HttpClient()) { // Clear any existing headers client.DefaultRequestHeaders.Accept.Clear(); // Add Authorization Bearer OAuth 2.0 Token to header client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessTokenResponse.AccessToken); // Add Content-Type: application/json client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); Console.WriteLine($"Making API Call to {methodName}"); // Sent POST request HttpResponseMessage httpResponseMessage = client.PostAsync(new Uri(faxWSUrl), httpContent).GetAwaiter().GetResult(); Console.WriteLine("Reading API Call Response content"); // Get JSON body content from response string responseContent = httpResponseMessage.Content.ReadAsStringAsync().GetAwaiter().GetResult(); // Check to see if Http Status Code was successful if(!httpResponseMessage.IsSuccessStatusCode) { Console.WriteLine($"Http Error Returned, Status Code: {httpResponseMessage.StatusCode}, Reason Phrase: {httpResponseMessage.ReasonPhrase}"); Console.WriteLine($"Response Content:{responseContent}"); return; } JObject jsonResult = null; try { // Parse JSON string to Json Object that will help parse response jsonResult = JObject.Parse(responseContent); } catch(JsonReaderException ex) { Console.WriteLine(ex.Message); } // Write JSON response out Console.WriteLine($"Json Response: {jsonResult.ToString()}"); // SAMPLE RESPONSE //{ // "SendFaxBroadcastResponse": { // "return": false, // "FaxJobId": "", // "WSError": { // "ErrorCode": 0, // "ErrorString": "" // } // } //} // Get response portion // Serialize to JSON string {SendFaxBroadcastResponse:{}} var response = jsonResult["SendFaxBroadcastResponse"]; // Get API call status bool success = response["return"].Value<bool>(); // See if method call was considered successful // If not then output detail from WSError object and RAW JSON response if(!success) { var wsError = response["WSError"]; var errorCode = wsError["ErrorCode"].Value<int>(); var errorString = wsError["ErrorString"].Value<string>(); Console.WriteLine($"ErrorCode: {errorCode}, ErrorString:{errorString}"); return; } Console.WriteLine($"Successfully sent fax"); var faxJobId = response["FaxJobId"]; Console.WriteLine($"Successfully submitted fax broadcast FaxJobId: {faxJobId}"); } #endregion API Call } catch(Exception ex) { Console.WriteLine($"Error while calling {methodName} : {ex.Message}"); } finally { Console.WriteLine($"Finished call to {methodName}"); } } } } |