box-apiboxapiv2

BoxV2 .Net API RefreshAccessTokenAsync() method throws Object reference Error


I am using BoxV2 .Net SDK. When I attempt refresh AcccessToken, I get an Object reference not set to an instance of an object error. There is no inner exception details. Below line of code is throwing error. Can anyone advice what is wrong with the call?

await client.Auth.RefreshAccessTokenAsync("my-refresh-token");

Below is the code snippet wihch I use to get the tokens first time.

var config = new BoxConfig("client-id", "client-secret", new Uri("return-url"));
var client = new BoxClient(config);

Making a request to config.AuthCodeUri.ToString() to get the one-time-code for authentication

OAuthSession session = await client.Auth.AuthenticateAsync("one-time-code"); 
string refreshToken = session.RefreshToken;

Any help will be appreciated, thanks in advance.


Solution

  • I found the solution. The BoxClient requires a valid OAuthSession to function properly. So I had to use the overloaded constructor of BoxClient which accepts OAuthSession session parameter. So I modified the code as below and it worked flawlessly.

    var session = new OAuthSession("access-token", "refresh-token", "expires-in", "token-type");
    var config = new BoxConfig("client-id", "client-secret", new Uri("return-url"));
    var client = new BoxClient(config, session);