【问题标题】:OneDrive SDK showing empty White window for OAuthOneDrive SDK 显示 OAuth 的空白白色窗口
【发布时间】:2017-06-06 01:06:05
【问题描述】:

我正在针对Microsoft.OneDrive.SDK 进行编码,并且我还包含了Microsoft.OneDrive.SDK.Authentication。我遇到的问题是我有一个控制台应用程序,它只打开一个白色窗口,它清楚地提示我登录。

代码:

using System;
using System.Threading.Tasks;
using Microsoft.OneDrive.Sdk;
using Microsoft.OneDrive.Sdk.Authentication;

namespace SecondTake_OneDrive_SDK
{
class Program
{
    [STAThread]
    static void Main()
    {
        RunTask().Wait();
    }

    static async Task RunTask(OneDriveClient oneDriveClient = null)
    {
        Task authTask = null;
        if (oneDriveClient == null)
        {
            var msaAuthenticationProvider = new MsaAuthenticationProvider("CLIENT_ID",null, "https://login.live.com/oauth20_desktop.srf", new[] { "onedrive.readonly", "wl.signin" }, null, new CredentialVault("CLIENT_ID));
            authTask =  msaAuthenticationProvider.AuthenticateUserAsync();
        }
        try
        {
            await authTask;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
}

【问题讨论】:

  • 在调试应用程序时,如果您在if( oneDriveClient == null) 行上放置一个断点 - oneDriveClient 仍然为空吗?我想它不是。因此,程序将跳过所有代码并跳转到它下面的 try 块。
  • 您想要实现的行为是什么?除非您通过其他方式获得了令牌,否则用户将需要进行身份验证。
  • @ChristopherThomas,我试图理解为什么我会收到一个白框提示但没有加载任何内容。
  • @prestonsmith oneDriveClient 仍然为空,因为它是默认值——然后它会遇到 if。

标签: c# oauth-2.0 onedrive


【解决方案1】:

通过source code 挖掘,获取身份验证详细信息的默认 ui 似乎是 Windows 窗体:

这对我来说有点新,但another post 关于从控制台应用程序运行 Windows 窗体的主题似乎调用 Application.EnableVisualStyles();作为 Main() 的第一行。

也许这将使您的控制台应用程序能够正确绘制内容。

【讨论】:

  • 然而,有趣的是,我的逻辑存在于 async Task 中,你知道是否有一种方法可以简单地使用 Application.Run() 调用我的 Task 而不必创建一个新的Form?
  • 我的背景并不是真正的 Win Forms,如果这与您的评论无关,请原谅我。 SDK 中的 msaAuthenticationProvider.AuthenticateUserAsync() 正在尝试为用户创建一个表单以提供凭据(我假设这是您得到的白框)。如果启用视觉样式,白框是否显示任何内容?
【解决方案2】:

MsaAuthenticationProvider 使用 Windows 窗体。为了正确绘制表单,您需要开始运行标准消息循环。这可以通过Application.Run 方法完成。
另一个问题是,通过调用.Wait(),您将阻塞当前线程。

如果不想创建任何表单,可以创建自己的ApplicationContext,使用Application.Run(ApplicationContext)构造函数,然后手动关闭:

class Program
{
    [STAThread]
    static void Main()
    {
        var appContext = new ApplicationContext();
        RunTask(appContext); // run task asynchronously, pass ApplicationContext
        Application.Run(); // start processing messages
    }

    static async Task RunTask(ApplicationContext appContext, OneDriveClient oneDriveClient = null)
    {
        try
        {
            Task authTask = null;
            if (oneDriveClient == null)
            {
                var msaAuthenticationProvider = new MsaAuthenticationProvider("CLIENT_ID",null, "https://login.live.com/oauth20_desktop.srf", new[] { "onedrive.readonly", "wl.signin" }, null, new CredentialVault("CLIENT_ID));
                authTask =  msaAuthenticationProvider.AuthenticateUserAsync();
            }

            await authTask;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            appContext.ExitThread(); // stop messaging loop
        }
    }
}

或者您也可以拨打Application.Run() overload with no parameters 并使用.ContinueWith

class Program
{
    [STAThread]
    static void Main()
    {
        RunTask().ContinueWith(t => { Application.Exit(); }); // run task asynchronously, then exit
        Application.Run(); // start processing messages
    }

    static async Task RunTask(OneDriveClient oneDriveClient = null)
    {
        try
        {
            Task authTask = null;
            if (oneDriveClient == null)
            {
                var msaAuthenticationProvider = new MsaAuthenticationProvider("CLIENT_ID",null, "https://login.live.com/oauth20_desktop.srf", new[] { "onedrive.readonly", "wl.signin" }, null, new CredentialVault("CLIENT_ID));
                authTask =  msaAuthenticationProvider.AuthenticateUserAsync();
            }

            await authTask;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多