For a more in depth and other programming language samples, download the Sample Client Install

C# Copy imageCopy
using System;
using System.IO;
using System.Net;
using IWSConsoleSample.IWSServiceReference;

namespace IWSConsoleSample
{
    class GetInboundItemsByIDSampleClass
    {
        public static void GetInboundItemsByIDSample()
        {
            string username = "mbxXXXXXXXX";
            string password = "XXXX";

            ///////////////////////////////////////////////////////////
            ///OAuth 2.0 Authentication Start, 
            ///Comment this block out if you are using username and password authentication
            // Get an access token from Concord Identity Server

            string endPoint = "https://login.concord.net/v1/";
            string grantType = "password";
            string scope = "IWS";
            string IWSClientId = "ConcordIWSClient";
            string IWSClientSecret = "";

            AccessTokenRequest accessTokenRequest = null;
            AccessTokenResponse accessTokenResponse = null;
            TokenManager tokenManager = new TokenManager(endPoint);

            accessTokenRequest = new AccessTokenRequest(
                clientId: IWSClientId,
                clientSecret: IWSClientSecret,
                username: username,
                password: password,
                grantType: grantType,
                scope: scope,
                redirectUri: null,
                code: null,
                refreshToken: null,
                state: null);

            ///OAuth 2.0 Authentication End
            ///////////////////////////////////////////////////////////

            using (IWSAuthWrapper ws = new IWSAuthWrapper())
            {
                Authentication authentication = null;
                GetInboundItemsByIDRequest request = null;
                GetInboundItemsByIDResponse response = null;
                string faxFolder = @"c:\myfaxes\";

                authentication = new Authentication();
                authentication.UserId = username;

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

                // User Name and Password authentication
                 // authentication.Password = password;

                // OAuth 2.0 Authentication requires that you pass in an access token as part of the Authorization: header 
                // (No Password required)

                ws.AccessToken = accessTokenResponse.AccessToken;

                request = new GetInboundItemsByIDRequest();

                // ReqSettings not implemented
                // will be ignored if instantiated
                // request.ReqSettings;


                //Array of strings representing InboundItemId retrieved using GetInboundItemList
                request.InboundItemIDs = new string[] { 
                    "ct12065551212-20120322150106062-341-5",
                    "ct12065551212-20120322150106023-342-3" 
                    };

                try
                {
                    response = ws.GetInboundItemsByID(authentication, request);


                    if(!response.Success)
                    {
                        bool requiresNewToken = false;

                        foreach (WSError error in response.Messages)
                        {
                            Console.WriteLine("Error retrieving fax documents. Error Code: {0} Error Description: {1}", error.ErrorCode, error.ErrorDescription);

                            if (error.ErrorCode == -486 && error.ErrorDescription == "Invalid access token") requiresNewToken = true;
                        }

                        // Check to see if response indicates access token has expired, if so attempt to Refresh or Get New OAuth access token and try method call again
                        if (requiresNewToken)
                        {
                            Console.WriteLine("AccessToken is not valid");

                            // Attempt to get a new access token or use a refresh token, if we fail we should stop
                            accessTokenResponse = tokenManager.GetAccessToken(accessTokenRequest);

                            if (accessTokenResponse == null)
                            {
                                Console.WriteLine("Failed to get an AccessToken, returned null");

                                return;
                            }

                            if (accessTokenResponse.HttpStatusCode != HttpStatusCode.OK)
                            {
                                Console.WriteLine("AccessToken not valid, Error Code: {0}, Error Description: {1}", accessTokenResponse.Error, accessTokenResponse.ErrorDescription);

                                return;
                            }
                        }

                        return;
                    }

                    // Check to see if any of the faxes requested fail to be retrieved
                    if(response.FailureCount > 0)
                    {
                        foreach(var failedMessageId in response.FailedItemIDs)
                        {
                            // Get the position of the failed messageId from the items we requested
                            int messageIndex = Array.IndexOf(request.InboundItemIDs, failedMessageId);

                            // Retrieve the failure message
                            var message = response.Messages[messageIndex];

                            Console.WriteLine("Failed to retrieve messageId: {0}, Code: {1}, Description: {2}", failedMessageId, message.ErrorCode, message.ErrorDescription);
                        }
                    }

                    if(response.InboundItemCount < 1)
                    {
                        Console.WriteLine("No faxes returned");

                        return;
                    }

                    Console.WriteLine("Number of fax messages returned: " + response.InboundItemCount);
                    Console.WriteLine("");

                    foreach(InboundItem inboundItem in response.InboundItems)
                    {
                        Console.WriteLine("MessageID: " + inboundItem.Document.MessageID);
                        Console.WriteLine("---------------------------");
                        Console.WriteLine("FileName: " + inboundItem.Document.FileName);
                        Console.WriteLine("CalledNumber: " + inboundItem.Document.CalledNumber);
                        Console.WriteLine("CallerNumber: " + inboundItem.Document.CallerNumber);
                        Console.WriteLine("ContentType: " + inboundItem.Document.ContentType);
                        Console.WriteLine("ContentSubType: " + inboundItem.Document.ContentSubType);
                        Console.WriteLine("Duration: " + inboundItem.Document.Duration);
                        Console.WriteLine("NumPages: " + inboundItem.Document.NumPages);
                        Console.WriteLine("ReceivedTime: " + inboundItem.Document.ReceivedTime);
                        Console.WriteLine("RemoteCSID: " + inboundItem.Document.RemoteCSID);
                        Console.WriteLine("Resolution: " + inboundItem.Document.Resolution);
                        Console.WriteLine("Speed: " + inboundItem.Document.Speed);
                        Console.WriteLine("Status: " + inboundItem.Document.Status);
                        Console.WriteLine("");

                        try
                        {
                            File.WriteAllBytes(faxFolder + inboundItem.Document.FileName, inboundItem.Document.FileData);
                        }
                        catch(Exception ex)
                        {
                            Console.WriteLine("Error while writing file: " + ex.Message);

                            return;
                        }

                    }

                    // Example Output
                    // 
                    // Number of faxes returned: 1
                    // 
                    // MessageID: ct12065772972-20120322150106062-341-5
                    //---------------------------
                    // FileName: ct12065772972-20120322150106062-341-5.tif
                    // CalledNumber: 12065772972
                    // CallerNumber: 13129624125
                    // ContentType: fax
                    // ContentSubType: image/tiff/TIFFG4
                    // Duration: 17
                    // NumPages: 1
                    // ReceivedTime: 3/22/2012 3:01:23 PM
                    // RemoteCSID: 12065772972
                    // Resolution: 2
                    // Speed: 14400
                    // Status: 0
                    // 
                    // Finished retrieving inbound item list


                    // Note that typically following successful retrieval of a fax you need to call 
                    // RemoveInboundItemByID to remove the fax from being retrieved again. 

                    // IMPORTANT: Be sure that the request to remove items is based on the response.InboundItems 
                    // array returned and not the requested list in case any of the items failed to download

                }
                catch(Exception ex)
                {
                    Console.WriteLine("Exception: " + ex.Message);
                }

                Console.WriteLine("Finished retrieving inbound items");
            }
        }
    }
}

See Also