我收到 invalidRequest 异常抛出:'Microsoft.Graph.ServiceException' in mscorlib.ni.dll

I get invalidRequest Exception thrown: 'Microsoft.Graph.ServiceException' in mscorlib.ni.dll

我正在将 Onedrive SDK 与我的 Xamarin 应用程序的 UWP 部分集成。按下下载按钮后,我会看到 Onedrive 登录页面,但它会在这一行中抛出上述错误:

     try
    {
        var appFolder = await OneDriveClient.Drive.Special.AppRoot.Request().GetAsync();
        Debug.WriteLine(appFolder.Name);
    }
    catch (ServiceException e  )
    {
        Debug.WriteLine(e.Message +"    " +  e.Error.Code);

    }

这里是完整的相关代码:

     public async Task Download(string filename)
{
    //AccountSelectionLoaded();
    await InitializeClient();
    try
    {
        var appFolder = await OneDriveClient.Drive.Special.AppRoot.Request().GetAsync();
        Debug.WriteLine(appFolder.Name);
    }
    catch (ServiceException e  )
    {
        Debug.WriteLine(e.Message +"    " +  e.Error.Code);

    }

    var file = await OneDriveClient.Drive.Special.AppRoot.Children[filename].Content.Request().GetAsync();

    //var fileStream = await fileBuilder.Content.Request().GetAsync();

    IStorageFile appFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("test.db3",
                        CreationCollisionOption.OpenIfExists);
    byte[] fileBytes;
   using (DataReader reader = new DataReader(file.AsInputStream()))
        {
            fileBytes = new byte[file.Length];
            await reader.LoadAsync((uint)file.Length);
            reader.ReadBytes(fileBytes);
        }
   Debug.WriteLine(fileBytes.Length);
    Debug.WriteLine("Writing");
    await FileIO.WriteBytesAsync(appFile, fileBytes);
    Debug.WriteLine("End of writing");       
}

private async Task InitializeClient()
{
    if (OneDriveClient == null)
    {
        Task authTask;
        var msaAuthProvider = new MsaAuthenticationProvider(oneDriveConsumerClientId,oneDriveConsumerReturnUrl,scopes);
        await msaAuthProvider.AuthenticateUserAsync();
        OneDriveClient = new OneDriveClient(oneDriveConsumerBaseUrl, msaAuthProvider);

        AuthenticationProvider = msaAuthProvider;


    }
}

感谢您报告此问题。事实上,我们可以在 UWP 应用程序

中使用 OneDrive .NET SDK 2.0.4 看到相关问题

我会通过内部方式报告这个问题。

作为解决方法,请参阅此博客:Windows 10 - Implementing a UWP App with the Official OneDrive SDK

Laurent Bugnion 描述了在 UWP 应用中启用 OneDrive 功能的详细步骤(以及 demo)。

private IOneDriveClient _client;
public MainPage()
{
  InitializeComponent();
  AuthenticateButton.Click += async (s, e) =>
  {
    var scopes = new[]
    {
      "onedrive.readwrite",
      "onedrive.appfolder",
      "wl.signin"
    };
    _client = OneDriveClientExtensions.GetClientUsingOnlineIdAuthenticator(
      _scopes);
    var session = await client.AuthenticateAsync();
    Debug.WriteLine($"Token: {session.AccessToken}");
  };
}

当时,项目正在使用 1.2.0 SDK现在还在工作