GetTokenForUserAsync() public static method

Get Token for User.
public static GetTokenForUserAsync ( ) : Task
return Task
        internal static async Task <string> SetExtensionValueAsync()
        {
            string endpoint = serviceEndpoint + "/me";
            var    token    = await AuthenticationHelper.GetTokenForUserAsync();

            string extName   = domainName + "_hrprofile";
            string propName  = "p1";
            string propValue = "value";

            JObject extensionValue = new JObject
            {
                {
                    extName, new JObject {
                        { propName, propValue }
                    }
                },
            };

            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(new HttpMethod("PATCH"), endpoint))
                {
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

                    // This header has been added to identify our sample in the Microsoft Graph service. If extracting this code for your project please remove.
                    request.Headers.Add("SampleID", "aspnet-connect-rest-sample");
                    request.Content = new StringContent(JsonConvert.SerializeObject(extensionValue), Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = await client.SendAsync(request))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            return("Success");
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        // Deletes an existing group in the tenant.
        public static async Task <bool> DeleteGroupAsync(string groupId)
        {
            bool eventDeleted = false;

            try
            {
                HttpClient client = new HttpClient();
                var        token  = await AuthenticationHelper.GetTokenForUserAsync();

                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);

                // Endpoint for the specified group
                Uri eventEndpoint = new Uri(serviceEndpoint + "myOrganization/groups/" + groupId);

                HttpResponseMessage response = await client.DeleteAsync(eventEndpoint);

                if (response.IsSuccessStatusCode)
                {
                    eventDeleted = true;
                    Debug.WriteLine("Deleted group: " + groupId);
                }

                else
                {
                    Debug.WriteLine("We could not delete the group. The request returned this status code: " + response.StatusCode);
                    eventDeleted = false;
                }
            }

            catch (Exception e)
            {
                Debug.WriteLine("We could not delete the group: " + e.Message);
                eventDeleted = false;
            }

            return(eventDeleted);
        }
Esempio n. 3
0
        // Downloads the content of an existing file.
        public static async Task <Stream> DownloadFileAsync(string fileId)
        {
            Stream fileContent = null;

            try
            {
                HttpClient client = new HttpClient();
                var        token  = await AuthenticationHelper.GetTokenForUserAsync();

                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);

                // Endpoint for content in an existing file. Use "/me/drive/root/children/<file name>/content" if you know the name but not the Id.
                Uri fileEndpoint = new Uri(serviceEndpoint + "me/drive/items/" + fileId + "/content");

                HttpResponseMessage response = await client.GetAsync(fileEndpoint);

                if (response.IsSuccessStatusCode)
                {
                    fileContent = await response.Content.ReadAsStreamAsync();

                    Debug.WriteLine("Downloaded file: " + fileId);
                }
                else
                {
                    Debug.WriteLine("We could not download the file. The request returned this status code: " + response.StatusCode);
                    return(null);
                }
            }

            catch (Exception e)
            {
                Debug.WriteLine("We could not download the file. The request returned this status code: " + e.Message);
                return(null);
            }

            return(fileContent);
        }
Esempio n. 4
0
        // Deletes a file in the user's root directory.
        public static async Task <bool> DeleteFileAsync(string fileId)
        {
            bool fileDeleted = false;

            try
            {
                HttpClient client = new HttpClient();
                var        token  = await AuthenticationHelper.GetTokenForUserAsync();

                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);

                // Endpoint for the file to delete.
                Uri fileEndpoint = new Uri(serviceEndpoint + "me/drive/items/" + fileId);

                HttpResponseMessage response = await client.DeleteAsync(fileEndpoint);

                if (response.IsSuccessStatusCode)
                {
                    fileDeleted = true;
                    Debug.WriteLine("Deleted file Id: " + fileId);
                }
                else
                {
                    Debug.WriteLine("We could not delete the file. The request returned this status code: " + response.StatusCode);
                    fileDeleted = false;
                }
            }

            catch (Exception e)
            {
                Debug.WriteLine("We could not delete the file. The request returned this status code: " + e.Message);
                fileDeleted = false;
            }

            return(fileDeleted);
        }
        internal static async Task <string> SequentialBatchCallAsync()
        {
            var token = await AuthenticationHelper.GetTokenForUserAsync();

            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, Endpoint))
                {
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    JArray batchRequests = new JArray
                    {
                        new JObject
                        {
                            { "url", "/me/events?$top=2" },
                            { "method", "GET" },
                            { "id", "1" }
                        },
                        new JObject
                        {
                            { "url", "/me/drive/root/children" },
                            { "method", "POST" },
                            { "id", "2" },
                            { "body", new JObject
                              {
                                  { "name", "BatchingTestFolder" },
                                  { "folder", new JObject() }
                              } },
                            { "headers", new JObject {
                                  { "Content-Type", "application/json" }
                              } },
                            { "dependsOn", new JArray("1") }
                        },
                        new JObject
                        {
                            { "url", "/me/drive/root/children/BatchingTestFolder" },
                            { "method", "GET" },
                            { "id", "3" },
                            { "dependsOn", new JArray("2") }
                        },
                        new JObject
                        {
                            { "url", "/me/drive/root/children/BatchingTestFolder" },
                            { "method", "DELETE" },
                            { "id", "4" },
                            { "dependsOn", new JArray("3") }
                        }
                    };

                    JObject batchPayload = new JObject(new JProperty("requests", batchRequests));

                    request.Content = new StringContent(JsonConvert.SerializeObject(batchPayload), Encoding.UTF8, "application/json");
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

                    // This header has been added to identify our sample in the Microsoft Graph service. If extracting this code for your project please remove.
                    request.Headers.Add("SampleID", "aspnet-connect-rest-sample");

                    using (HttpResponseMessage response = await client.SendAsync(request))
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            var json = JObject.Parse(await response.Content.ReadAsStringAsync());
                            return(json.ToString());
                        }
                        else
                        {
                            Debug.WriteLine("We could not run sequential batch request. The request returned this status code: " + response.StatusCode);
                            return(null);
                        }
                    }
                }
            }
        }