question

Michelle Pollack avatar image
Michelle Pollack asked Phong Vu answered

C# JWT Auth Help - "cannot convert 'string' to 'RingCentral.GetTokenRequest'"

Hihi! I'm trying to use JWT Authentication in what I think should be a simple C# script, but I'm having trouble getting my JWT to authorize and was hoping some of y'all lovely ppl might be able to help.

DISCLAIMER: I'm totally new to the RingCentral API (and fairly new to programming in general), so please excuse any incorrect terminology and feel free to correct me on anything!

I've (I think) set up my Visual Studio project correctly as described in RC's JWT Auth documentation here, but for some reason, when I try to authorize the JWT, Visual Studio throws me an error at line 17, telling me it "cannot convert from 'string' to 'RingCentral.GetTokenRequest", and of course then refuses to compile.

I'm struggling to understand why this is happening, though, being that it's verbatim RC's example code, unless I did something wrong setting up the project? I don't know enough about the "GetTokenRequest" type to properly diagnose this, but I don't see how I could define the JWT as anything other than a string since it's just text.

Again, I'm still very new to programming, so it's entirely possible I'm just missing something super obvious.

I've included the code I'm working with (ripped directly from the link above).

Any help would be greatly appreciated!!!!


Current C# Script:

using System;
using System.Threading.Tasks;
using RingCentral;
 
namespace JWT_Auth
{
    class Program
    {
        static RestClient restClient;
        static void Main(string[] args)
        {
            restClient = new RestClient(
                 Environment.GetEnvironmentVariable("RC_CLIENT_ID"),
                 Environment.GetEnvironmentVariable("RC_CLIENT_SECRET"),
                 Environment.GetEnvironmentVariable("RC_SERVER_URL"));
             restClient.Authorize(
                 Environment.GetEnvironmentVariable("RC_JWT")).Wait();
                 Console.WriteLine("Successfully authed.");
        }
    }
}

Current .env file:

# Sandbox
#RC_SERVER_URL       = 'https://platform.devtest.ringcentral.com'
# Production
RC_SERVER_URL        = 'https://platform.ringcentral.com'
RC_CLIENT_ID         = 'MY_CLIENT_ID'
RC_CLIENT_SECRET     = 'MY_CLIENT_SECRET'
 
# This credential is used for JWT-grant types
RC_JWT               = 'MY_JWT'

Again, any help or advice is welcome!!

ringcentral apprest apisdkauthenticationget token
1 |3000

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

1 Answer

Phong Vu avatar image
Phong Vu answered

Can you change your code like this

// I don't know how you load the .env file. But you should have this in your project
using dotenv.net;

namespace JWT_Auth
{
    class Program
    {
        static RestClient restClient;
        static void Main(string[] args)
        {
            DotEnv.Load();
            restClient = new RestClient(
                 Environment.GetEnvironmentVariable("RC_CLIENT_ID"),
                 Environment.GetEnvironmentVariable("RC_CLIENT_SECRET"),
                 Environment.GetEnvironmentVariable("RC_SERVER_URL"));
            await restClient.Authorize(Environment.GetEnvironmentVariable("RC_JWT"
            Console.WriteLine("Successfully authed.");
        }
    }
}

Better see the entire sample code here


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