Microsoft C++ 用于图形 API 的 Rest SDK

Microsoft C++ Rest SDK for Graph APIs

本文关键字:Rest SDK API 图形 C++ 用于 Microsoft      更新时间:2023-10-16

我正在尝试使用Microsoft的C++ Rest SDK(https://microsoft.github.io/cpprestsdk/index.html(来调用图形API,但到目前为止,这一直是一场斗争。

在 C# 中,我可以用几行代码完成我的任务。例如,请参阅Microsoft教程中的以下代码:

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++中开发类似的功能,为此,我们正在探索Microsoft C++ Rest SDK。但是我找不到任何好的例子来实现简单的事情,例如提供客户端ID,客户端密码以获取访问令牌和授权。

如果有人遇到任何示例/链接来实现相同的效果,请告诉我。

这里有一些 Dropbox、LinkedIn 和 MS Live scope 中的 oauth 2.0 代码: https://github.com/microsoft/cpprestsdk/blob/master/Release/samples/Oauth2Client/Oauth2Client.cpp

C++ Rest SDK 中的其他示例:
https://github.com/microsoft/cpprestsdk/tree/master/Release/samples

首先,您必须区分:
1. MS 图形身份验证 - 实际上是基于 oauth 2.0(简称:MSAL(的 Azure 访问目录/Microsoft标识平台身份验证
2。使用身份验证过程中的访问令牌访问 MS 图形 API(在标准过程中,应使用 MS 图形 SDK(

对于C++,没有 MSAL 或 SDK 库。 所以 - 为了进行身份验证,你应该使用我上面粘贴的 oauth 2.0 示例。 因为您需要自己编写所有内容,请深入阅读有关 MS Graph 身份验证的文档 https://learn.microsoft.com/en-us/graph/auth/

在这里,您可以查看所有需要的端点、机密等,以获取示例 Postman 调用: 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/

在 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图形 REST API v1.0 参考 https://learn.microsoft.com/en-us/graph/api/overview?toc=./ref/toc.json&view=graph-rest-1.0