我得到了invalidRequest异常抛出:' Microsoft.Graph.ServiceException'在mscorlib.ni.dll中

时间:2016-11-09 20:31:56

标签: xamarin.forms uwp microsoft-graph onedrive

我正在将Onedrive SDK与我的Xamarin App的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;


    }
}

1 个答案:

答案 0 :(得分:0)

感谢您报告此问题。实际上,我们可以在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 现在仍在使用