anu
anu

Reputation: 45

Microsoft graph authorization code flow - get authorization code from web app programmatically

I have created web app to CreateOrGet, Delete, Update onlinemeeting using Microsoft Graph API in C#.

To get authorization code as per link Get access on behalf of a user. It returns a webview as HttpClient calls api for AuthCodeGeneration and returns the response, which contains Authcode in browser. I have to manually copy it to execute CreateOrGet, Delete, Update onlinemeeting using Microsoft Graph API.

Is there any way to do this through code in C#?

Upvotes: 1

Views: 2802

Answers (1)

Allen Wu
Allen Wu

Reputation: 16498

You don't need to handle the "code" and "access token" by your self.

Install Microsoft Graph .NET SDK and implement Authorization code provider to get the authProvider. Use the authProvider to generate the graphClient.

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithRedirectUri(redirectUri)
    .WithClientSecret(clientSecret)
    .Build();

AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(confidentialClientApplication, scopes);

GraphServiceClient graphClient = new GraphServiceClient(authProvider);

var onlineMeeting = new OnlineMeeting
{
    StartDateTime = DateTimeOffset.Parse("2019-07-12T21:30:34.2444915+00:00"),
    EndDateTime = DateTimeOffset.Parse("2019-07-12T22:00:34.2464912+00:00"),
    Subject = "User Token Meeting"
};

await graphClient.Me.OnlineMeetings
    .Request()
    .AddAsync(onlineMeeting);

Upvotes: 1

Related Questions