ringcentral / RingCentral.Net

RingCentral SDK for .NET
MIT License
19 stars 26 forks source link

Access Tokens get revoked when RestClient is disposed preventing future use #37

Closed ahawes-clarity closed 3 years ago

ahawes-clarity commented 3 years ago

I need to use the recommended 3-legged Auth Code Flow.

I am using RingCentral.Net and the Authorize method with a GetTokenRequest passing in the Authorization Code and Redirect Uri.

I get back an Access Token (TokenInfo object) and I store it for later use.

However, when I go to use it later, 99% of the time the request fails because the Token is not found or said to be corrupt.

When I was looking at the SDK code today I noticed that when the RestClient is disposed it seems to call the Revoke method which seems to call the oauth/revoke endpoint of the RingCentral API. This would seem to have the affect of revoking the token.

Is this the case?

How can I use a stored Access Token if the RestClient will always revoke the token?

image

ahawes-clarity commented 3 years ago

A work around that seems to work for me is to set the RestClient Token to null before the RestClient is disposed. This seems to have the effect of removing the token the would be revoked which prevents it from being revoked.

Like this:

using var rc = new RestClient(ClientId, ClientSecret, ServerURL);

var getTokenRequest = new GetTokenRequest
{
    grant_type = "authorization_code",
    code = authorizationCode,
    redirect_uri = redirectUri.AbsoluteUri
};
await rc.Authorize(getTokenRequest);

var token = rc.token;

// Save Token for future use....

// This sets the token to null before rc is disposed preventing the access token from being revoked.
rc.token = null;

return
tylerlong commented 3 years ago

I have updated the docs: https://github.com/ringcentral/RingCentral.Net#c-using-statement

In short, do not use using if you want to retain the token.

If you don't use using, Dispose won't be invoked at all. And do not invoke Dispose in your code.

I removed all using from sample code: https://github.com/ringcentral/RingCentral.Net/blob/master/samples.md