Forum Discussion
Managing cookies
Here is a powershell script that is working:
$cloudUrl = "domain"
$requestHeaders = @{
Host=$cloudUrl
Authorization="Bearer $bearerToken"
}
#Get the tenant cookie
$cookieResponse = Invoke-WebRequest -Method Get -Uri "https://$cloudUrl//api/cookie" -Headers $requestHeaders -SessionVariable cookieSession -UseBasicParsing
$tenantCookie = $cookieSession.Cookies.GetCookies("https://$cloudUrl") | where Name -eq "CurrentAssessment"
#Call API endpoint
$apiEndpoint = "systems"
#$apiEndpoint = "access"
$apiResponse = Invoke-WebRequest -Method Get -Uri "https://$cloudUrl/api/$apiEndpoint" -WebSession $cookieSession -Headers $requestHeaders -UseBasicParsing
$apiResponse.Content
I'm not familiar with powershell. However, since I have some experience in Java and other languages, I tried to understand what this script is doing. I feel the first #Get the tenant cookie is working for me in my POWER QUERY but I don't understand this:
SessionVariable cookieSession -UseBasicParsing
and
$tenantCookie = $cookieSession.Cookies.GetCookies("https://$cloudUrl") | where Name -eq "CurrentAssessment"
If someone could make me understand how to use these in power query, I will figure out the rest of it. Appreciate any inputs.
- PhilipTreacy5 years agoSuper User
Hi Anonymous
The authorization should be specified as #"Authorization" . Try this
Cookies= Web.Contents("https://domain/api/cookie", [Headers=[#"Authorization"= "Bearer "&Access_Token("password", "token", "code")[access_token], #"Content-Type"="application/json"]])Phil
If I answered your question please mark my post as the solution.
If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.- Anonymous5 years agoNot applicable
Hi PhilipTreacy
Thanks for the reply.
Unfortunately, changing it to #"Authorization" did not make any difference. I'm still receiving the same error. The error is actually from the API call to our systems:
= Web.Contents("https://domain/api/systems", [Headers =[#"Authorization"= "Bearer "&Access_Token("password", "token", "code")[access_token], Cookie="CookiesResponse", #"Accept"="application/json", #"Accept-Encoding"="gzip, deflate, br"]])
Though I'm trying to send cookies here, it still asks me for sign in credentials which shouldn't be the case. I feel like the cookies are not being accepted maybe because they are not in right format or something. As a result for cookies, I get the domain as result in power query(example abc.domain.com) and if I try opening it in any format like Json, CSV, HTML etc it has nothing.
Cookies are returning results with both Authorization and #"Authorization". Don't we use #"" if there is any space in the name. Example Authorization-sample becomes #"Authorization-sample".- Anonymous5 years agoNot applicable
Does anyone have an idea on how Power BI works with cookies. I've tried sending the CookieResponse, yet it still doesn't seem to work.