As your code shows, you use the auth code flow to get access token, then create onlineMeeting by MS Graph API. Please see my code, it works well.
string clientId = "<your-client-id>";
string clientSecret = "<your-client-secret>";
string redirectUri = "<your-redirect-url>";
string authority = "https://login.microsoftonline.com/<tenant>";
string authorizationCode = "<the authorization code>";
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithRedirectUri(redirectUri)
.WithClientSecret(clientSecret)
.WithAuthority(authority)
.Build();
AuthorizationCodeProvider auth = new AuthorizationCodeProvider(app, scopes);
GraphServiceClient graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) => {
// Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
var authResult = await app.AcquireTokenByAuthorizationCode(scopes, authorizationCode).ExecuteAsync();
// Add the access token in the Authorization header of the API request.
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
})
);
var onlineMeeting = new OnlineMeeting
{
StartDateTime = DateTimeOffset.Parse("2021-07-12T21:30:34.2444915+00:00"),
EndDateTime = DateTimeOffset.Parse("2021-07-12T22:00:34.2464912+00:00"),
Subject = "My First MS Teams Meeting"
};
await graphServiceClient.Me.OnlineMeetings.Request().AddAsync(onlineMeeting);
Note:
authorizationCode: The auth code flow need to get this code first, then you could obtain the access token for API. See this step, you could request the url by browser and sign in with user, then it will response the code.
permission: To let it work, you need to add the permission(your application -> API permissions -> MS Graph -> Delegated permissions -> OnlineMeetings.ReadWrite) to create onlineMeeting, see here.
Reference:
Microsoft Graph API about creating onlineMeeting by C#: link
The sample about using AuthorizationCodeProvider
and more details about the code.
UPDATE:
Message: Create online meeting with application permission is only
supported in beta.
The API(/v1.0) just supports delegated permission(OnlineMeetings.ReadWrite), not application permission. You could see this in the previous note.
Both the /beta
also only supports delegated permission, see:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…