簡體   English   中英

Microsoft Graph API更新另一個用戶的照片?

[英]Microsoft Graph API Update another user's photo?

使用Microsoft Graph API,我能夠獲取Azure Active Directory租戶中所有用戶的列表,並確定他們是否有個人資料圖片。 我想在沒有照片的情況下獲取用戶列表並為其上傳一個,但即使我使用的帳戶具有對所有用戶帳戶的完全訪問權限且應用程序設置為具有完全權限,API也會返回403錯誤到圖譜API。

using (HttpClient client = new HttpClient())
{
    client.BaseAddress = new Uri("https://graph.microsoft.com/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/jpeg"));
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", oauthToken);

    // HTTP GET                
    HttpResponseMessage response = await client.PatchAsync($"v1.0/users/{emailAddress}/photo/$value", byteContent);
    if (!response.IsSuccessStatusCode)
    {
        throw new Exception("Error!");
    }
}

403 FORBIDDEN

是不是可以使用Graph API執行此操作,還是我錯過了某處的權限?

SCP值為:Calendars.Read Calendars.ReadWrite Contacts.Read Contacts.ReadWrite Directory.AccessAsUser.All Directory.Read.All Directory.ReadWrite.All email Exchange.Manage Files.Read Files.Read.Selected Files.ReadWrite Files.ReadWrite .AppFolder Files.ReadWrite.Selected full_access_as_user Group.Read.All Group.ReadWrite.All Mail.Read Mail.ReadWrite Mail.Send MailboxSettings.ReadWrite Notes.Create Notes.Read Notes.Read.All Notes.ReadWrite Notes.ReadWrite.All Notes .ReadWrite.CreatedByApp offline_access openid People.Read People.ReadWrite profile Sites.Read.All Tasks.Read Tasks.ReadWrite User.Read User.Read.All User.ReadBasic.All User.ReadWrite User.ReadWrite.All

首先,您應該看一下在// Build 2016期間發布的新Microsoft Graph SDK。這是Microsoft Graph SDK的Github: https//github.com/microsoftgraph
這是我創建的完整示例,使用它: https//github.com/Mimetis/NextMeetingsForGraphSample

對於你的問題,這里有兩種我編寫的方法,對我有用:我假設你有一種獲取有效訪問令牌的方法。

using (HttpClient client = new HttpClient())
{
    var authResult = await AuthenticationHelper.Current.GetAccessTokenAsync();
    if (authResult.Status != AuthenticationStatus.Success)
        return;

    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + authResult.AccessToken);

    Uri userPhotoEndpoint = new Uri(AuthenticationHelper.GraphEndpointId + "users/" + userIdentifier + "/Photo/$value");
    StreamContent content = new StreamContent(image);
    content.Headers.Add("Content-Type", "application/octet-stream");

    using (HttpResponseMessage response = await client.PutAsync(userPhotoEndpoint, content))
    {
        response.EnsureSuccessStatusCode();
    }
}

如果您使用Microsoft Graph SDK,它將非常簡單:)

GraphServiceClient graphService = new GraphServiceClient(AuthenticationHelper.Current);
var photoStream = await graphService.Users[userIdentifier].Photo.Content.Request().PutAsync(image); //users/{1}/photo/$value

勒布

關於更新其他用戶的照片, 文檔非常清楚。

要更新組織中任何用戶的照片,您的應用必須具有User.ReadWrite.All應用程序權限,並以其自己的身份調用此API,而不是代表用戶。 要了解更多信息,請參閱沒有登錄用戶的訪問權限。

這意味着您將需要應用程序的訪問令牌,而不是用戶訪問您的應用程序時獲得的令牌。 頁面顯示如何獲取應用程序訪問令牌。 這也意味着您必須向應用程序授予應用程序權限,這通常會被遺忘。

獲得令牌后,您可以在https://jwt.ms上查看令牌以查看令牌中的聲明。

你在談論令牌中的scp聲明。 只有在您擁有用戶令牌而非應用程序令牌時才會出現這種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM