【问题标题】:REST call to Microsoft Graph对 Microsoft Graph 的 REST 调用
【发布时间】:2018-07-19 12:05:21
【问题描述】:

我已获得访问令牌,但我很难了解您随后如何发送所需数据的请求。在示例Call Microsoft Graph 中,他们有:

获取 https://graph.microsoft.com/v1.0/me/messages?$select=subject,from,receivedDateTime&$top=25&$orderby=receivedDateTime%20DESC 接受:application/json 授权:Bearer token

但是将 Accept: 和 Authorization: 解析到 Microsoft Graph 的方法是什么?

我已尝试作为 POST,但它显示不记名令牌为空。

$token=$_SESSION['$token'];

$url = 'https://graph.microsoft.com/v1.0/me/calendarview?startdatetime=2018-02-08T18:29:54.171Z&enddatetime=2018-02-15T18:29:54.171Z';

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url,
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        Authorization => 'Bearer ' . $token,
        Content-Type => 'application/json'
    )
    )
);

$resp = curl_exec($curl);
curl_close($curl);

【问题讨论】:

  • 您看过 MS 的 ADAL 库吗?很多图API调用的例子github.com/AzureAD
  • 不是 100% 熟悉 CURL,但“授权”“承载 XxYyZy”应该是一个标题,而不是在发布的字段中。

标签: rest microsoft-graph-api


【解决方案1】:

使用提供的graphServiceClient

// Create Microsoft Graph client.
                try
                {
                    graphClient = new GraphServiceClient(
                        "https://graph.microsoft.com/v1.0",
                        new DelegateAuthenticationProvider(
                            async (requestMessage) =>
                            {
                                var token = await GetTokenForUserAsync();
                                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);


                            }));
                    return graphClient;
                }

使用它来验证您的请求。

$request = 'https://graph.microsoft.com/v1.0/me/calendarview?startdatetime=2018-02-08T18:29:54.171Z&enddatetime=2018-02-15T18:29:54.171Z';
hrm = new HttpRequestMessage(HttpMethod.Get, request);
            // Authenticate (add access token) our HttpRequestMessage
            await graphClient.AuthenticationProvider.AuthenticateRequestAsync(hrm);
            // Send the request and get the response.
            response = await graphClient.HttpProvider.SendAsync(hrm);
            jsonString = await response.Content.ReadAsStringAsync();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-24
    • 2019-02-24
    • 2020-07-28
    • 2021-06-11
    • 1970-01-01
    • 2017-09-25
    • 2021-12-18
    • 1970-01-01
    相关资源
    最近更新 更多