Get a user presence Microsoft Graph Api using Graph SDK 5.17.0 C#

Hisanth S 0 Reputation points
2023-07-16T03:57:15.3566667+00:00

Hi, i am working with graph API for teams and new to the graph API. I need to get user presence. The API is working fine with graph Explorer by using the following endpoint https://graph.microsoft.com/v1.0/me/presence

I am using this end point in my windows application (C# .net Core). I am creating class library and added Graph SDK 5.17.0 .

I have given the delegated Permission Presence.Read and Presence.Read.All from organization account.

  1. I am trying with client secret by using tenantId, clientId, clientSecret
    • It throws an error like 'Forbidden'
  2. I am trying with the following
Using Azure.Identity;
Using Microsoft.Graph;


var tenantId = "MytenantId";
var clientId = "MyclientId";

// using Azure.Identity;
var options = new UsernamePasswordCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};

//var scopes = new[] { "https://graph.microsoft.com/.default" };
var scopes = new[] { "Presence.Read" };
var userName = "******@mail.com";
var password = "Password1!";

var userNamePasswordCredential = new UsernamePasswordCredential(
    userName, password, tenantId, clientId, options);

var graphClient = new GraphServiceClient(userNamePasswordCredential, scopes);

var mypresence = await graphClient.Me.Presence.GetAsync();

It's not returning the presence.

Microsoft Security | Microsoft Graph
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. CarlZhao-MSFT 46,406 Reputation points
    2023-07-17T03:29:51.6866667+00:00

    Hi @Hisanth S

    ROPC flow is an authentication flow that runs on the backend and cannot ask for your consent to permissions when hardcoding user credentials. So make sure your app has been granted the Presence.Read or Presence.Read.All delegated permission and granted admin consent for that permission.

    2

    Debugging:

    User's image

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


  2. AsithwMSFT 1,520 Reputation points Microsoft External Staff
    2023-12-25T00:56:22.0166667+00:00

    @Hisanth S

    Get User Presence API with Application Permission now available in Graph v1.0. I verified it.

    See this comment too from December 21, 2023. The Graph API change log is not yet updated.

    therefore, following Api should work now with application permission.

    https://graph.microsoft.com/v1.0/users/6e7b768e-07e2-4810-8459-485f84f8f204/presence
    
    https://graph.microsoft.com/v1.0/communications/presences/6e7b768e-07e2-4810-8459-485f84f8f204
    

    If the answer is helpful, please click Accept Answer and kindly upvote. If you have any further questions about this answer, please click Comment.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.