{"id":3099,"date":"2023-03-09T14:07:04","date_gmt":"2023-03-09T22:07:04","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/surface-duo\/?p=3099"},"modified":"2023-03-09T14:07:04","modified_gmt":"2023-03-09T22:07:04","slug":"microsoft-graph-android","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/surface-duo\/microsoft-graph-android\/","title":{"rendered":"Introduction to Microsoft Graph for Android"},"content":{"rendered":"<p>\n  Hello Android developers,\n<\/p>\n<p>\n  Today we\u2019ll be continuing our blog series on using the Microsoft Authentication Library (MSAL) and the Microsoft Graph to connect to M365 services in your own Android apps.\n<\/p>\n<p>\n  Last week, we covered the <a href=\"https:\/\/devblogs.microsoft.com\/surface-duo\/android-msal\/\">basics of MSAL<\/a>, so be sure to check that out first!\n<\/p>\n<p>\n  This week, we\u2019ll focus on the Microsoft Graph, specifically:\n<\/p>\n<ul>\n<li>\n    The basics of MS Graph\n  <\/li>\n<li>\n    Exploring the MS Graph API with Graph Explorer\n  <\/li>\n<li>\n    Adding MS Graph API permissions to your app registration\n  <\/li>\n<\/ul>\n<p>\n  Next week, after you have a better understanding of the Microsoft Graph and how to add permissions to your app registration, we\u2019ll show you how to call the MS Graph API from an Android app!\n<\/p>\n<h2>Microsoft Graph Overview<\/h2>\n<p>\n  The <a href=\"https:\/\/learn.microsoft.com\/graph\/overview\">Microsoft Graph<\/a> is a common set of APIs that allows developers to get user data from a common <a href=\"https:\/\/learn.microsoft.com\/microsoft-365\/solutions\/tenant-management-overview?view=o365-worldwide#a-microsoft-365-tenant-defined\">Microsoft tenant<\/a>. Depending on the tenant, you can access different kinds of information. For example, you can access your own data in a personal Microsoft tenant, and access data and analytics across your company in a work-based Microsoft tenant.\n<\/p>\n<p>\n  With MS Graph, you can access a wide range of information from M365, Windows, and Enterprise Mobility + Security services, like calendar events, user profile information, Teams meeting data, and Viva Insights analytics. These APIs can be accessed from a variety of platforms, including Android, to enhance a user&#8217;s experience with personalized data. To learn more, check out the full list of <a href=\"https:\/\/learn.microsoft.com\/graph\/overview-major-services\">MS Graph services and features<\/a>.\n<\/p>\n<p>\n  <img decoding=\"async\" width=\"975\" height=\"477\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/diagram-description-automatically-generated.png\" class=\"wp-image-3100\" alt=\"\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/diagram-description-automatically-generated.png 975w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/diagram-description-automatically-generated-300x147.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/diagram-description-automatically-generated-768x376.png 768w\" sizes=\"(max-width: 975px) 100vw, 975px\" \/>\n<\/p>\n<p><em>Figure 1. Visualization of the Microsoft Graph and the information it exposes.<\/em>\n<\/p>\n<p>\n  Data from the M365 platform can be accessed in three main ways: with the Microsoft Graph API, <a href=\"https:\/\/learn.microsoft.com\/graph\/connecting-external-content-connectors-overview\">connectors<\/a>, or <a href=\"https:\/\/learn.microsoft.com\/graph\/data-connect-concept-overview\">Data Connect<\/a>. Today, we\u2019ll be focusing on the MS Graph API, which uses REST APIs and SDKs to access the <code>https:\/\/graph.microsoft.com<\/code> endpoint.\n<\/p>\n<h2>Graph Explorer<\/h2>\n<p>\n  The best way to start familiarizing yourself with the MS Graph API is to check out the <a href=\"https:\/\/developer.microsoft.com\/graph\/graph-explorer\">Graph Explorer<\/a>! This online tool helps you visualize and test out different API calls, inspect request\/response bodies, and see what permissions are required.\n<\/p>\n<p>\n  To use the Graph Explorer, you must follow a few simple steps:\n<\/p>\n<ol>\n<li>\n  Sign in\n<\/li>\n<li>\n  Select a query\n<\/li>\n<li>\n  Consent to permissions\n<\/li>\n<li>\n  Run a query\n<\/li>\n<\/ol>\n<h3>Sign in<\/h3>\n<p>\n  The first step is to sign in your Microsoft account! We would recommend using a personal account, if possible, because work\/school accounts may have stricter restrictions on how users can use MS Graph.\n<\/p>\n<p>\n  <img decoding=\"async\" width=\"3007\" height=\"1693\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-1.png\" class=\"wp-image-3101\" alt=\"\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-1.png 3007w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-1-300x169.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-1-1024x577.png 1024w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-1-768x432.png 768w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-1-1536x865.png 1536w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-1-2048x1153.png 2048w\" sizes=\"(max-width: 3007px) 100vw, 3007px\" \/>\n<\/p>\n<p><em>Figure 2. Before making calls with the Graph Explorer, you need to sign in with a Microsoft account.<\/em>\n<\/p>\n<h3>Select a query<\/h3>\n<p>\n  The next step is to select a query to run, which you can do by searching through the <em>Sample queries<\/em> pane on the lefthand side of the screen. As you can see, the queries are sorted by which resources they access, and they help give you an idea of what can be accomplished with the MS Graph API. You can check out the <a href=\"https:\/\/learn.microsoft.com\/graph\/api\/overview?view=graph-rest-1.0\">Microsoft Graph REST API v1.0 documentation<\/a> for the full list of possible queries.\n<\/p>\n<p>\n  The pre-generated sample queries provide a great starting point for building requests, since they already fill out the target resource URL and provide sample request bodies. You can then start to customize requests by modifying the endpoint or request body \u2013 in some cases, such as a POST request to create a new OneNote page (Figure 3), this may even be necessary for the request to succeed.\n<\/p>\n<p>\n  <img decoding=\"async\" width=\"2528\" height=\"1192\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-2.png\" class=\"wp-image-3102\" alt=\"\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-2.png 2528w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-2-300x141.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-2-1024x483.png 1024w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-2-768x362.png 768w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-2-1536x724.png 1536w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-2-2048x966.png 2048w\" sizes=\"(max-width: 2528px) 100vw, 2528px\" \/>\n<\/p>\n<p><em>Figure 3. In Graph Explorer, you can start out with a sample query and then modify the endpoint or request body to customize the request. For example, when trying to create a new page in OneNote, you can customize the page HTML in the request body, and you need to change the highlighted \u201csection-id\u201d placeholder to a specific section ID from one of your notebooks.<\/em>\n<\/p>\n<h3>Consent to permissions<\/h3>\n<p>\n  In order to run the query you\u2019ve selected, you will also need to grant MS Graph the permissions to read\/write to different resources. Otherwise, you\u2019ll run into a <code>Forbidden -\u00a0403. Access denied<\/code> error when trying to make a request. \n<\/p>\n<p>\n  There are two ways you can grant the proper permission(s):\n<\/p>\n<ul>\n<li>\n    If you already know what calls you\u2019ll be making, then you can click on your profile photo, click on <em>Consent to permissions<\/em>, and then search for and consent to the permissions you need (Figure 4).\n  <\/li>\n<\/ul>\n<p><em><img decoding=\"async\" width=\"1299\" height=\"756\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/word-image-3099-4.png\" class=\"wp-image-3103\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/word-image-3099-4.png 1299w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/word-image-3099-4-300x175.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/word-image-3099-4-1024x596.png 1024w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/word-image-3099-4-768x447.png 768w\" sizes=\"(max-width: 1299px) 100vw, 1299px\" \/><\/em>\n<\/p>\n<p><em>Figure 4. In the \u201cConsent to permissions\u201d view, you can search for and grant various MS Graph permissions.<\/em>\n<\/p>\n<ul>\n<li>\n    If you don\u2019t know ahead of time which calls you\u2019ll make, you can grant permissions as you go. If you prepare a query and then switch to the <em>Modify permissions<\/em> tab, you can view and consent to all the permissions required for the request (Figure 5). \n  <\/li>\n<\/ul>\n<p>\n  <img decoding=\"async\" width=\"2171\" height=\"1264\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-3.png\" class=\"wp-image-3104\" alt=\"Graphical user interface, text, application, email\n\nDescription automatically generated\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-3.png 2171w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-3-300x175.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-3-1024x596.png 1024w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-3-768x447.png 768w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-3-1536x894.png 1536w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-3-2048x1192.png 2048w\" sizes=\"(max-width: 2171px) 100vw, 2171px\" \/>\n<\/p>\n<p><em>Figure 5. Before running a query, you can also check the \u201cModify permissions\u201d tab for the permissions you need to grant.<\/em>\n<\/p>\n<h3>Run a query<\/h3>\n<p>\n  Finally, once you\u2019re logged in and have granted the proper permissions, you just have to click \u201cRun query\u201d!\n<\/p>\n<p>\n  <img decoding=\"async\" width=\"1859\" height=\"1097\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-4.png\" class=\"wp-image-3105\" alt=\"Graphical user interface, text, application, email\n\nDescription automatically generated\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-4.png 1859w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-4-300x177.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-4-1024x604.png 1024w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-4-768x453.png 768w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-application-email-4-1536x906.png 1536w\" sizes=\"(max-width: 1859px) 100vw, 1859px\" \/>\n<\/p>\n<p><em>Figure 6. Once your query has run, you can inspect the request body\/headers, as well as the response preview and code snippets for how to perform the same call with different MS Graph SDKs.<\/em>\n<\/p>\n<p>\n  You can inspect both the request and response details, view code snippets for how to make the same request with SDKs, and find links to API documentation. This really comes in handy when you need to know how to format request bodies\/headers and how to parse response information.\n<\/p>\n<h2>Adding permissions to your app registration<\/h2>\n<p>\n  Now that you know that basics of the Microsoft Graph API and the kinds of requests you can make (thanks Graph Explorer \ud83d\ude01), we\u2019ll move on to how you can start using MS Graph in an Android app.\n<\/p>\n<p>\n  The first step is related to an MSAL topic we covered last week: <a href=\"https:\/\/devblogs.microsoft.com\/surface-duo\/android-msal\/#register-an-app-in-the-azure-portal\">registering an app in the Azure Portal<\/a>. In Graph Explorer, we had to consent to permissions to make sure we could read\/write to different resources. We have to do the same thing now in our app registration to let our app make requests to the MS Graph API.\n<\/p>\n<p>\n  You can use the Graph Explorer or the <a href=\"https:\/\/learn.microsoft.com\/graph\/permissions-reference\">Microsoft Graph permissions reference<\/a> to figure out what permissions your app will need, then in Azure Portal, you can add these under the <em>API permissions<\/em> section (Figure 6). In most cases, you will need to add <em>delegated permissions<\/em>, which mean you\u2019re accessing the API as the signed-in user. <em>Application permissions<\/em> are for the cases when your app is running as a background service without a signed-in user.\n<\/p>\n<p>\n  <img decoding=\"async\" width=\"1431\" height=\"688\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-email-description.png\" class=\"wp-image-3106\" alt=\"\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-email-description.png 1431w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-email-description-300x144.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-email-description-1024x492.png 1024w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/03\/graphical-user-interface-text-email-description-768x369.png 768w\" sizes=\"(max-width: 1431px) 100vw, 1431px\" \/>\n  <br \/><em>Figure 7. In the API permissions section in Azure Portal, you can add the necessary permissions for MS Graph requests to your app registration. This example app contains the permissions for reading and writing OneNote information.<\/em>\n<\/p>\n<p>\n  Once you\u2019ve updated your app registration, users will be prompted the first time they sign in to consent to the permissions you added to your app.\n<\/p>\n<p>\n  So, to review, in this post we learned the basics of the Microsoft Graph, how to build and test requests with Graph Explorer, and how to add MS Graph API permissions to an app registration in Azure Portal. Stay tuned for next week\u2019s post, where we\u2019ll combine today\u2019s learnings with our MSAL lessons from last week to show you how to call the Microsoft Graph API in an Android app!\n<\/p>\n<h2>Resources and feedback<\/h2>\n<p>\n  To learn more about using MS Graph in Android apps, check out:\n<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/category\/microsoft-graph\/\">Microsoft Graph DevBlog<\/a>\n  <\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/azure\/active-directory\/develop\/tutorial-v2-android\">Tutorial: Sign in users and call the Microsoft Graph from an Android application<\/a>\n  <\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/azure\/active-directory\/develop\/mobile-app-quickstart?pivots=devlang-android\">Quickstart: Sign in users and call the Microsoft Graph API from a mobile application<\/a>\n  <\/li>\n<li><a href=\"https:\/\/github.com\/Azure-Samples\/ms-identity-android-java\/\">Quickstart sample repo<\/a> \n  <\/li>\n<\/ul>\n<p>\n  If you have any questions, or would like to tell us about your apps, use the <a href=\"http:\/\/aka.ms\/SurfaceDuoSDK-Feedback\">feedback forum<\/a> or message us on <a href=\"https:\/\/twitter.com\/surfaceduodev\">Twitter @surfaceduodev<\/a>.\n<\/p>\n<p>\n  Finally, there won\u2019t be a livestream this week, but check out the\u00a0<a href=\"https:\/\/youtube.com\/c\/surfaceduodev\" target=\"_blank\" rel=\"noopener\">archives on YouTube<\/a>. We\u2019ll see you online again soon!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Android developers, Today we\u2019ll be continuing our blog series on using the Microsoft Authentication Library (MSAL) and the Microsoft Graph to connect to M365 services in your own Android apps. Last week, we covered the basics of MSAL, so be sure to check that out first! This week, we\u2019ll focus on the Microsoft Graph, [&hellip;]<\/p>\n","protected":false},"author":72597,"featured_media":3100,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[732,473,731],"class_list":["post-3099","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-surface-duo-sdk","tag-graph","tag-kotlin","tag-msal"],"acf":[],"blog_post_summary":"<p>Hello Android developers, Today we\u2019ll be continuing our blog series on using the Microsoft Authentication Library (MSAL) and the Microsoft Graph to connect to M365 services in your own Android apps. Last week, we covered the basics of MSAL, so be sure to check that out first! This week, we\u2019ll focus on the Microsoft Graph, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts\/3099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/users\/72597"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/comments?post=3099"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts\/3099\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/media\/3100"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/media?parent=3099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/categories?post=3099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/tags?post=3099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}