News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Basic C# example not working.
Tags: rest api
Feb 17, 2022 at 1:30pm   •   6 replies  •  0 likes
William Scheffey

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.

6 Answers
answered on Mar 29, 2024 at 9:46am  

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


 0
answered on Mar 29, 2024 at 9:34am  

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


 0
answered on Mar 29, 2024 at 9:31am  

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;

     }
 }

 0
answered on Mar 28, 2024 at 3:12pm  
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.


 0
on Mar 28, 2024 at 3:27pm   •  0 likes

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"
            );
answered on Mar 28, 2024 at 9:28pm  

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

 0
on Mar 29, 2024 at 7:28am   •  0 likes

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>"));
answered on Mar 28, 2024 at 6:51am  

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

1711633870057.png


 0
on Mar 28, 2024 at 7:38am   •  0 likes

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



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us