Forum Discussion

Doc-tan's avatar
Doc-tan
Regular Visitor
7 months ago
Solved

Custom connector API - TOKEN (error binary)

I have create a custom connector but i can not see and use the token because of error

Details: "We cannot convert a value of type Binary to type Text."

at the postman i can post the same url with the same header and body and answer me with json 

{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJiZX",
    "refresh": "Srbwtr7J"
}
 
my code:
 
let
                        creds = Extension.CurrentCredential(),
                       
                        Response =
                       
                            Web.Contents(
                                    
                                    server &"SignIn",
                                    [
                                        Headers = [
                                                    #"Content-Type" = "application/json",
                                                    #"api-version" = "1.0"
                                                    ],
                                        Content =
                                                    Text.ToBinary(
                                                                    Json.FromValue([
                                                                    username = creds[Username],
                                                                    password = creds[Password],
                                                                    afm = "",
                                                                    code = ""
                                                                    ])
                                                                )
                                    ]
                                        ),

                       
                       
                        JsonResponse = Json.Document(Response),
                        Token =  JsonResponse[token]
  • I solved my problem with putting url, header and body in vars and used these in web.contents.

     

    let
                            creds = Extension.CurrentCredential(),
                            urlToken = Server &"SignIn",
                           
                            HeadersToken = [#"Content-Type" = "application/json",#"api-version" = "1.0"],
                            BodyToken = Json.FromValue([
                                                    username = creds[Username],
                                                    password = creds[Password],
                                                    afm = "",
                                                    code = ""
                                                    ]),


                            ResponseToken =
                             
                                Web.Contents(
                                        url,
                                        [
                                           Headers = HeadersToken ,
                                           Content = BodyToken
                                       
                                        ]
                                            ),
                            JsonResponseToken = Json.Document(ResponseToken),
                            Token =  JsonResponseToken[token]
                         
                       
                        in
                       
                        Token
     
     
    Thank for all your answers!!

6 Replies

  • v-hjannapu's avatar
    v-hjannapu
    Community Support

    Hi Doc-tan,
    Thank you  for reaching out to the Microsoft fabric community forum.

    The problem is happening inside the Power Query custom connector. In Power Query, the response coming from Web.Contents is treated as Binary by default. But later in the code, it’s being read like normal text/JSON. Because of this mismatch, Power Query throws the error saying it can’t convert Binary to Text, and the token is not getting read.

    So this is not related to headers, body, or login details. It’s only a response handling issue in the connector. Once the response is properly handled as JSON, you should be able to access the token without any problem.

    please go through with the below document hope it may resolve your Issue:
    Json.Document - PowerQuery M | Microsoft Learn
    Web.Contents - PowerQuery M | Microsoft Learn
    Hope the above provided information help you resolve the issue, if you have any further concerns or queries, please feel free to reach out to us.
    Regards,
    Community Support Team.

    • v-hjannapu's avatar
      v-hjannapu
      Community Support

      Hi Doc-tan,
      I hope the information provided above assists you in resolving the issue. If you have any additional questions or concerns, please do not hesitate to contact us. We are here to support you and will be happy to help with any further assistance you may need.

      Regards,
      Community Support Team.


  • Ray_Minds's avatar
    Ray_Minds
    Solution Supplier

    Hello Doc-tan  Please find the solution :
    Before : 



    The issue you’re experiencing with extracting thetoken from your custom connector is a common problem in Power Query when dealing with API responses.
    Even though theAPI worksfinein Postman, Power Query requires careful handling of binary content and JSON parsing.
    Here’s what’s happening:
    1. Binary Response:Web.Contentsalwaysreturns a binary.You cannot directly treat it as text or JSON. If you try to reference a key beforeparsing, Power Query throwserrors like“Cannot convert a value of type Binary to type Text”.
    2. JSONParsing: Onceyou parsethebinary with Json.Document, theoutput is a record, not text. To access thetoken field, you must ensure: oTheresponseis valid JSON. The key matches exactly (Power Query iscase-sensitive). In your API response, the key is"token", not "Token"

     

     

     

  • Doc-tan's avatar
    Doc-tan
    Regular Visitor

    I solved my problem with putting url, header and body in vars and used these in web.contents.

     

    let
                            creds = Extension.CurrentCredential(),
                            urlToken = Server &"SignIn",
                           
                            HeadersToken = [#"Content-Type" = "application/json",#"api-version" = "1.0"],
                            BodyToken = Json.FromValue([
                                                    username = creds[Username],
                                                    password = creds[Password],
                                                    afm = "",
                                                    code = ""
                                                    ]),


                            ResponseToken =
                             
                                Web.Contents(
                                        url,
                                        [
                                           Headers = HeadersToken ,
                                           Content = BodyToken
                                       
                                        ]
                                            ),
                            JsonResponseToken = Json.Document(ResponseToken),
                            Token =  JsonResponseToken[token]
                         
                       
                        in
                       
                        Token
     
     
    Thank for all your answers!!