2017-10-04 28 views
2

我正在嘗試將成員添加到組。我可以列出我組織中的所有組,通過電子郵件獲取用戶,獲取所有用戶和我甚至可以從組中刪除成員但我無法添加 - 返回的錯誤爲400 Bad Request圖REST添加成員到組 - 錯誤請求

這裏是一個相同的函數簽名爲那些工作中的作用:(我確實有accesstoken,有效組ID和一個有效會員ID)

我已確認身體數據看起來是正確的,至少作爲就我所見,從example in the docs

不知道還有什麼我可以添加到讓事情更清晰,問我會更新

public async Task<string> AddGroupMember(string accessToken, string groupId, string memberId) 
{ 
    var status = string.Empty; 
    string endpoint = $"https://graph.microsoft.com/v1.0/groups/{groupId}/members/$ref"; 
    string queryParameter = ""; 

    // pass body data 
    var keyOdataId = "@odata.id"; 
    var valueODataId = $"https://graph.microsoft.com/v1.0/directoryObjects/{memberId}"; 

    var values = new List<KeyValuePair<string, string>> 
     { 
      new KeyValuePair<string, string>(keyOdataId, valueODataId) 
     }; 
    var body = new FormUrlEncodedContent(values); 

    try 
    { 
     using(var client = new HttpClient()) 
     { 
      using(var request = new HttpRequestMessage(HttpMethod.Post, endpoint + queryParameter)) 
      { 
       request.Content = body; 
       request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
       request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); 

       using(var response = await client.SendAsync(request)) 
       { 
        if (response.StatusCode == HttpStatusCode.NoContent) 
         status = "Member added to Group"; 
        else 
         status = $"Unable to add Member to Group: {response.StatusCode}"; 
       } 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     status = $"Error adding Member to Group: {ex.Message}"; 
    } 

    return status; 
} 

感謝您的幫助,任何人都可以提供 - 這是最後一次通話我必須做出然後回家免費

+0

發現誰照顧知道未來的任何問題: VAR體=新FormUrl ...代碼不正確,我們需要的是一個簡單的JSON字符串 改成這樣: VAR體=新的StringContent( 「{\」 「+ keyOdataId + 」\「:\ 」「 + valueODataId + 」\「}」,編碼。 UTF8,「application/json」); 一種笨重,但作品 – Tab

回答

2

發現任何關心誰知道未來的問題:

var body = new FormUrl...我的代碼是不正確的,什麼需要是一個簡單的js對字符串改成這個修訂

var jsonData = [email protected]"{{ ""{keyOdataId}"": ""{valueODataId}"" }}"; var body = new StringContent(jsonData, Encoding.UTF8, "application/json");

我通常把值的一類,但是這是概念證明和JSON關鍵需要看起來正是這樣@odata.id

2

澄清這裏發生了什麼:

這個調用的請求體應該是JSON編碼的(application/json)。 FormUrlEncodedContent方法將您的字典作爲Form編碼(application/x-www-form-urlencoded)返回。

你可以手工編寫JSON(就像你目前爲止),但更好的解決方案是利用Json.NET。這將讓你在encode the dictionary你多用FormUrlEncodedContent以同樣的方式:

var values = new Dictionary<string, string> 
    { 
     { keyOdataId, valueODataId} 
    }; 
var body = JsonConvert.SerializeObject(values); 

如果你打算做了很多工作,與微軟的圖形,我會強烈建議切換到Microsoft Graph .NET SDK

你在這裏的方法是簡單得多的使用SDK:

public async Task<string> AddGroupMember(string groupId, string memberId) 
{ 
    GraphServiceClient graphClient = AuthenticationHelper.GetAuthenticatedClient(); 
    User userToAdd = new User { Id = memberId }; 
    await graphClient.Groups[groupId].Members.References.Request().AddAsync(userToAdd); 
} 
+0

我會做json.net轉換 - 我正在做一個未來的應用程序的概念證明,看看如何一切正常。 SDK看起來更容易,謝謝你的提示和感謝回覆,當我們做出真正的交易時,給我看看什麼。 – Tab

+0

簡單地將'values'var轉換爲您所描述的不是贏家: '[{「Key」:「@ odata.id」,「Value」:「https://graph.microsoft.com/v1.0/directoryObjects/ee32df9c-6acc-453C-abb2-afb4380e364d 「}]' 應該是這樣的: '{ 」@ odata.id「:」 https://graph.microsoft.com/v1.0/ directoryObjects/{id}「 }' 2,您不能將集合序列化到」body「中,因爲body需要一種StringContent類型而不僅僅是一個字符串 – Tab

+0

對不起,複製並粘貼了錯誤的行。您應該使用Dictionary 而不是List )。 –

相關問題