Office 365 Graph API 报告 getOneDriveActivityFileCounts 或 GetEmailActivityCounts 给出错误
Office 365 Graph API Reporting getOneDriveActivityFileCounts or GetEmailActivityCounts gives error
我正在尝试使用图表 API 从 O365 提取一些报告,示例工作正常,图表浏览器也工作,但是当尝试提取任何 return 是 CSV 或文本文件的报告时我收到错误,我正在使用以下核心:
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create("XXX")
.Build();
string[] scopes = new string[] { "Organization.ReadWrite.All",
"Reports.Read.All" };
var accounts = await publicClientApplication.GetAccountsAsync();
AuthenticationResult result;
try
{
result = await publicClientApplication.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
.ExecuteAsync();
}
catch (MsalUiRequiredException)
{
result = await publicClientApplication.AcquireTokenInteractive(scopes)
.ExecuteAsync();
}
// Create an authentication provider by passing in a client application and graph scopes.
DeviceCodeProvider authProvider = new DeviceCodeProvider(publicClientApplication, scopes);
// Create a new instance of GraphServiceClient with the authentication provider.
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var x = await graphClient.Reports.GetOneDriveActivityFileCounts("D30").Request().GetAsync()
;
这给出了一个错误:
Newtonsoft.Json.JsonReaderException:'Unexpected character encountered while parsing value: R. Path '',第 0 行,位置 0。'
根据文档,这应该 return 一个 URL 我可以下载文件的地方,我试图请求一个 JSON 格式,但它也不被支持,我是什么做错了吗?!
如果你用GraphServiceClient graphClient = new GraphServiceClient(authProvider);
,就会报这个错。
深入SDK,发现在HttpProvider下有一个JsonConvert.DeserializeObject<Report>(inputString)
方法
这是它的信息:
/// <summary>
/// Deserializes the JSON string to an object of the specified type.
/// </summary>
/// <typeparam name="T">The deserialization type.</typeparam>
/// <param name="inputString">The JSON string to deserialize.</param>
/// <returns>The deserialized object.</returns>
T DeserializeObject<T>(string inputString);
显然这是错误的原因。它无法反序列化,因为它是 CSV 而不是 JSON 数据。
目前我认为这是Graph SDK的一个bug。
作为解决方法,您可以直接调用 API 端点来获取数据。
或者您可以在代码中实现自己的序列化程序。
参见以下示例:
HttpProvider provider = new HttpProvider(new MySerializer());
var graphServiceClient = new GraphServiceClient(authProvider, provider);
var x = await graphClient.Reports.GetOneDriveActivityFileCounts("D30").Request().GetAsync();
而 MySerializer() 是这样的:
internal class MySerializer : ISerializer
{
public T DeserializeObject<T>(Stream stream)
{
throw new NotImplementedException();
}
public T DeserializeObject<T>(string inputString)
{
//implement your logical here
return default(T);
}
public string SerializeObject(object serializeableObject)
{
//implement your logical here
return "TEST MESSAGE";
}
}
inputString
是csv报告的内容。你可以自己处理。然后它会return到你定义的"x"。
自 2020 年 9 月起,一种选择是使用 beta sdk which allows you to request for json payload。调用将如下所示
var options = new List<QueryOption>()
{
new QueryOption("format", "application/json")
};
var x = graphClient.Reports.GetEmailActivityCounts("D7").Request(options).GetAsync().Result;
参考:
- Providing custom parameters for msgraph
- 在 dot net core 中使用 beta sdk。
dotnet add package Microsoft.Graph.Beta
我正在尝试使用图表 API 从 O365 提取一些报告,示例工作正常,图表浏览器也工作,但是当尝试提取任何 return 是 CSV 或文本文件的报告时我收到错误,我正在使用以下核心:
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create("XXX")
.Build();
string[] scopes = new string[] { "Organization.ReadWrite.All",
"Reports.Read.All" };
var accounts = await publicClientApplication.GetAccountsAsync();
AuthenticationResult result;
try
{
result = await publicClientApplication.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
.ExecuteAsync();
}
catch (MsalUiRequiredException)
{
result = await publicClientApplication.AcquireTokenInteractive(scopes)
.ExecuteAsync();
}
// Create an authentication provider by passing in a client application and graph scopes.
DeviceCodeProvider authProvider = new DeviceCodeProvider(publicClientApplication, scopes);
// Create a new instance of GraphServiceClient with the authentication provider.
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var x = await graphClient.Reports.GetOneDriveActivityFileCounts("D30").Request().GetAsync()
;
这给出了一个错误: Newtonsoft.Json.JsonReaderException:'Unexpected character encountered while parsing value: R. Path '',第 0 行,位置 0。'
根据文档,这应该 return 一个 URL 我可以下载文件的地方,我试图请求一个 JSON 格式,但它也不被支持,我是什么做错了吗?!
如果你用GraphServiceClient graphClient = new GraphServiceClient(authProvider);
,就会报这个错。
深入SDK,发现在HttpProvider下有一个JsonConvert.DeserializeObject<Report>(inputString)
方法
这是它的信息:
/// <summary>
/// Deserializes the JSON string to an object of the specified type.
/// </summary>
/// <typeparam name="T">The deserialization type.</typeparam>
/// <param name="inputString">The JSON string to deserialize.</param>
/// <returns>The deserialized object.</returns>
T DeserializeObject<T>(string inputString);
显然这是错误的原因。它无法反序列化,因为它是 CSV 而不是 JSON 数据。
目前我认为这是Graph SDK的一个bug。
作为解决方法,您可以直接调用 API 端点来获取数据。
或者您可以在代码中实现自己的序列化程序。
参见以下示例:
HttpProvider provider = new HttpProvider(new MySerializer());
var graphServiceClient = new GraphServiceClient(authProvider, provider);
var x = await graphClient.Reports.GetOneDriveActivityFileCounts("D30").Request().GetAsync();
而 MySerializer() 是这样的:
internal class MySerializer : ISerializer
{
public T DeserializeObject<T>(Stream stream)
{
throw new NotImplementedException();
}
public T DeserializeObject<T>(string inputString)
{
//implement your logical here
return default(T);
}
public string SerializeObject(object serializeableObject)
{
//implement your logical here
return "TEST MESSAGE";
}
}
inputString
是csv报告的内容。你可以自己处理。然后它会return到你定义的"x"。
自 2020 年 9 月起,一种选择是使用 beta sdk which allows you to request for json payload。调用将如下所示
var options = new List<QueryOption>()
{
new QueryOption("format", "application/json")
};
var x = graphClient.Reports.GetEmailActivityCounts("D7").Request(options).GetAsync().Result;
参考:
- Providing custom parameters for msgraph
- 在 dot net core 中使用 beta sdk。
dotnet add package Microsoft.Graph.Beta