Trimble Connect User Forum

 View Only

 Problem with getProjects after Authorization

Kamil Sobania's profile image
Kamil Sobania posted 04-10-2024 02:50

Hello all.

So, I have some properties.

private string _tcEmailAddress;
private string _tcApiKey;
private string _tcApiToken;

public List<TcProject> Projects;
private List<string> _projectNames;

private const string ProjectName = "...";

private const string ServiceUriEu = "https://app21.connect.trimble.com/tc/api/2.0";
private const string AuthorityUri = AuthorityUris.ProductionUri;
private const string TokenUri = "https://id.trimble.com/oauth/token";
private static readonly Uri RedirectUri = new Uri("http://localhost/");

private static readonly ClientCredential ClientCredential = new ClientCredential(
    clientId: ClientId, 
    clientSecret: ClientSecret, 
    name: ProjectName
    )
{
        RedirectUri = RedirectUri
};

readonly static string[] Scopes = new string[] { ClientCredential.Name };
TrimbleConnectClient client;


And here how i handle with auth in sign in function:

private async void GetAuthorizationToken(string emailAddress) {
    TokenCache.DefaultShared.Clear();
            
    var authContext = new AuthenticationContext(ClientCredential, new TokenCache())
    {
        AuthorityUri = new Uri(AuthorityUri),
    };

     try
    {
        AuthCodeCredentialsProvider provider = new AuthCodeCredentialsProvider(authContext);
        provider.AuthenticationRequest = new InteractiveAuthenticationRequest()
        {
            Scope = $"openid {string.Join(" ", Scopes)}"
        };
                
         var config = new TrimbleConnectClientConfig 
        { 
            ServiceURI = new Uri(ServiceUriEu),
        };
                
        client = new TrimbleConnectClient(config, provider);
        using (client)
        {
            await client.InitializeTrimbleConnectUserAsync();
        }

            var token = await authContext.AcquireTokenAsync(); 
            _tcApiToken = token.IdToken;
    }
    catch (Exception e)
    {...}
}


And now, the issue is that when downloading projects in the new funtion either there is an error or it does not download projects.

public async void GetProjectsList(){
    Projects = new List<TcProject>();

    if (_tcApiToken == "") return;
    try
    {
        using (client)
        {
            await client.InitializeTrimbleConnectUserAsync(); // HERE IS ERROR, WITHOUT THIS LINE OF CODE PROJECTS ARE NOT DOWNLOAD
            var projects = (await client.GetProjectsAsync()).ToArray();
            foreach (var p in projects)
            {
                var project = new TcProject {...};
                Projects.Add(project);
            }
        }
    } catch (Exception e)
    {...}
}

The error is


System.ObjectDisposedException: Unable to access deleted object.
Object name: 'System.Net.Http.HttpClient'.
   in System.Net.Http.HttpClient.CheckDisposed()
   w System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
   In Trimble.Connect.Client.Common.Http.HttpRequestBuilder.<SendAsync>d__20.MoveNext()

 
--- The end of the stack trace from the previous location where the exception occurred ---.


[...]

--- The end of the stack trace from the previous location where the exception occurred ---.

   in System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0(Object state)

To make it more fun, when the GetProjectList() function for projects is in the GetAuthorizationToken() function it all works. However, the project is in Windows Forms and wants these functions to be separate.

Is it possible to do something about this? How should I handle this problem?