public static string GetAsyncListData(string RestQry)
{
try
{
var json = string.Empty;
string strSiteURL = Convert.ToString(ConfigurationManager.AppSettings["SiteURL"]);
SharePointOnlineCredentials credentials = SPHelper.GetSPHttpHandler();
string realm = TokenHelper.GetRealmFromTargetUrl(new Uri(strSiteURL));
string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, new Uri(strSiteURL).Authority, realm).AccessToken;
using (var handler = new HttpClientHandler() { UseDefaultCredentials = false })// Credentials = credentials
{
var uri = new Uri(strSiteURL);
//handler.CookieContainer.SetCookies(uri, credentials.GetAuthenticationCookie(uri));
using (var client = new HttpClient(handler))
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
// client.DefaultRequestHeaders.Add("X-API-Version", "2");
var response = client.GetAsync(string.Format(RestQry, strSiteURL)).Result;
response.EnsureSuccessStatusCode();
json = response.Content.ReadAsStringAsync().Result;
return json;
//client.BaseAddress = new Uri(endpointURL);
//client.DefaultRequestHeaders.Accept.Clear();
//client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//client.DefaultRequestHeaders.Add("apikey", apikey);
}
}
}
catch (Exception ex)
{
ExceptionLogger.ErrorLog(ex.StackTrace, ex.Message, ex.Source, "Projects:GetAsyncListData", Convert.ToString(ex.InnerException), "");
return string.Empty;
}
No comments:
Post a Comment