Jas*_*SFT 1 c# microsoft-graph-api
我正在使用对 Microsoft Graph 的 httpClient 调用的旧实现来获取用户的照片。以下代码可以工作,但我现在使用的是 Graph Client SDK,并且操作略有不同。我在转换代码时遇到了困难,因为在线示例和其他参考文献似乎没有相同的方法。
var response = await httpClient.GetAsync($"{webOptions.GraphApiUrl}/beta/me/photo/$value");
byte[] photo = await response.Content.ReadAsByteArrayAsync();
return Convert.ToBase64String(photo);
Run Code Online (Sandbox Code Playgroud)
var graphServiceClient = await graphClient.GetAuthenticatedGraphClientAsync(HttpContext);
Stream photo = await graphServiceClient.Me.Photo.Content.Request().GetAsync();
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这里和这里的示例,但我有点迷失,因为ReadAsByteArrayAsync()在新对象上不可用photo。
由于端点支持将照片内容作为唯一msgraph-sdk-dotnet的返回Get photoStream
public interface IProfilePhotoContentRequest : IBaseRequest
{
/// <summary>Gets the stream.</summary>
/// <returns>The stream.</returns>
Task<Stream> GetAsync();
/// <summary>Gets the stream.</summary>
/// <param name="cancellationToken">The <see cref="T:System.Threading.CancellationToken" /> for the request.</param>
/// <param name="completionOption">The <see cref="T:System.Net.Http.HttpCompletionOption" /> to pass to the <see cref="T:Microsoft.Graph.IHttpProvider" /> on send.</param>
/// <returns>The stream.</returns>
Task<Stream> GetAsync(
CancellationToken cancellationToken,
HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead);
//...
}
Run Code Online (Sandbox Code Playgroud)
而Convert.ToBase64StringMethod接受一个字节数组,下面的例子演示了如何转换原始例子:
var graphServiceClient = await graphClient.GetAuthenticatedGraphClientAsync(HttpContext);
var photoStream = await graphServiceClient.Me.Photo.Content.Request().GetAsync();
var photoBytes = ReadAsBytes(photoStream);
var result = Convert.ToBase64String(photoBytes);
Run Code Online (Sandbox Code Playgroud)
在哪里
//Convert Stream to bytes array
public static byte[] ReadAsBytes(Stream input)
{
using (var ms = new MemoryStream())
{
input.CopyTo(ms);
return ms.ToArray();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1609 次 |
| 最近记录: |