【问题标题】:How to Authenticate Podio in C#如何在 C# 中验证 Podio
【发布时间】:2019-05-11 03:54:25
【问题描述】:

这是我第一次尝试进行身份验证,但我不太明白。我正在使用 Visual Basic 并安装了 Nuget Podio.Async。

在下面的代码中不会使用应用程序或密码进行身份验证。

当我删除“异步”和“等待”时,它似乎通过了身份验证,但随后我丢失了“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);
        }

【问题讨论】:

    标签: c# podio


    【解决方案1】:

    方法Init 是一个async 方法。您在同步方法(`main')中调用它。这是调用异步方法的错误方法!

    请尝试以下解决方法,它应该可以解决您的问题:

        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);
        }
    

    更新 1:

    因为main方法是同步方法,所以我调用Wait()方法是为了在main方法内部同步调用Init方法。为了让事情正常工作never 定义了一个带有void 返回值的异步方法,我还将Init 的返回值更改为Task

    【讨论】:

    • 我在“等待”下出现了一条红线——“任务不包含等待的定义”
    • 由于某种原因,它说“异步的返回类型必须是无效的”。我正在使用视觉工作室。
    • 哦,它说异步方法必须返回 void OR 任务。但他们都没有工作。
    • 您引用了System.Threading.Tasks 吗? using System.Threading.Tasks
    【解决方案2】:

    事实证明,从 7.1 版开始,您现在可以使用异步 Main 方法。这段代码对我有用:

            static async Task<int> Main()
            {
                var podio = new Podio(clientId, clientSecret);
                await podio.AuthenticateWithPassword(username, password);
    
                var item = await podio.ItemService.GetItem(1124848809);
                Console.WriteLine(item.Fields.Count);
    
                return 0;
            }
    

    【讨论】:

    • 我告诉过你与调用async方法的方式有关的问题。
    • 好的,谢谢。我将您的答案标记为已接受。这是我在这里的第一个问题。还在学习协议。再次感谢!
    • 没关系@Xiolin,Stackoverlow 是程序员的家。让自己舒服
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多