none
How to call Cognitive Services via PQ using my own Cognitive Services account RRS feed

  • Question

  • I keep getting:  Web.Contents with the Content option is only supported when connecting anonymously.
    // GetTranslatorAccessToken
    let
        EmptyContent = Text.ToBinary("", TextEncoding.Ascii),
       
        Response =
            Web.Contents("https://api.cognitive.microsoft.com/sts/v1.0/issueToken",
                [
                    Headers =
                        [
                            #"Content-Type"="application/json",
                            Accept="application/jwt",
                            #"Ocp-Apim-Subscription-Key"= TranslatorKey
                        ],
                    Content=EmptyContent
                ]),
        #"Imported Text" = Table.FromColumns({Lines.FromBinary(Response,null,null,20127)}),
        Res = #"Imported Text"{0}[Column1]
    in
        Res
    // TranslatorKey
    "b110bc78-7bbe-471c-a24a-5a6c629c18e7" meta [IsParameterQuery=true, Type="Text", IsParameterQueryRequired=true]
    // FnTranslateText
    (textToTranslate, TranslateTolanguage) =>   
    let
        URL = "https://api.microsofttranslator.com/v2/http.svc/Translate?appid=Bearer " & GetTranslatorAccessToken & "&text=" & textToTranslate & "&to=" & TranslateTolanguage,
        Response = Web.Contents(
            URL,
            [
                Headers = [Accept="application/xml"]
            ]
        ),
        XML = Xml.Tables(Response, null, 65001),
        TranslatedText = XML{0}[#"Element:Text"]
    in
        TranslatedText

    Sunday, January 22, 2017 3:01 PM

Answers

All replies

  • This limitation is for security reasons. What type of authentication is it? If that's acceptable to you, you might be able to work around this issue by constructing the Authorization header manually.
    Monday, January 23, 2017 3:29 PM
  • How do I do that?

    // GetTranslatorAccessToken
    let
        EmptyContent = Text.ToBinary("", TextEncoding.Ascii),
       
        Response =
            Web.Contents("https://api.cognitive.microsoft.com/sts/v1.0/issueToken",
                [
                    Headers =
                        [
                            #"Content-Type"="application/json",
                            Accept="application/jwt",
                            #"Ocp-Apim-Subscription-Key"= TranslatorKey
                        ],
                    Content=EmptyContent
                ]),
        #"Imported Text" = Table.FromColumns({Lines.FromBinary(Response,null,null,20127)}),
        Res = #"Imported Text"{0}[Column1]
    in
        Res
    // TranslatorKey
    "b110bc78-7bbe-471c-a24a-5a6c629c18e7" meta [IsParameterQuery=true, Type="Text", IsParameterQueryRequired=true]
    // FnTranslateText
    (textToTranslate, TranslateTolanguage) =>   
    let
        URL = "https://api.microsofttranslator.com/v2/http.svc/Translate?appid=Bearer " & GetTranslatorAccessToken & "&text=" & textToTranslate & "&to=" & TranslateTolanguage,
        Response = Web.Contents(
            URL,
            [
                Headers = [Accept="application/xml"]
            ]
        ),
        XML = Xml.Tables(Response, null, 65001),
        TranslatedText = XML{0}[#"Element:Text"]
    in
        TranslatedText

    Tuesday, January 24, 2017 10:15 AM
  • Curt,

    How do I do that?

    Friday, January 27, 2017 9:42 AM
  • Well, what kind of authentication is it? If it's OAuth, then this may not be practical.
    Tuesday, January 31, 2017 2:40 PM