Nov 17 2023 07:50 AM
Hi,
We are working on a bot framwerk project where we need to update our bot status based on the status in the ms teams (like available, un available, in a meeting).
We have used the below code for getting the status,
try
{
var clientId = "#client id";
var clientSecret = "# client secret";
var tenantId = "# tenant id";
string authority = $"https://login.microsoftonline.com/{tenantId}";
string scope = "https://graph.microsoft.com/.default";
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(new Uri(authority))
.Build();
string[] scopes = new[] { scope };
AuthenticationResult result = await app.AcquireTokenForClient(scopes)
.ExecuteAsync();
string accessToken = result.AccessToken;
// Create a GraphServiceClient instance using the obtained access token
GraphServiceClient graphClient = new GraphServiceClient(
new DelegateAuthenticationProvider(requestMessage =>
{
requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
return Task.FromResult(0);
}));
// Use the graphClient to make requests to Microsoft Graph API
var user = await graphClient.Users[emailId].Presence.Request().GetAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "------------- There was an error while getting status");
}
But in this code we are passing the default scope. In the actual app registration we have added the graph client permission for 'PRESENSE.READ' and while trying to get the user Presence feature, its throwing a error because of no permission .
So explicitly we have assigned the token which has right permission and tried to fetch the status , but still the availability is shown "PRESENSE UNKNOWN" where the user status was Available.
It will be good if you can help with use on adding the exact scopes of the particular app registration in code and also need to fetch the correct status of user.
Nov 19 2023 09:08 PM - edited Nov 19 2023 09:09 PM
Hello @Lakshmi_145 - Thanks for raising your query.
Please find below solution:
1) Please pass the AAD Object Id of user rather than email id while calling the API:
Like below:
https://graph.microsoft.com/v1.0/users/66825e03-7ef5-42da-9069-724602c31f6b/presence
Reference doc: Get presence - Microsoft Graph v1.0 | Microsoft Learn
Because you are passing wrong id, it is returning result as "PresenceUnknown".
Nov 19 2023 10:35 PM
We have updated the code to add the scope Presence.Read in scopes.
try
{
var clientId = "#client id";
var clientSecret = "# client secret";
var tenantId = "# tenant id";
string authority = $"https://login.microsoftonline.com/{tenantId}";
string scope = "https://graph.microsoft.com/.default";
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(new Uri(authority))
.Build();
string[] scopes = new[] { scope, "Presence.Read" };
AuthenticationResult result = await app.AcquireTokenForClient(scopes)
.ExecuteAsync();
string accessToken = result.AccessToken;
// Create a GraphServiceClient instance using the obtained access token
GraphServiceClient graphClient = new GraphServiceClient(
new DelegateAuthenticationProvider(requestMessage =>
{
requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
return Task.FromResult(0);
}));
// Use the graphClient to make requests to Microsoft Graph API
var user = await graphClient.Users[connectionRequest.Requestor.User.AadObjectId].Presence.Request().GetAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "------------- There was an error while getting status");
}
And getting the below exception,
We are able to fetch the status by giving the correct token explicitly.
Nov 19 2023 10:40 PM
@Lakshmi_145 - Please try to add only One scope and remove the other one.
Also, check if your generated token is having the same scope and check if it is working in Postman or Graph Explorer as well.
Nov 19 2023 10:47 PM - edited Nov 19 2023 10:48 PM
We tried to give string[] scopes = new[] { "Presence.Read" };
and got the exception,
Nov 19 2023 10:50 PM
Nov 19 2023 11:23 PM
Nov 21 2023 08:34 AM
Nov 23 2023 06:39 AM
@Lakshmi_145 - If you have added the Presence.Read
permission on the app registration, using .default
should work. Also, if you want to specify the exact scope, you can replace "https://graph.microsoft.com/.default"
with "https://graph.microsoft.com/Presence.Read"
in your scopes array which i think you are already doing.
Secondly, the presence status of a user in Teams can be one of the following: Available, Away, BeRightBack, Busy, DoNotDisturb, Offline, PresenceUnknown. If you are getting "PresenceUnknown", it could be due to one of the following reasons:
Could you please verify that?