如何使用新的 Microsoft Graph api 创建授权令牌?

How do I create an auth token with the new microsoft graph api?

我一直在用这个:

https://github.com/Azure-Samples/active-directory-php-graphapi-web.git

访问有效的图 api。我的 azure AD 注册应用程序能够查询 API 以获取目录中的用户列表。

但现在我想列出目录中用户的文件夹。 本页

http://graph.microsoft.io/docs

说 url 应该是:

https://graph.microsoft.com/v1.0/me/drive/root/children

当我在 REST 调用中使用 url 时,我得到

"code": "InvalidAuthenticationToken",
"message": "CompactToken parsing failed with error code: -2147184105"

有道理,它正在从

获取令牌
https://graph.windows.net

所以,我迷路了。 API 有很多不同的版本,从消费级 onedrive(以前的 skydrive),第一个图表 api(我通过 https://graph.windows.net), the office 365 API (which I access via https://login.microsoftonline.com) and now the graph api (formerly universal api https://graph.microsoft.com 访问)我只是不知道去哪里开始寻找正确的信息。

我目前在 PHP 工作,我敢肯定这在微软支持的平台列表中会很低,但是关于访问令牌生成如何在最新版本中工作的任何方向api 对比 o365 api 对比其他图表 api(在 graph.windows.net)将不胜感激。

有没有人和我一样困惑? 是否有一些中心参考资料解释了这些 api 之间的所有差异以及如何访问它们?

https://login.microsoftonline.com的端点是Azure AD授权端点,它提供SSO页面供用户登录和验证并获取授权码。

其他像 https://graph.microsoft.com 是一个资源端点,它建立在 REST APIs 上并提供来自 Microsoft 的资源和服务。

具体到端点https://graph.windows.net,官网的解释是:

The Azure Active Directory Graph API provides programmatic access to Azure Active Directory through REST API endpoints. Apps can use the Azure AD Graph API to perform create, read, update, and delete (CRUD) operations on directory data and directory objects, such as users, groups, and organizational contacts. And https://graph.mircosoft.com is a unified API that also includes APIs from other Microsoft services like Outlook, OneDrive, OneNote, Planner, and Office Graph, all accessed through a single endpoint with a single access token.

有关详细信息,请参阅 AD Graph REST

要通过Azure AD集成office 365,您必须检查您是否有office 365租户,以及您的office 365租户的管理员用户是否有访问Azure AD的权限。您可以参考 Deep Dive into the Office 365 Unified API 以获取集成 Office 365 Unified API 的分步指南。

此外,您可以参考Get started with Office 365 APIs powered by Microsoft Graph创建一个PHP示例。

Microsoft Graph 应该为您提供一个端点(和令牌获取)来访问 Office 365 和 Azure AD 服务提供的数据。请访问 https://graph.microsoft.com 了解更多详情 - 但请使用 v1.0 版本,因为这是适用于生产服务的 GA 版本。

关于您关于没有用户的服务应用程序的问题 UI - 您可以使用 client_credential 流程获取仅限应用程序的访问令牌。 (这在 Microsoft Graph 文档中目前没有记录,但它受到支持和描述 elsewhere - just set the resource to be https://graph.microsoft.com/)。在 Azure 管理门户中,您还需要 select 您的应用也需要 "Application Permissions"。目前支持仅应用程序访问邮件资源,但不支持应用程序仅访问一个驱动器资源(通过 Microsoft Graph)。我们会尽快打开它。

希望这对您有所帮助,

导入live outlook联系人我真的很努力。但经过几天的研发,我发现 https://dev.office.com/blogs/outlook-rest-api-v1-0-office-365-discovery-and-live-connect-api-deprecation which made me to switch on to microsoft graph.I have also tried with azure documentation and other stuff but I found it very much confusing but still I wasn't clear with it.So I have implemented the following in php which turned out to be luckily successful. Just follow the following steps: 1) Create your application in https://apps.dev.microsoft.com a) 生成一个新的 password.Save 应用程序 ID 和密码。 b) 将平台添加为 web 并添加 redirect url with https 因为只能使用 https 而 http 不适用。 c) 在 Advance options 下检查 Live SDK Support 并保存。

2) 将 url 中的范围作为 contacts.read 传递,因为我们需要登录用户的联系人。

  $client_id="YOUR_CLIENT_ID";
 $redirect_uri = SiteUrl.'hotmail-contact';
             $url="https://login.microsoftonline.com/common/oauth2/v2.0/authorize?
                    client_id=".$client_id."
                    &response_type=code
                    &redirect_uri=".$redirect_uri."
                    &response_mode=query
                    &scope=offline_access%20user.read%20mail.read%20contacts.read
                    &state=12345";

3)认证成功后,会return授权码。现在在获得代码后,我们通过 curl post 请求在 https://login.live.com/oauth20_token.srf 获得令牌请求,post 字段为

  $fields=array(
      'code'=>  urlencode($auth_code),
      'client_id'=>  urlencode($client_id),
      'client_secret'=>  urlencode($client_secret),
      'redirect_uri'=>  urlencode($redirect_uri),
      'grant_type'=>  urlencode('authorization_code')
    );

4) 获取联系人

$url = 'https://graph.microsoft.com/v1.0/me/contacts' we can even apply filters to them

现在使用参数 url 和令牌

请求 curl
  public function curl_use_token($url,$token) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
  //  curl_setopt($ch,CURLOPT_HTTPHEADER,array('HeaderName: HeaderValue'));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization:Bearer '.$token));
  //  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization", "Bearer " + $token));
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

    $data = curl_exec($ch);
    curl_close($ch);
    // print(gettype($data));
    // print($data);
    return $data;
  }

5) 获取数据后,数据 returned 不会是纯 json 格式,因此我们可以通过应用正则表达式从数据中仅提取 json 部分,然后解码它我们可以使用它。 感谢阅读