Microsoft Graph API. Getting user activity

Valentin Maschenko 1 Reputation point
2022-12-28T11:21:20.26+00:00

Currently I'm trying to create user activity from uwp application. But every time I encounter this response.

{  
    "error": {  
        "code": "AuthenticationError",  
        "message": "Error authenticating with resource",  
        "innerError": {  
            "date": "2022-12-28T09:20:16",  
            "request-id": "some id",  
            "client-request-id": "some id"  
        }  
    }  
}  

Here is my c# code example. Request json content string was taken from Microsoft Learn

 public sealed class UserActivityProvider : IUserActivityProvider  
    {  
        private const string activityId = "SendMessageUserActivity";  
        private static HttpClient httpClient = new HttpClient();  
  
        public UserActivityProvider()  
        {  
        }  
  
        private async Task<string> GetAccessTokenAsync(Account account)  
        {  
            var accessToken = string.Empty;  
  
            var publicClientApplication = PublicClientApplicationBuilder.Create(MicrosoftConstants.ClientId)  
               .WithRedirectUri(MicrosoftConstants.RedirectUri)  
               .Build();  
  
            var scopes = new string[]  
            {  
                "UserActivity.ReadWrite.CreatedByApp"  
            };  
  
            AuthenticationResult? authToken = null;  
  
            try  
            {  
                authToken = await publicClientApplication.AcquireTokenSilent(scopes, account.Email).ExecuteAsync();  
            }  
            catch (Exception)  
            {  
                authToken = await publicClientApplication.AcquireTokenInteractive(scopes).ExecuteAsync();  
            }  
  
            if (authToken != null)  
            {  
                accessToken = authToken.AccessToken;  
            }  
  
            return accessToken;  
        }  
  
        public async Task CreateUserActivityAsync(Account account, CreatingMessageUserActivityParameters userActivityParameters)  
        {  
            var accessToken = await GetAccessTokenAsync(account);  
            if (accessToken != string.Empty)  
            {  
                var contentForCreatingActivity = new StringContent(json, Encoding.UTF8, "application/json");  
                httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);  
                var response = await httpClient.PutAsync($"https://graph.microsoft.com/beta/me/activities/{activityId}", contentForCreatingActivity);  
                var stringifiedResponse = await response.Content.ReadAsStringAsync();  
            }  
        }  
    }  

And here is also get method for retrieving all activities and it's also return bad request

public async Task<string?> IsUserActivityExistsAsync(Account account)  
{  
    string? resultSubject = null;  
  
    var accessToken = await GetAccessTokenAsync(account);  
    if (accessToken != string.Empty)  
    {  
        httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);  
        var response = await httpClient.GetAsync("https://graph.microsoft.com/v1.0/me/activities");  
        var stringifiedResponse = await response.Content.ReadAsStringAsync();  
    }  
  
    return resultSubject;  
}  

All articles referencing that I have to provide correct scope but I took that scope from official Microsoft Learn and there wouldn't be a mistake.

Microsoft Security Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. Bhanu Kiran 3,616 Reputation points
    2022-12-31T00:09:57.613+00:00

    Hi @Valentin Maschenko ,

    Please ensure that you have assigned necessary license to access M365 resources.

    Hope this helps.

    0 comments No comments

Your answer

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