使用 Microsoft Graph API 在 Visual Studio 2017 中调用 Microsoft 365 服务Call Microsoft 365 services in Visual Studio 2017 with the Microsoft Graph API

你可以使用 Visual Studio 中的连接服务配置应用,从而调用 Microsoft Graph API。本文介绍如何获取登录用户的个人资料照片、将其上载至 OneDrive,和发送一封包含指向照片的共享链接的电子邮件。You can use the Connected Services in Visual Studio to configure your app to call the Microsoft Graph API. This article describes how to get a signed in user's profile photo, upload it to OneDrive, and send an email with a sharing link to the photo.

开始设置Get set up

若要通过 Microsoft Graph 使用 Office 365 连接服务,你需要执行以下操作:To use the Office 365 Connected Services with Microsoft Graph, you'll need to do the following:

  • 如果未下载,请下载 Visual Studio 2017 Preview。如果你使用的是 Visual Studio 的早期版本,你可以将 Visual Studio 2017 Preview 与你的当前版本并排使用。Download the Visual Studio 2017 Preview, if you haven't already. If you're using an earlier version of Visual Studio, you can use Visual Studio 2017 Preview side by side with your current version.

  • 获取 Microsoft 365 订阅。若要获取免费试用版,请加入 Microsoft 365 开发人员计划Get a Microsoft 365 subscription. To get a free trial, join the Microsoft 365 Developer program.

获取初学者项目Get the starter project

下载 Microsoft Graph ASP.NET 已连接服务示例。此示例包括针对 Microsoft Graph 进行身份验证所需的引用。下载初学者项目后,请解压,然后在 Visual Studio 2017 Preview 中打开示例。Download the Microsoft Graph ASP.NET Connected Services Sample. This sample includes the references that you need to authenticate against Microsoft Graph. After you download the starter project, unzip, and open the sample in Visual Studio 2017 Preview.

添加已连接服务Add the Connected Service

你现在可以将 Microsoft Graph 服务添加到 Visual Studio 项目中。You're now ready to add the Microsoft Graph service to your Visual Studio project.

  1. 在解决方案资源管理器中,选择“连接的服务”打开“连接的服务”选项卡。In Solution Explorer, choose Connected Services to open the Connected Services tab.

  2. 选择“使用 Microsoft Graph 访问 Microsoft 365 服务”提供程序。Choose the Access Microsoft 365 services with Microsoft Graph provider. 按照向导操作。Follow the wizard. 选择以下权限(你可以在以后更改):Select the following permissions (you can change the permissions later):

    • 对于“文件”API,将权限设置为“拥有对文件的完全访问权限”。For the File APIs, set permissions to Have full access to your files.
    • 对于“邮件”API,将权限设置为“以你的身份发送邮件”。For the Mail APIs, set permissions to Send mail as you.
    • 对于“用户”API,将权限设置为“登录并读取你的个人资料”。For the User APIs, set permissions to Sign you in and read your profile.

调用 Microsoft Graph APICall the Microsoft Graph API

初学者示例被配置为发送简单的电子邮件。你可以使用 Microsoft Graph 更新示例,从而发送一封包含指向 OneDrive 中已登录用户的个人资料照片的链接的电子邮件。The starter sample is configured to send a simple email. You can use Microsoft Graph to update the sample to send an email with a link to the signed-in user's profile photo in OneDrive.

  1. 转到托管可调用 Microsoft Graph 的代码的“Models\GraphService.cs”。Go to 'Models\GraphService.cs', which hosts the code to call Microsoft Graph.

  2. 按以下方法查找对 SDK 的调用并 取消评论。这演示了如何调用 Microsoft Graph 以获取个人资料照片、将文件上载至 OneDrive 和获取共享链接。Find and Uncomment calls to the SDK in the following methods. This shows how to call Microsoft Graph to get a profile photo, upload a file to OneDrive, and get a sharing link.

        GetCurrentUserPhotoStream(GraphServiceClient graphClient)
    
        UploadFileToOneDrive(GraphServiceClient graphClient, byte[] file)
    
        GetSharingLink(GraphServiceClient graphClient, string Id)
    

提示: 每条评论均以“//Uncomment:”开头Tip: Each comment starts with '//Uncomment:'

运行示例Run the sample

生成和运行示例Build and run the sample. 接下来,选择右上角的“登录”链接,然后依次选择“获取电子邮件地址”和“发送电子邮件”Next, choose the Sign-in link on the top right, and then choose Get email address followed by Send email.

这将发送一封电子邮件,其中包含指向你的个人资料照片的链接。This will send an email that includes a link to your profile photo.

注意:Notes:

  • 如果停止并从 Visual Studio 中重新运行示例,你可能需要明确注销才能使示例运行。If you stop and rerun the sample from Visual Studio, you might need to explicitly sign out for the sample to work.
  • 如果你收到异常,指示用户未通过身份验证,则你可能需要重复添加连接的服务步骤。If you get an exception that indicates that the User is not authenticated, you might need to repeat the Add the Connected Service step.
  • 确保使用与你在向导的 选择域 步骤中选择的相同域中的帐户登录。Make sure to sign in using an account in the same domain as the one you selected in the Select Domain step of the wizard.

浏览代码Explore the code

你现在可以使用 Visual Studio 2017 连接并配置你的服务。初学者示例为你创建基架和引用。You can now use Visual Studio 2017 to connect to and configure your services. The starter sample creates the scaffolding and references for you.

初学者示例包括以下文件:The starter sample includes the following files:

需要帮助?Need help?

如果需要帮助,请在 Microsoft Q&A 上发布你的问题。If you need help, post your questions on Microsoft Q&A. 使用 {microsoft-graph-identity} 标记你发布的问题。Tag your post with {microsoft-graph-identity}.