Microsoft Graph API-读取Office 365 SharePoint列表并提取数据(图像) [英] Microsoft Graph API - Read Office 365 SharePoint List and Pull Data (Images)
问题描述
请参阅以下文章: Microsoft Graph API-Office 365访问SharePoint嵌套文件夹项目
我可以通过Graph API获取图像列表.我有一个MVC Azure应用,正在其中进行这些Graph API调用.
I'm able to get a list of Images via the Graph API. I have an MVC Azure App where I'm making these Graph API calls.
但是,当我尝试通过webUrl
属性访问Image时,访问被拒绝.
However when I try to access the Image via the webUrl
property, I get access denied.
我已经能够通过使用我可以访问的@microsoft.graph.downloadUrl
属性来解决此问题,并通过HttpClient
下载它,将其转换为字节数组并转换为缩略图.
I've been able to get around this by using the @microsoft.graph.downloadUrl
property which I can access, download it via HttpClient
convert to Byte Array and transform into a Thumbnail.
但是,理想情况下,我只能使用我的MVC网站对图形端点进行切片和切块,然后通过管道输出webUrl
,因此我不必处理调整大小或存储图像字节数据的情况.
However ideally I would just be able to use my MVC site to slice and dice the graph endpoint, then just pipe out webUrl
so I don't have to handle resizing or storing the image byte data.
首先,为什么webUrl
返回访问被拒绝?
First, why is webUrl
return Access Denied?
第二,仅通过我的MVC应用通过管道传输图像URL的最佳方法是什么?我不想负责存储/变换图像.数据需要公开访问.
Second, what would the best approach to just pipe the Image URL through my MVC App? I don't want to be responsible for housing/transforming the image. The data needs to be accessed publicly.
推荐答案
There are a couple of ways to do this but given that you're looking to show a thumbnail, I'd take a look at /thumbnails
endpoint:
GET /sites/{site-id}/drive/items/{item-id}/thumbnails
您可以将其与/content
端点结合使用来检索二进制文件本身:
You can use this in conjunction with the /content
endpoint to retrieve the binary itself:
GET /me/drive/items/{item-id}/thumbnails/{thumb-id}/{size}/content
关于如何处理二进制结果,我通常在后端使用Base64对其进行编码,然后将数据URL返回给客户端.您还可以缓存此结果,并让您的方法在向Graph发出新请求之前简单地检查有效的缓存副本.
As for what to do with the binary result, I typically Base64 encode it on the backend and return a Data URL to the client. You can also cache this result and have your method simply check for a valid cached copy before making a new request to Graph.
这对于数据非常小的个人资料照片"和缩略图"特别有效.
This works particularly well with Profile Photos and Thumbnails where the data is pretty small.
如何使用.NET Graph Client SDK在C#中执行此操作的示例:
An example of how you might do this in C# using the .NET Graph Client SDK:
var imageStream = await graphClient.Me
.Photo
.Content
.Request()
.GetAsync();
using (var memoryStream = new MemoryStream())
{
imageStream.CopyTo(memoryStream);
var base64pic = Convert.ToBase64String(memoryStream.ToArray());
return "data:image;base64," + base64pic;
}
这篇关于Microsoft Graph API-读取Office 365 SharePoint列表并提取数据(图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!