I am using one of the Microsoft Graph API in my project, i.e Outlook mail - "listconferencerooms" api.
Although it is giving he result, but the rooms listed in the result is not as expected.
When I cross check the Api result with the Meeting rooms available in my mailbox, both are not matching. In fact everyday i see the same set of meeting rooms which is not changing as per availability.
Code as below,
[HttpGet]
[Route("api/GetMeetingRooms")]
public async Task<string> getMeetingRooms()
{
//HttpResponseMessage message = null;
try
{
AppConfig appConfig = new AppConfig();
string token = await appConfig.GetTokenForApplication();
string response = string.Empty;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://graph.microsoft.com/");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage result = client.GetAsync("https://graph.microsoft.com/beta/me/findRooms").Result;
response = result.Content.ReadAsStringAsync().Result;
}
return response;
}
catch (Exception ex)
{
return ex.Message;
}
}
Kindly help me whether I am using correct API to get all available meeting, Conference rooms for my organization?
Or since the API is in Beta version I am not able to get the correct result.
Related
https://graph.microsoft.com/v1.0/users/ is returning only 100 records, I want to fetch all the record present in Azure AD.
I have tried with above API to but it always gives me 100 records and with top, I am able to fetch only 999 records. But I have more than 100k records and want to fetch at a time.
The call to https://graph.microsoft.com/v1.0/users/ returns a property called #odata.nextlink. Use #odata.nextlink to request more pages of user data.
I think it is not possible to fetch all users with one request but you could wirte a method getting all users for you. The following code is requesting the first 100 users. Afterwards it is calling the next 100 users until there is no more users.
It is only a work around. You should keep in mind that this function needs a long time to run
public async Task<List<GraphApiUser>> GetAllCloudUserAsync()
{
var query = "/users";
var response = await SendGraphApiRequest(HttpMethod.Get, query);
var data = JsonConvert.DeserializeObject<GetMultipleUserResponse>(await response.Content.ReadAsStringAsync());
var result = new List<GraphApiUser>();
result.AddRange(data.value);
var debugCounter = 1;
while (!string.IsNullOrEmpty(data.NextLink))
{
response = await SendGraphApiRequest(HttpMethod.Get, "/"+data.NextLink);
data = JsonConvert.DeserializeObject<GetMultipleUserResponse>(await response.Content.ReadAsStringAsync());
result.AddRange(data.value);
debugCounter++;
}
return result;
}
GetMultipleUserResponse-Class looks like that:
public class GetMultipleUserResponse
{
public List<GraphApiUser> value { get; set; }
[JsonProperty("odata.nextLink")]
public string NextLink { get; set; }
}
The GraphApiUser-Class looks diffrent from AD to AD because everyone is able to define own claims. Setting up this class belongs to you!
Sending a request can be done like this:
private async Task<HttpResponseMessage> SendGraphApiRequest(HttpMethod httpMethod, string query,
string json = "")
{
HttpClient http = new HttpClient();
var requestUri = "Your Ressource Id" + "Your Tenant" + "your query"+ "your api version";
HttpRequestMessage request = new HttpRequestMessage(httpMethod, requestUri);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", GetYourTokenHere());
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await http.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
string error = await response.Content.ReadAsStringAsync();
object formatted = JsonConvert.DeserializeObject(error);
Debug.WriteLine("Error Calling the Graph API: \n" +
JsonConvert.SerializeObject(formatted, Newtonsoft.Json.Formatting.Indented));
return null;
}
return response;
}
As per this documentation you should be able to create an appRoleAssignment via Microsoft Graph, however this doesn't work. In a GitHub issue I was instructed to create the issue here. We have migrated most of our code from Azure Graph API to Microsoft Graph and this is the last piece that is missing.
This finally worked for me!
There might be more optimized ways to post the JSON but I had to go to basics to make sure nothing is causing this to fail behind the scenes.
const string ROLE_ASSIGNMENT_FORMATTER = "https://graph.microsoft.com/beta/servicePrincipals/{0}/appRoleAssignments";
public static async Task AddApplicationUsers(string enterpriseAppId, string userId, string roleId)
{
HttpClient client = new HttpClient();
string url = string.Format(ROLE_ASSIGNMENT_FORMATTER, enterpriseAppId);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken());
var roleAssignment = new
{
appRoleId = roleId,
principalId = userId,
resourceId = enterpriseAppId
};
var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(roleAssignment), Encoding.UTF8, "application/json");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
return ;
}
else
{
throw new HttpRequestException(response.ReasonPhrase);
}
}
I am attempting to add members to a group. I am able to list all groups in my org, get user by email, get all users and I can even remove a Member from a group but I cannot add one - The error returned is 400 Bad Request.
Here is the function which is the same function signature as those that work: (I do have the accesstoken, valid group id and a valid member id)
I have confirmed the body data looks correct at least as far as I can see from the example in the docs.
Not sure what else I can add to make things clearer, ask and I'll update
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;
}
Thanks for any help that anyone can offer - this is the last call I have to make then home free
Found the issue for any who care to know for the future:
var body = new FormUrl... my code was incorrect, what's needed is a simple json string changed to this UPDATED:
var jsonData = $#"{{ ""{keyOdataId}"": ""{valueODataId}"" }}";
var body = new StringContent(jsonData, Encoding.UTF8, "application/json");
I would normally put the values in a class but this is for proof of concept and the json key needs to look exactly like this #odata.id
Clarifying what is happening here:
The request body for this call should be JSON encoded (application/json). The FormUrlEncodedContent method returns your dictionary as Form encoded (application/x-www-form-urlencoded).
You can write the JSON by hand (like you have so far) but a better solution would be to leverage Json.NET. This will let you encode the dictionary in much the same way you were with FormUrlEncodedContent:
var values = new Dictionary<string, string>
{
{ keyOdataId, valueODataId}
};
var body = JsonConvert.SerializeObject(values);
If you're going to be doing a lot of work with Microsoft Graph, I would highly recommend switching to the Microsoft Graph .NET SDK.
You're method here would be far simpler using the 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);
}
I need to get events from a calendar, and find out individual users in the event.
var graphServiceClient = new GraphServiceClient(...);
var events = graphServiceClient.Me.CalendarView.Request().GetAsync();
// ...
var attendee = events[0].Attendees[0];
// Is attendee a group or user?
// If a group, how do we expand it?
We can determine whether the mail address is a user or the group via retrieve the user/group. For example, we can get the specific group via the REST below:
https://graph.microsoft.com/v1.0/groups?$filter=mail+eq+'group1#yourtenant.onmicrosoft.com'
If the email we provide is a user, then the response would have an empty value, otherwise it return the information for this group.
And to get the members of specific group, we can make a request with the id return by the above request:
https://graph.microsoft.com/v1.0/groups/{groupid}/members
And here is the code to get the group and its members for your reference. And I recommend you make the REST via HttpClient because it is more flexible and efficient.
public async Task GetGroup(string mailAddress)
{
var groups = await graphserviceClient.Groups.Request().Top(10).GetAsync();
foreach (var group in groups.CurrentPage)
{
Console.WriteLine(group.Mail);
if (mailAddress.Equals(group.Mail))
return group.Id;
}
while (groups.NextPageRequest != null)
{
groups = await groups.NextPageRequest.GetAsync();
foreach (var group in groups.CurrentPage)
{
Console.WriteLine(group.Mail);
if (mailAddress.Equals(group.Mail))
return group.Id;
}
}
return null;
}
public async void GetMembers(string groupId)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "bearer " + _accessToken);
string serviceURL = String.Format("https://graph.microsoft.com/v1.0/groups/{0}/members?$select=mail", groupId);
var response = client.GetAsync(serviceURL).Result;
JObject json = JObject.Parse(response.Content.ReadAsStringAsync().Result);
foreach (var mail in json["value"].Values("mail"))
{
Console.WriteLine(mail);
}
}
update
We need to have the "Group.Read.All" scope to read the groups:
Been stuck for days, hoping someone can help me.
I have been trying to run the YouTube 'Search by keyword' example from Google's API examples for .net in a VS 2013 Express for Web MVC4 project, and the ExecuteAsync() calling the Google API never comes back.
I believe the example code works as I tested it in VS 2013 Express for Windows Desktop as a console application and it came back fine. Also the stats in google's developers console tell me the API request is being received.
Here is what I did:
I created a new VS 2013 Express for Web MVC4 project called GoogleTest and installed the 'Install-Package Google.Apis.YouTube.v3' package.
I then added the following model.
public class SearchYouTube
{
public int ID { get; set; }
public async Task RunYouTube()
{
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = " <MY DEVELOPER KEY HERE> ",
ApplicationName = this.GetType().ToString()
});
var searchListRequest = youtubeService.Search.List("snippet");
searchListRequest.Q = "googleapi examples"; // Replace with your search term.
searchListRequest.MaxResults = 50;
// Call the search.list method to retrieve results matching the specified query term.
var searchListResponse = await searchListRequest.ExecuteAsync();
List<string> videos = new List<string>();
List<string> channels = new List<string>();
List<string> playlists = new List<string>();
// Add each result to the appropriate list, and then display the lists of
// matching videos, channels, and playlists.
foreach (var searchResult in searchListResponse.Items)
{
switch (searchResult.Id.Kind)
{
case "youtube#video":
videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId));
break;
case "youtube#channel":
channels.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId));
break;
case "youtube#playlist":
playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId));
break;
}
}
Console.WriteLine(String.Format("Videos:\n{0}\n", string.Join("\n", videos)));
Console.WriteLine(String.Format("Channels:\n{0}\n", string.Join("\n", channels)));
Console.WriteLine(String.Format("Playlists:\n{0}\n", string.Join("\n", playlists)));
}
}
Then I call the above class in the Home controller like so:
public ActionResult Index()
{
ViewBag.Message = "MVC example";
SearchYouTube searchObject = new SearchYouTube();
searchObject.RunYouTube().Wait();
return View();
}
Running this in the debugger, the program steps into but never returns from this line in the SearchYouTube class above:
var searchListResponse = await searchListRequest.ExecuteAsync();
Can anyone help explain what I am doing wrong or what I am missing??
You seem to have a deadlock on your hands because you're doing "sync over async". When you use Task.Wait you're blocking and wasting a thread. After the inner async operation (i.e. await searchListRequest.ExecuteAsync();) completes it evidently needs that same thread to continue processing the rest of the method.
All that happens because of the SynchronizationContext present in ASP.Net which is captured when await is used so that the continuation would be posted to it. When you use ConfigureAwait(false) you're configuring the continuation to not run on the captured context and use the ThreadPool instead.
In console apps there is no SC and so every continuation runs on the ThreadPool. It's as if every await had ConfigureAwait(false).
To solve this deadlock you can use ConfigureAwait(false) or even better, make the MVC method async so you don't need to block synchronously (more on async in MVC):
public async Task<ActionResult> Index()
{
ViewBag.Message = "MVC example";
SearchYouTube searchObject = new SearchYouTube();
await searchObject.RunYouTube();
return View();
}