示例#1
0
        public async Task <string> SendGetRequest(string resource)
        {
            try
            {
                var client = new HttpClient();
                var token  = await _identitiyHelper.GetToken();

                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
                client.DefaultRequestHeaders.Add("Accept", "application/json");

                var requestUri = $"{BaseGraphApiUri}{resource}";

                var response = await client.GetAsync(new Uri(requestUri));

                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception("We could not send the message. Status Code: " + response.StatusCode);
                }

                return(await response.Content.ReadAsStringAsync());
            }
            catch (Exception e)
            {
                throw new Exception("We could not send the message: " + e.Message);
            }
        }
示例#2
0
        // Get an authenticated Microsoft Graph Service client.
        public GraphServiceClient GetAuthenticatedClient()
        {
            _graphClient = new GraphServiceClient(
                new DelegateAuthenticationProvider(
                    async requestMessage =>
            {
                var accessToken = await _identitiyHelper.GetToken();

                // Append the access token to the request.
                requestMessage.Headers.Authorization =
                    new AuthenticationHeaderValue("bearer", accessToken);
            }));
            return(_graphClient);
        }
示例#3
0
        public async Task <string> GetApplications()
        {
            var graphApiLink = "https://graph.microsoft.com/beta/deviceAppManagement/mobileApps";

            try
            {
                HttpClient client = new HttpClient();
                var        token  = await _identitiyHelper.GetToken();

                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
                client.DefaultRequestHeaders.Add("Accept", "application/json");
                // Build contents of post body and convert to StringContent object.
                // Using line breaks for readability.
                //string postBody = "{'Message':{"
                //                  + "'Body':{ "
                //                  + "'Content': '" + bodyContentWithSharingLink + "',"
                //                  + "'ContentType':'HTML'},"
                //                  + "'Subject':'" + subject + "',"
                //                  + "'ToRecipients':[" + recipientsJSON + "],"
                //                  + "'Attachments':[" + attachments + "]},"
                //                  + "'SaveToSentItems':true}";

                //var emailBody = new StringContent(postBody, System.Text.Encoding.UTF8, "application/json");

                HttpResponseMessage response = await client.GetAsync(new Uri(graphApiLink));

                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception("We could not send the message: " + response.StatusCode);
                }


                return(await response.Content.ReadAsStringAsync());
            }

            catch (Exception e)
            {
                throw new Exception("We could not send the message: " + e.Message);
            }
        }