结合使用 Postman 和 Microsoft Graph APIUse Postman with the Microsoft Graph API

使用 Microsoft Graph Postman 集合,只需几分钟,即可开始使用 Microsoft Graph API。You can use the Microsoft Graph Postman collection to get started with Microsoft Graph APIs in minutes.

Postman 图像

本文介绍了如何快速掌握 Postman 和 Microsoft Graph。This article explains how to get up and running with Postman and Microsoft Graph. 此外,还可以使用 Graph 管理器直接在 Web 浏览器中浏览 Microsoft Graph API。You can also explore Microsoft Graph APIs directly in your web browser by using Graph Explorer.

有关如何操作的详细信息,请跟随此文章中的步骤或观看 Microsoft Graph Postman 工作区入门视频。For details about how to do this, follow the steps in this article or watch the Getting started with Microsoft Graph Postman workspace video.

第一步 - 创建 Microsoft Graph Postman 集合分支Step 1 - Forking the Microsoft Graph Postman collection

要使用 Postman 集合,请创建其分支到你的 Postman 工作区。To use the Postman collection, fork it to your own Postman workspace. 在 web 浏览器中进行该操作。Do this from the web browser.

  1. 转到 Postman 并登录。Go to Postman and sign in.
  2. 转到标签名为 Microsoft Graph 的 Postman 集合。Go to the Postman collection labeled Microsoft Graph.
  3. 为你的分支填充标签。Fill in a label for your own fork. 此值可以是任何文本。This can be any text.
  4. 在工作区下,请确保在下拉列表中选中 我的工作区Under Workspace, ensure that My Workspace is selected in the drop-down list.
  5. 单击 分支集合Click Fork Collection.

随后会重定向至你的工作区中主要 Microsoft Graph Postman 集合的一个分支。You will be redirected to a fork of the main Microsoft Graph Postman collection in your own workspace.

第二步 - (可选 - 仅针对 Postman web 浏览器)下载 Postman 代理。Step 2 - (Optional - Postman Web browser only) Download the Postman Agent

要在 web 浏览器中使用此 Postman 集合,请下载 Postman 桌面代理To use this particular Postman collection in your web browser, download the Postman Desktop Agent. 由于 web 浏览器的限制,无法在未下载此代理的情况下在 web 上使用 Postman。You can't use Postman for the web without this due to CORS restrictions in the web browser.

如果你正在使用 Postman for Windows 应用,则不需要此代理。You don't need the agent if you're using the Postman for Windows app. 打开 Postman for Windows 后,你会在工作区中看到这个分支集合。If you open Postman for Windows, you will see this forked collection in your workspace.

第三步 - 创建 Azure AD 应用程序Step 3 - Create an Azure AD application

要在你的开发者租户中使用此集合,请创建一个 Azure AD 应用程序并根据想要调用的请求给予其合适的权限。To use this collection in your own developer tenant, create an Azure AD application and give it the appropriate permissions for the requests you want to call. 如果没有开发者租户,你可以通过 Microsoft 365 开发人员计划注册一个。If you don't have a developer tenant, you can sign up for one through the Microsoft 365 Developer Program.

  1. 转到 portal.azure.com 并使用开发者租户管理员帐户登录。Go to portal.azure.com and sign in with your developer tenant administrator account.
  2. 单击 “Azure Services” 下的 “Azure Active Directory”。Under Azure Services, click Azure Active Directory.
  3. 在左侧菜单中,单击“应用注册”。On the left menu, click App registrations.
  4. 在水平菜单中,单击“新建注册”。On the horizontal menu, click New registration.
  5. 将“应用名称”设置为“Postman”。Set the Application name to Postman.
  6. 将“重定向 URI”设置为“https://oauth.pstmn.io/v1/browser-callback”。Set the Redirect URI to https://oauth.pstmn.io/v1/browser-callback.
  7. 单击“注册”。Click Register.
  8. 在左侧菜单中,单击 “API 权限”。On the left menu, click API Permissions.
  9. 在水平菜单中,单击“添加权限”,选择 “Microsoft Graph”,然后选择“委派权限”。In the horizontal menu, click Add a permission, select Microsoft Graph, and then select Delegated Permissions.
  10. 键入“Mail.”,展开“邮件”选项,然后检查 Mail.Read.Type Mail., expand the Mail options, and check Mail.Read.
  11. 单击“应用权限”然后键入“User.”,随后检查“应用权限”。Click Application permissions and type User., and check Application Permissions.
  12. 展开“用户”选项并检查 User.Read.AllExpand the User options and check User.Read.All.
  13. 单击“添加权限”。Click Add permissions.
  14. 在水平菜单中,单击“授予管理员许可”,并单击“确定”。In the horizontal menu, click Grant admin consent for, and click Yes.
  15. 在左侧菜单中,单击“概述”。In the left menu, click Overview. 你可以从这里获取“应用程序(客户端)ID” 和“目录(客户端)ID”。From here, you can get the Application (client) ID and Directory (tenant) ID. 这些会在第四步中用到。You will need these in step 4.
  16. 单击左侧菜单中的“证书和机密”。In the left menu, click Certificates & secrets.
  17. 单击“新建客户端机密”,并输入简短说明,然后单击“添加”。Click New client secret, enter a description, and click Add. 将鼠标光标悬浮在新的客户端机密 上并复制。Hover over the new client secret Value and copy it. 第四步中会用到它。You will need this in step 4.

Azure AD 应用程序现已具有代表用户提出调用 Mail.Read 请求的权限并成为 User.Read.All 的一个应用。The Azure AD application now has permissions to make requests on behalf of a user to call Mail.Read and as an application for User.Read.All.

步骤 4 - 在 Postman 中配置身份验证Step 4 - Configuring authentication in Postman

在此步骤中,将设置用于检索访问令牌的环境变量。In this step, you set up the environment variables used to retrieve an access token.

  1. 转到 分叉Go to Fork environment.
  2. 添加标签分叉。Add a label for fork. 此值可以是任何文本。This can be any text.
  3. 在工作区下,请确保在下拉列表中选中 我的工作区Under Workspace, ensure that My Workspace is selected in the drop-down list.
  4. 单击 分叉环境Click Fork Environment.
  5. ClientID中, 步骤 3.15 应用程序(客户端)ID 值设置当前值。In ClientID, set the Current value to the application (client) ID value from step 3.15.
  6. ClientSecret中, 3.17 客户端密码值设置当前值。In ClientSecret, set the Current value to the client secret value from step 3.17.
  7. TenantID中, 3.15 目录(租户)ID 值设置当前值。In TenantID, set the Current value to the directory (tenant) ID value from step 3.15.
  8. 在右上方,单击 保存On the top right, click Save.
  9. 关闭" "选项卡 "。Close the Manage Environments tab.
  10. 在右上方眼睛图标旁边,验证是否选择了 "M365 环境 ",而不是" 没有On the top right, next to the eye icon, verify that M365 Environment is selected in the drop down and not No environment.

第五步 - 获取委派的访问令牌Step 5 - Get a delegated access token

由于这是你第一次通过委派身份验证流程运行请求,你需要获取访问令牌。Because this is the first time you are running a request as a delegated authentication flow, you need to get an access token.

  1. 将鼠标光标悬浮在“委派”文件夹上,单击省略号,然后选择“编辑”。Hover over the Delegated folder, click the ellipsis, and select Edit
  2. 单击“授权”选项卡。Click the Authorization tab.
  3. 在右侧下滑并单击“获取新的访问令牌”。Scroll down on the right and click Get New Access Token.
  4. 登录你的开发者租户管理员账户。Sign in with your developer tenant adminstrator account.
  5. 单击“继续”,然后单击“使用令牌”按钮。Click Proceed, and then click the Use Token button.
  6. 在该对话框的右下角,单击“更新”。On the botton right of the dialog, click Update.

你现在已具有用于委派请求的有效访问令牌。You now have a valid access token to use for delegated requests.

第六步 - 运行你的第一个委派请求Step 6 - Run your first delegated request

"委派 文件夹内是各种您可以呼叫的 Microsoft Graph 工作负载的请求。Inside the Delegated folder are requests for various Microsoft Graph workloads you can call.

  1. 展开 文件夹,然后展开 邮件 文件夹。Expand the Delegated folder and then expand the Mail folder.
  2. 双击“获取我的邮件”来打开请求。Double-click Get my messages to open the request.
  3. 在右上角,单击“发送”。On the top right, click Send.

你已经成功地使用委派身份验证完成了一次 Microsoft Graph 调用。You have now successfully made a Microsoft Graph call using delegated authentication.

第七步 - 获取应用程序访问令牌Step 7 - Get an application access token

由于这是你第一次通过应用程序身份验证流程运行请求,你需要获取访问令牌。Because this is the first time you are running a request as a application authentication flow, you need to get an access token.

  1. 将鼠标光标悬浮在“应用程序”文件夹上,单击省略号,然后选择“编辑”。Hover over the Application folder, click the ellipsis, and select Edit.
  2. 单击“授权”选项卡。Click the Authorization tab
  3. 在右侧下滑并单击“获取新的访问令牌”。Scroll down on the right side and click Get New Access Token.
  4. 单击“继续”,然后单击“使用令牌”按钮。Click Proceed, and then click the Use Token button.
  5. 在该对话框的右下角,单击“更新”。On the bottom right of the dialog, click Update.

你现在已具有用于委派请求的有效访问令牌。You now have a valid access token to use for application requests.

第八步 - 运行你的第一个应用程序请求Step 8 - Run your first application request

应用程序”文件夹中是你可以调用的各种 Microsoft Graph 工作负载请求。Inside the Application folder are requests for various Microsoft Graph workloads you can call.

  1. 展开“应用程序”文件夹,然后展开“用户”文件夹。Expand the Application folder and then expand the User folder.
  2. 双击“获取用户”来打开请求。Double-click Get Users to open the request.
  3. 在右上角,单击“发送”。On the top right, click Send.

你已经成功地使用应用程序身份验证完成了一次 Microsoft Graph 调用。You have now successfully made a Microsoft Graph call using application authentication.

你可以按照这些步骤向 Microsoft Graph 发出其他请求。You can follow these steps to make other requests to Microsoft Graph. 请记住,你需要向你的 Azure AD 应用程序添加权限来使其他请求正常运行;否则,你将在响应中收到权限被拒的错误。Remember that you have to add permissions to your Azure AD application for other requests to work; Otherwise, you will get permission denied errors in your responses.

参与集合Contribute to the collection

若要贡献你自己的请求,需要 Postman 许可。If you want to contribute your own requests, you will need a Postman license. 你可以对分支集合做出更改,然后将鼠标光标悬浮在集合的顶端节点,然后选择“创建拉取请求”。You can make your changes to the forked collection, and then hover over the collection top node and select Create pull request.

另请参阅See also

有关如何操作的详细信息,请观看 Microsoft Graph Postman 工作区入门视频。For details about how to do this, watch the Getting started with Microsoft Graph Postman collection video.