question

William Scheffey avatar image
William Scheffey asked Jason Jones answered

Basic C# example not working.

Hello, first time user here. I'm trying to test the most basic code sample for C# but it isn't working and it's throwing a very vague exception. Code and exception below. I replaced the sensitive variables with comments describing what they had been set to.

using System;
using System.Threading.Tasks;
using RingCentral;
using DotNetEnv;

namespace Call_Ringout
{
    class Program
    {
        static RestClient restClient;
        static void Main(string[] args)
        {
            restClient = new RestClient(
                /*Client ID*/,
                /*Client Secret*/,
                "https://platform.devtest.ringcentral.com");
            restClient.Authorize(
                /*Username*/,
                /*Extension*/,
                /*Password*/;
            call_ringout().Wait();
        }

        static private async Task call_ringout()
        {
            var parameters = new MakeRingOutRequest();
            parameters.from = new MakeRingOutCallerInfoRequestFrom
            {
                phoneNumber = /*From Number*/
            };
            parameters.to = new MakeRingOutCallerInfoRequestTo
            {
                phoneNumber = /*To Number*/
            };
            parameters.callerId = new MakeRingOutCallerInfoRequestTo
            {
                phoneNumber = /From Number/
            };
            parameters.country = new MakeRingOutCoutryInfo
            {
                id = "1"
            };
            parameters.playPrompt = false;
            //var resp = await restClient.Restapi().Account().Extension().RingOut().Post(parameters);
            //Console.WriteLine("Call Placed. Call status" + resp.status.callStatus);

            var _1 = restClient.Restapi();
            var _2 = _1.Account();
            var _3 = _2.Extension();
            var _4 = _3.RingOut();
            try
            {
                var resp = await _4.Post(parameters);
                Console.WriteLine("Call Placed. Call status" + resp.status.callStatus);
            }
            catch(Exception e)
            {
                Console.WriteLine(e.InnerException);
                Console.WriteLine(e.Message);
                Console.WriteLine(e.ToString());
            }
        }
    }
}

I broke up the line

var resp = await restClient.Restapi().Account().Extension().RingOut().Post(parameters);

Into its component parts to see where the problem is. It is in the last part, which is why I put the try block around it alone.

Here is the error I'm getting:

Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
   at RingCentral.RestClient.Request(HttpRequestMessage httpRequestMessage, Int32 retriedTimes, Nullable`1 cancellationToken)
   at RingCentral.RestClient.Request(HttpMethod httpMethod, String endpoint, Object content, Object queryParams, Nullable`1 cancellationToken)
   at RingCentral.RestClient.Request[T](HttpMethod httpMethod, String endpoint, Object content, Object queryParams, Nullable`1 cancellationToken)
   at RingCentral.RestClient.Post[T](String endpoint, Object content, Object queryParams, Nullable`1 cancellationToken)
   at RingCentral.Paths.Restapi.Account.Extension.RingOut.Index.Post(MakeRingOutRequest makeRingOutRequest, Nullable`1 cancellationToken)
   at Call_Ringout.Program.call_ringout() in C:\Users\wscheffey\source\repos\Call_Ringout\Call_Ringout\Program.cs:line 55

C:\Program Files\dotnet\dotnet.exe (process 28572) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Any help greatly appreciated.

rest api
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Jason Jones avatar image
Jason Jones answered Phong Vu commented

I am having the same issue when doing a calllog, has anybody found a solution to this ?

1711633870057.png


1711633870057.png (32.5 KiB)
1 comment
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image Phong Vu ♦♦ commented ·

I don't see how you create the query params so I cannot say what could be wrong. But get the latest .NET SDK and try this code.

using System;
using System.IO;
using System.Threading.Tasks;
using System.Collections.Generic;
using RingCentral;
using Newtonsoft.Json
namespace Read_User_CallLog
{
  class Program
  {
    static RestClient restClient;
    static void Main(string[] args)
    {
      try
      {
        DotEnv.Load();
        // Instantiate the SDK
        restClient = new RestClient(
                "RC_CLIENT_ID",
                "RC_CLIENT_SECRET",
                "RC_SERVER_URL");

        // Authenticate a user using a personal JWT token
        await restClient.Authorize( "RC_JWT" );

        await read_user_calllog();
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
      }
    }
    /*
    * Read user call log between a period of time
    */
    static private async Task read_user_calllog()
    {
      try
      {
        var queryParams = new ReadUserCallLogParameters();
        queryParams.dateFrom = "2024-01-01T00:00:00.000Z";
        queryParams.dateTo = "2024-01-31T23:59:59.009Z";
        queryParams.view = "Detailed";

        var resp = await restClient.Restapi().Account().Extension().CallLog().List(queryParams);
        foreach (CallLogRecord record in resp.records)
        {
          Console.WriteLine(JsonConvert.SerializeObject(record, Formatting.Indented));
        }
      }
      catch (Exception ex)
      {
        Console.WriteLine("Cannot read user call log data. " + ex.Message);
      }
    }
  }
}
0 Likes 0 ·
Jason Jones avatar image
Jason Jones answered Phong Vu commented
using Newtonsoft.Json;
using RingCentral;
using System;
using System.Threading.Tasks;

namespace Read_User_CallLog
{
    class Program
    {
        static RestClient restClient;
        static async Task Main(string[] args)
        {
            try
            {
                // Instantiate the SDK
                RestClient restClient = new RestClient(
                    "<myclientid>",
                    "<mysecret>",
                    "https://dummy.com"
                );
                // Authenticate a user using a personal JWT token
                // I have to use ConfigureAwait instead of wait cause it will 
                //just never return with wait.

                restClient.Authorize("<MyJWT>").ConfigureAwait(false);
                await read_user_calllog(
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        
        static private async Task read_user_calllog()
        {
            try
            {
                var queryParams = new ReadUserCallLogParameters();
                queryParams.dateFrom = "2024-01-01T00:00:00.000Z";
                queryParams.dateTo = "2024-01-31T23:59:59.009Z";
                queryParams.view = "Detailed";

                var resp = await restClient.Restapi().Account().Extension().CallLog().List(queryParams);

                foreach (CallLogRecord record in resp.records)
                {
                    Console.WriteLine(JsonConvert.SerializeObject(record, Formatting.Indented));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Cannot read user call log data. " + ex.Message);
            }
        }
    }
}

1711663637580.png

Thank you for replying. My code was in a .net API. I changed it to a console app but still getting the same issue, any help would be welcome.


1711663637580.png (16.9 KiB)
1 comment
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image Phong Vu ♦♦ commented ·

You declared the static RestClient restClient in your class, then you declare a local restClient????

// Instantiate the SDK
RestClient restClient = new RestClient(
                    "<myclientid>",
                    "<mysecret>",
                    "https://dummy.com"
             );

Remove it!

// Instantiate the SDK
restClient = new RestClient(
                    "<myclientid>",
                    "<mysecret>",
                    "https://dummy.com"
            );
0 Likes 0 ·
Jason Jones avatar image
Jason Jones answered Phong Vu commented

I still get the same error message

1711686390458.png


class Program
{
    static RestClient restClient = new RestClient(
          "<myclientid>",
                    "<mysecret>",
                    "https://dummy.com"
    );
    static async Task Main(string[] args)
    {
        try
        {
            // Authenticate a user using a personal JWT token
             restClient.Authorize("<MyJWT>").ConfigureAwait(false);

            await read_user_calllog();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    /*
     * Read user call log between a period of time
     */
    static private async Task read_user_calllog()
    {
        try
        {
            var queryParams = new ReadUserCallLogParameters();
            queryParams.dateFrom = "2024-01-01T00:00:00.000Z";
            queryParams.dateTo = "2024-01-31T23:59:59.009Z";
            queryParams.view = "Detailed";

            var resp = await restClient.Restapi().Account().Extension().CallLog().List(queryParams);
            foreach (CallLogRecord record in resp.records)
            {
                Console.WriteLine(JsonConvert.SerializeObject(record, Formatting.Indented));
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Cannot read user call log data. " + ex.Message);
        }
    }

1711686390458.png (23.3 KiB)
1 comment
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image Phong Vu ♦♦ commented ·

It's not the same error. Before, the restClient was null.

What is the reason you call the authorize this way?

restClient.Authorize("<MyJWT>").ConfigureAwait(false);

Try this to see if it helps

await restClient.Authorize("<MyJWT>"));
0 Likes 0 ·
Jason Jones avatar image
Jason Jones answered

I changed everything to be await and I am now able to get records back, however it is only returning calls I have made. I am an admin on our account. Please help me with this.


 class Program
 {

     static RestClient restClient = new RestClient(
         "<myclientid>",
         "<mysecret>", true
     );
     static async Task Main(string[] args)
     {
         try
         {
             // Authenticate a user using a personal JWT token
             await restClient.Authorize("<MyJWT>");

             var ringCallLogResponse = await read_user_calllog();
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
         }
     }

     static private async Task<CallLogResponse> read_user_calllog()
     {
         CallLogResponse callLog = new CallLogResponse();
         try
         {
             var queryParams = new ReadUserCallLogParameters();
             queryParams.dateFrom = "2024-01-01T00:00:00.000Z";
             queryParams.dateTo = "2024-01-31T23:59:59.009Z";
             queryParams.view = "Detailed";

             callLog = await restClient.Restapi().Account().Extension().CallLog().List(queryParams);

             foreach (CallLogRecord record in callLog.records)
             {
                 Console.WriteLine(JsonConvert.SerializeObject(record, Formatting.Indented));
             }
         }
         catch (Exception ex)
         {
             Console.WriteLine("Cannot read user call log data. " + ex.Message);
         }

         return callLog;

     }
 }
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Jason Jones avatar image
Jason Jones answered

When I run it at https://developers.ringcentral.com/api-reference/Call-Log/readCompanyCallLog. I get back 99 pages with 100 records per page.

1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Jason Jones avatar image
Jason Jones answered

I removed the .Extension() and changed ReadUserCallLogParameters to ReadCompanyCallLogParameters and it works. Thank you for you help.

1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys