question

Jesse Merriman avatar image
Jesse Merriman asked Phong Vu commented

Re-authorize RestClient using token with .Net SDK

I am having trouble finding a way to authorize a user using an access token with the re-written RingCentral.NET sdk.

In every example I have found in the documentation, it requires you to authorize the RestClient with a username and password. I would much rather authorize with the password once, store the access token, and then handle token refreshes and authorization by passing in the access token.

For api calls (i.e. RingOut) there is no option to pass an access token as a parameter.

As an example scenario, if someone in my app authorizes their account with a username and password, I encrypt and store the access token. If they for some reason close my app and re-open it within a short time period, I cannot re-authorize the app using the decrypted token that I have stored. I have to pass in the username and password again to get a new token.

It seems to me that there is really no point in having a token when using this SDK, unless I am missing something.

Anybody know how I can accomplish this without storing the user credentials?

sdk
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 answered

After the first time you authorized a user successfully, you will get a TokenInfo object with all valid values (access token, refresh token etc)

You can then save the token info in the app (a global variable) and use it every time you want to call an API

static TokenInfo token = null;

static private async Task reuse_token()
{
  RestClient restClient = new RestClient("CLIENTID", "CLIENTSECRET", false);
  if (tokens == null)
  {
    Console.WriteLine("get new token");
    await restClient.Authorize("USERNAME", "EXTENSION", "PASSWORD");
    token = restClient.token;
  }
  else
  {
    Console.WriteLine("reuse token");
    restClient.token = token;
  }
  // use the restClient object to call APIs
}

Or you can deserialize it and save it in your database. The next time you want to use it, just read it and serialize to a TokenInfo object then set to the restClient class

static string tokenString = "";

static private async Task reuse_token()
{
  RestClient restClient = new RestClient("CLIENTID", "CLIENTSECRET", false);
  if (tokenString == "")
  {
    Console.WriteLine("get new tokens");
    await restClient.Authorize("USERNAME", "EXTENSION", "PASSWORD");
    tokenString = JsonConvert.SerializeObject(restClient.token);
  }
  else
  {
    Console.WriteLine("reuse tokens string");
    var tokenClass = JsonConvert.DeserializeObject<TokenInfo>(tokenString);
    restClient.token = tokenClass;
  }
  // use the restClient object to call APIs
}


1 |3000

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

Jesse Merriman avatar image
Jesse Merriman answered

Thank you for the quick response. I will try this out now.

1 |3000

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

Jesse Merriman avatar image
Jesse Merriman answered Phong Vu commented

This worked perfectly. It appears that an underlying problem was that I was not serializing the token using JsonConvert before I encrypted and stored it.

Problem Solved! Thank you @Phong Vu

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 are welcome!

0 Likes 0 ·

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