Microsoft C++ Rest SDK for Graph APIs

问题描述 投票:0回答:1

我正在试用微软的C++ Rest SDK (https:/microsoft.github.iocpprestsdkindex.html。)来调用Graph APIs,但到目前为止还很吃力。

在C#中,我可以在几行代码中完成我的任务。例如,参考以下微软教程中的代码。

        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);

        }

现在为了实现跨平台支持,我需要用C++开发类似的功能,为此,我们正在探索微软的C++ Rest SDK。但我找不到好的例子来实现提供客户端ID、客户端秘密来获取访问令牌和授权这样简单的事情。

如果有谁遇到过类似的例子,请告诉我。

c++ azure-ad-graph-api cpprest-sdk
1个回答
0
投票

这里你有一些代码,用于Dropbox、Linkedin和MS Live范围内的oauth 2.0。https:/github.commicrosoftcpprestsdkblobmasterReleasesamplesOauth2ClientOauth2Client.cpp。

C++ Rest SDK内的其他示例。https:/github.commicrosoftcpprestsdktreemasterReleasesamples。

首先,你要区分:1.MS Graph认证--其实就是Azure Access DirectoryMicrosoft身份平台认证,基于oauth 2.0(简称:MSAL)2.MS Graph API的认证。使用认证过程中的访问令牌访问MS Graph API(在标准过程中,你应该使用MS Graph SDK) 3.

对于C++来说,没有MSAL或SDK库。所以--对于认证,你应该使用我上面粘贴的oauth 2.0例子。因为你需要自己编写所有的东西,所以请深入阅读关于MS Graph的认证文档。https:/docs.microsoft.comen-usgraphauth

在这里,你可以观看所有需要的端点,秘密等样本Postman调用。https:/docs.microsoft.comen-usgraphus-postman#set-up-on-behalf-of-api-calls。https:/developer.microsoft.comen-usgraphblogs30daysmsgraph-day-13-postman-microsoft-graph-calls。

在URL中,使用了以下变量。

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  

关于API调用,请阅读Microsoft Graph REST API v1.0的参考资料。https:/docs.microsoft.comen-usgraphapioverview?toc=.reftoc.json&view=graph-rest-1.0。

© www.soinside.com 2019 - 2024. All rights reserved.