Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
AfsarP1
Helper IV
Helper IV

Managing cookies

Hello, 

 

I'm having an issue with managing cookies. I'm successfully retrieving cookies by sending the access_token as an Authorization request Headers:

 

let

Cookies= Web.Contents("https://domain/api/cookie", [Headers=[Authorization= "Bearer "&Access_Token("password", "token", "code")[access_token], #"Content-Type"="application/json"]]),

 

CookiesResponse = Cookies,

 

Once the cookies are received, I'm trying to send them as an Authorization request Headers to make an API call along with the Bearer token and other parameters:

 

apidata = 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"]])

in

apidata

 

Note: Access_Token is a function that gets me the access_token/Bearer.  

 

Now with this code, I'm successfully getting cookies but my API call(apidata) is asking for credentials which shouldn't be the case. The Authorization should complete with the Headers being sent. Does anyone have an idea of why this could happen or if there is a better way to manage/send cookies with your API calls?

 

The same thing works completely fine in Postman.

 

Appreciate your response. 

8 REPLIES 8
AfsarP1
Helper IV
Helper IV

Hello Team

 

I'm still chasing for the information on cookies. I would appreciate any response.

AfsarP1
Helper IV
Helper IV

Its been quite some time and I'm still trying to get information on this. I know we would go out of vacation soon or some would already be on vacation. I would appreciate any inputs before we all head out to our vacation. 

 

Wishing you all happy holidays. 

AfsarP1
Helper IV
Helper IV

Does anyone from the community know a better way to connect with Microsoft team to get information on this topic. If anyone has any information, please do share. 

 

AfsarP1
Helper IV
Helper IV

@Team I've explored everything there is on the internet about cookies using power queries. There is very limited or no information. There have been a very few posts in the past about cookies asking different questions. Looks like a handful of connector might have tried using cookies so far but there is no concrete information online. Could someone from the Microsoft Team, let me know if there is a way to use cookies in Power Query. 

If someone from the community has done it, I would appreciate your inputs.  

AfsarP1
Helper IV
Helper IV

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.

Hi @AfsarP1 

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.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


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"

 

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. 

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.