简体   繁体   English

如何使用新的microsoft graph api创建身份验证令牌?

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

I've been using this: 我一直在用这个:

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

to access the graph api, which works. 访问图表api,这是有效的。 My azure AD registered application is able to query the API to get a list of users in the directory. 我的Azure注册应用程序能够查询API以获取目录中的用户列表。

But now I want to list folders for a user in the directory. 但现在我想在目录中列出用户的文件夹。 This page 这一页

http://graph.microsoft.io/docs

says the url should be: 说网址应该是:

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

When I use that url in my REST call, I get 当我在我的REST调用中使用该URL时,我得到了

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

Makes sense, it's getting a token from 有道理,它从中得到一个令牌

https://graph.windows.net

So, I'm lost. 所以,我迷路了。 There's so many different versions of the API, from the consumer grade onedrive (formerly skydrive), the first graph api (which I access via 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 ) I just don't know where to begin to look for correct information. 有许多不同版本的API,来自消费级onedrive(以前称为skydrive),第一个图形api(我通过https://graph.windows.net访问),office 365 API(我通过https访问: //login.microsoftonline.com )现在图形api(以前的通用api https://graph.microsoft.com )我只是不知道从哪里开始寻找正确的信息。

I'm working in PHP at the moment, and I'm sure that's going to be pretty low on microsoft's list of supported platforms, but any direction about how the access token generation works in the newest api versus the o365 api versus the other graph api (at graph.windows.net) would be appreciated. 我目前正在使用PHP,我确信微软的支持平台列表会相当低,但是关于访问令牌生成如何在最新的API中与o365 api相对于其他图表的任何方向api(在graph.windows.net)将不胜感激。

Is anybody else as confused as I am? 有人像我一样困惑吗? Is there some central reference that explains all the differences between these apis and how to access them? 是否有一些中心参考解释了这些api与如何访问它们之间的所有差异?

The Microsoft Graph should provide you with one endpoint (and token acquisition) to access data offered by Office 365 and Azure AD services. Microsoft Graph应为您提供一个端点(和令牌获取),以访问Office 365和Azure AD服务提供的数据。 Please visit https://graph.microsoft.com for more details - but please use the v1.0 version as this is the GA version that is appropriate for production services. 有关详细信息,请访问https://graph.microsoft.com - 但请使用v1.0版本,因为这是适用于生产服务的GA版本。

As for your question about a service app with no user UI - you can get an app-only access token using the client_credential flow. 至于您关于没有用户UI的服务应用程序的问题 - 您可以使用client_credential流获取仅限应用程序访问令牌。 (This is not currently documented in the Microsoft Graph documentation, but it is supported and described elsewhere - just set the resource to be https://graph.microsoft.com/ ). (这在Microsoft Graph文档中目前没有记录,但在其他地方支持和描述 - 只需将资源设置为https://graph.microsoft.com/ )。 In the Azure Management Portal you'll need to select the "Application Permissions" that your app requires too. 在Azure管理门户中,您需要选择应用程序所需的“应用程序权限”。 Currently app-only access to mail resources is supported, but app only access to one drive resources (through Microsoft graph) is not supported. 目前支持仅限应用程序访问邮件资源,但不支持应用程序仅访问一个驱动器资源(通过Microsoft图形)。 We'll be looking to open that up shortly. 我们很快就会打开它。

Hope this helps, 希望这可以帮助,

The endpoint of https://login.microsoftonline.com is Azure AD authorization endpoint which provides SSO page for users login on and authenticate & obtain an authorization code. https://login.microsoftonline.com的端点是Azure AD授权端点,它为用户登录并验证和获取授权代码提供SSO页面。

The others like https://graph.microsoft.com is a resource endpoint which is built on REST APIs and provides resources and services from Microsoft. 其他像https://graph.microsoft.com是一个资源端点,它建立在REST API之上,并提供来自Microsoft的资源和服务。

Specifically, to endpoint https://graph.windows.net , the explanation on official site is : 具体来说,到端点https://graph.windows.net ,官方网站上的解释是:

The Azure Active Directory Graph API provides programmatic access to Azure Active Directory through REST API endpoints. Azure Active Directory图谱API通过REST API端点提供对Azure Active Directory的编程访问。 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. 应用程序可以使用Azure AD Graph API对目录数据和目录对象(如用户,组和组织联系人)执行创建,读取,更新和删除(CRUD)操作。 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. https://graph.mircosoft.com是一个统一的API,其中还包括来自Outlook,OneDrive,OneNote,Planner和Office Graph等其他Microsoft服务的API,所有这些API都通过具有单一访问令牌的单个端点进行访问。

Refer to AD Graph REST for more information. 有关更多信息,请参阅AD Graph REST

To integrate office 365 via Azure AD, you have to check whether you have an office 365 tenant and your administrator user of office 365 tenant has the access permission on Azure AD. 要通过Azure AD集成Office 365,您必须检查是否有Office 365租户,而Office 365租户的管理员用户是否具有Azure AD的访问权限。 You can refer to Deep Dive into the Office 365 Unified API for step by step guide of integrating office 365 Unified API. 您可以将Deep Dive引用到Office 365 Unified API中,以获取有关集成Office 365 Unified API的分步指南。

Furthermore, you can refer to Get started with Office 365 APIs powered by Microsoft Graph to create a PHP example. 此外,您可以参考Microsoft Graph支持的Office 365 API入门来创建PHP示例。

I have really tried hard to import live outlook contacts. 我真的很努力地导入实时展望联系人。 But after few days of R&D I have found 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. 但经过几天的研发,我找到了https://dev.office.com/blogs/outlook-rest-api-v1-0-office-365-discovery-and-live-connect-api-deprecation ,这让我想到了切换到微软graph.I也尝试了天蓝色的文档和其他的东西,但我发现它非常混乱,但我仍然不清楚它。所以我已经在PHP中实现了以下,结果幸运成功。 Just follow the following steps: 1) Create your application in https://apps.dev.microsoft.com a) Generate a new password.Save the application id and the password with you. 只需按照以下步骤操作:1)在https://apps.dev.microsoft.com中创建应用程序a)生成新密码。随身携带应用程序ID和密码。 b) Add platform as web and add redirect url with https as only https can be used and http is not applicable. b)将平台添加为web并使用https添加重定向URL,因为只能使用https且http不适用。 c) Check Live SDK Support under Advance options and save. c)检查高级选项下的Live SDK支持并保存。

2) pass the scope in the url as contacts.read as we need the signed in user's contacts. 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) After successful authentication , it would return the auth code . 3)成功验证后,它将返回验证码。 Now after obtaining the code we get request for the token by curl post request at https://login.live.com/oauth20_token.srf with postfields as 现在,在获得代码后,我们通过https://login.live.com/oauth20_token.srf请求curl post请求获取令牌,并将postfields作为

  $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) To fetch the contacts 4)获取联系人

$url = ' https://graph.microsoft.com/v1.0/me/contacts ' we can even apply filters to them $ url =' https://graph.microsoft.com/v1.0/me/contacts '我们甚至可以对它们应用过滤器

Now request curl with paramaters url and token 现在请求curl with paramaters url和token

  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) After getting the data , the data returned wont be in pure json format so we can extract only a json part from the data by applying regex and after decoding it we can use it. 5)获取数据后,返回的数据不会是纯json格式,因此我们只能通过应用正则表达式从数据中提取json部分,解码后我们可以使用它。 Thanks for reading 谢谢阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM