Forum Discussion
Lakshmi_145
Nov 17, 2023Iron Contributor
Not able to fetch the status of user from Teams using graph client
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 co...
Lakshmi_145
Nov 20, 2023Iron Contributor
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.
ChetanSharma-msft
Microsoft
Nov 20, 2023Lakshmi_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.
- Lakshmi_145Nov 20, 2023Iron Contributor
We tried to give string[] scopes = new[] { "Presence.Read" };
and got the exception,
- ChetanSharma-msftNov 20, 2023
Microsoft
- Lakshmi_145Nov 20, 2023Iron Contributor