C
C#2y ago
Kukuba008

✅ Microsoft Graph API

Hello I need to create program that will load data from Outlook calendar, which is done using Microsoft Graph API, so i have this code:
var scopes = new[] { "https://graph.microsoft.com/.default" };

// Multi-tenant apps can use "common",
// single-tenant apps must use the tenant ID from the Azure portal
var tenantId = "teanantid";

// Values from app registration
var clientId = "clientid";
var clientSecret = "clientsecret";

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

// https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);


var x = await graphClient.Users["clientid"].Request().GetAsync();
var scopes = new[] { "https://graph.microsoft.com/.default" };

// Multi-tenant apps can use "common",
// single-tenant apps must use the tenant ID from the Azure portal
var tenantId = "teanantid";

// Values from app registration
var clientId = "clientid";
var clientSecret = "clientsecret";

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

// https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);


var x = await graphClient.Users["clientid"].Request().GetAsync();
Got from here: https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=CS#client-credentials-provider But im getting this error:
Code: Authorization_RequestDenied
Message: Insufficient privileges to complete the operation.
Code: Authorization_RequestDenied
Message: Insufficient privileges to complete the operation.
*All the variables are filled properly, i just deleted the values here. What should i do ? Thanks
Choose a Microsoft Graph authentication provider - Microsoft Graph
Learn how to choose scenario-specific authentication providers for your application.
16 Replies
Anton
Anton2y ago
looks like you haven't authorized your app to do those action
Kukuba008
Kukuba0082y ago
so i have to do something like this ?
Kukuba008
Kukuba0082y ago
*privileges to different action, but its just an example
Anton
Anton2y ago
yeah ig
Kukuba008
Kukuba0082y ago
Hello i am still trying to make it work, but now im getting different error:
Code: ErrorAccessDenied
Message: Access is denied. Check credentials and try again.
Code: ErrorAccessDenied
Message: Access is denied. Check credentials and try again.
when i try to acces:
var x = await graphClient.Users["id"].Calendar.Request().GetAsync();
var x = await graphClient.Users["id"].Calendar.Request().GetAsync();
I have set the permitions to (image) but it still doesnt work ? What should i do, thanks.
Anton
Anton2y ago
check the docs of what permissions this function requires
Kukuba008
Kukuba0082y ago
Anton
Anton2y ago
are you using the right token? maybe you've got multiple
Kukuba008
Kukuba0082y ago
it should be the right one, i have tryied it many times, with different ones
Anton
Anton2y ago
does it work if you give it ReadWrite? are you sure it's not using the group category?
Kukuba008
Kukuba0082y ago
No idea sorry, i dont have permissions for azure, so i always have to ask someone to give them to me, i will ask them to regenerate the tokens, and allow the group calendar, which will most likely take more than day. So thanks for help, and i will be back tomorrow (if it breaks after that).
Anton
Anton2y ago
for the record, I haven't used these tools ever, I'm just giving you my best guesses
Kukuba008
Kukuba0082y ago
np np, everything is appreciated at this point lol Hello, so i have got the permissions for it now and generated new value for secret id, and im still getting the same error ? Any ideas on what i could do ?
Anton
Anton2y ago
contact support
Kukuba008
Kukuba0082y ago
ok thanks
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.