Skip to main content

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?

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
}



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


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


Reply