Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago

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

  • Anonymous's avatar
    Anonymous
    Not applicable

    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.

    • PhilipTreacy's avatar
      PhilipTreacy
      Super 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.

      • Anonymous's avatar
        Anonymous
        Not 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"

         

  • Anonymous's avatar
    Anonymous
    Not applicable

    @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.  

  • Anonymous's avatar
    Anonymous
    Not applicable

    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. 

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    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. 

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hello Team

     

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