user13260866
user13260866

Reputation: 181

Microsoft C++ Rest SDK for Graph APIs

I am trying out Microsoft's C++ Rest SDK (https://microsoft.github.io/cpprestsdk/index.html) to invoke Graph APIs but it has been a struggle so far.

In C# I can complete my tasks in a few lines of code. For example, refer following code from a Microsoft tutorial:

        AuthenticationConfig config = AuthenticationConfig.ReadFromJsonFile("appsettings.json");

        bool isUsingClientSecret = AppUsesClientSecret(config);

        IConfidentialClientApplication app;

        if (isUsingClientSecret)
        {
            app = ConfidentialClientApplicationBuilder.Create(config.ClientId)
                .WithClientSecret(config.ClientSecret)
                .WithAuthority(new Uri(config.Authority))
                .Build();
        }           


        string[] scopes = new string[] { $"{config.ApiUrl}.default" }; 

        AuthenticationResult result = null;
        try
        {
            result = await app.AcquireTokenForClient(scopes)
                .ExecuteAsync();                
        }
        catch (MsalServiceException ex) when (ex.Message.Contains("AADSTS70011"))
        {
        }

        // config.ApiUrl is set to "graph.microft.com"
        if (result != null)
        {
            var httpClient = new HttpClient();
            var apiCaller = new ProtectedApiCallHelper(httpClient);
            await apiCaller.CallWebApiAndProcessResultASync($"{config.ApiUrl}v1.0/users", result.AccessToken, Display);

        }

Now for cross-platform support, I need to develop similar functionality in C++ and for this purpose, we are exploring C++ Rest SDK from Microsoft. But I am unable to find any good examples to achieve a simple thing like providing client ID, client secret to get access token and to authorize.

Please let me know if anyone has come across any example / link to achieve the same.

Upvotes: 1

Views: 3482

Answers (1)

Luke Duda
Luke Duda

Reputation: 934

Here you have some code for oauth 2.0 in Dropbox, Linkedin and MS Live scope: https://github.com/microsoft/cpprestsdk/blob/master/Release/samples/Oauth2Client/Oauth2Client.cpp

Other samples within C++ Rest SDK:
https://github.com/microsoft/cpprestsdk/tree/master/Release/samples

First of all, you have to distinguish:
1. MS Graph authentication - which is, in fact, Azure Access Directory/Microsoft identity platform authentication, based on oauth 2.0 (short name: MSAL)
2. Accessing the MS Graph API using access token from the authentication process (in the standard process you should use MS Graph SDK)

For the C++ there is no MSAL or SDK library. So - for authentication, you should use oauth 2.0 example which I pasted above. Because you need to write everything on your own, please read deeply docs about authentication for MS Graph https://learn.microsoft.com/en-us/graph/auth/

Here you can watch all the needed endpoints, secrets etc. for sample Postman calls: https://learn.microsoft.com/en-us/graph/use-postman#set-up-on-behalf-of-api-calls https://developer.microsoft.com/en-us/graph/blogs/30daysmsgraph-day-13-postman-to-make-microsoft-graph-calls/

In the URLs there are the following variables used:

Callback URL: https://app.getpostman.com/oauth2/callback  
Auth URL: https://login.microsoftonline.com/**TENANTID**/oauth2/v2.0/authorize  
Access Token URL: https://login.microsoftonline.com/**TENANTID**/oauth2/v2.0/token  
Client ID: CLIENTID  
Client Secret: CLIENTSECRET  
Scope: https://graph.microsoft.com/.default  
State: RANDOMSTRING  

For the API calls, read about Microsoft Graph REST API v1.0 reference https://learn.microsoft.com/en-us/graph/api/overview?toc=./ref/toc.json&view=graph-rest-1.0

Upvotes: 1

Related Questions