Search code examples
c#podio

How to Authenticate Podio in C#


This is my first time trying to authenticate and I can't quite get it. I am working in Visual Basic and I have installed Nuget Podio.Async.

In the code below won't authenticate with app or password.

When I remove 'async' and 'await' it seems to authenticate but then I lose 'item.Fields'.

        static void Main()
        {
            Init();
        }

        public static async void Init()
        {
            var podio = new Podio(clientId, clientSecret);
            Console.WriteLine("Client ID and Secret");

            //await podio.AuthenticateWithApp(appId, appToken);
            await podio.AuthenticateWithPassword(username, password);
            Console.WriteLine("Authenticated");

            var item = await podio.ItemService.GetItem(1124848809);
            Console.WriteLine(item.Fields.Count);
        }

Solution

  • Method Init is an async method. You are calling it inside a sync method (`main'). This is the wrong way to calling async method!

    Please try workaround below and It should solve your problems:

        static void Main()
        {
            Init().Wait();
        }
    
        public static async Task Init()
        {
            var podio = new Podio(clientId, clientSecret);
            Console.WriteLine("Client ID and Secret");
    
            //await podio.AuthenticateWithApp(appId, appToken);
            await podio.AuthenticateWithPassword(username, password);
            Console.WriteLine("Authenticated");
    
            var item = await podio.ItemService.GetItem(1124848809);
            Console.WriteLine(item.Fields.Count);
        }
    

    Update 1:

    Because main method is a sync method, I called Wait() method to make calling Init method synchronously inside main method. To make things work correctly never define an async method with void return value, I also changed the return value of Init into Task.