Guest User

Untitled

a guest
Jan 27th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. public async Task<string> getWebTitle(string webUrl, string usr, string psw)
  2. {
  3. //Creating Password
  4. const string PWD = psw;
  5. const string USER = usr;
  6. const string RESTURL = "{0}/_api/web?$select=Title";
  7.  
  8. //Creating Credentials
  9. var passWord = new SecureString();
  10. foreach (var c in PWD) passWord.AppendChar(c);
  11. var credential = new SharePointOnlineCredentials(USER, passWord);
  12.  
  13.  
  14. //Creating Handler to allows the client to use credentials and cookie
  15. using (var handler = new HttpClientHandler() { Credentials = credential })
  16. {
  17. //Getting authentication cookies
  18. Uri uri = new Uri(webUrl);
  19. handler.CookieContainer.SetCookies(uri, credential.GetAuthenticationCookie(uri));
  20.  
  21. //Invoking REST API
  22. using (var client = new HttpClient(handler))
  23. {
  24. client.DefaultRequestHeaders.Accept.Clear();
  25. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  26.  
  27. HttpResponseMessage response = await client.GetAsync(string.Format(RESTURL, webUrl)).ConfigureAwait(false);
  28. response.EnsureSuccessStatusCode();
  29.  
  30. string jsonData = await response.Content.ReadAsStringAsync();
  31.  
  32.  
  33. return jsonData;
  34.  
  35. }
  36. }
  37. }
Add Comment
Please, Sign In to add comment