question

Jesse Merriman avatar image
Jesse Merriman asked ·

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?

authorizationc#sdktoken
1 |1000 characters needed characters left characters exceeded

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
}


Share
1 |1000 characters needed characters left characters exceeded

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.

Share
1 |1000 characters needed characters left characters exceeded

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 ·

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 Share
1 |1000 characters needed characters left characters exceeded

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

You are welcome!

0 Likes 0 · ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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