Forum Discussion

Amar-Agnihotri's avatar
Amar-Agnihotri
Resolver I
4 years ago

Extra characters at the end of JSON input error while generating authorization code in Power Query

Hi,

I am trying to get authorization code by creating a function in Power Query. The scenario is below -

 

When i will run this URL in browser -

https://login.microsoftonline.com/common/oauth2/authorize?client_id=<<CLIENT_ID>>&response_type=code&redirect_uri=http://localhost/redirect/&response_mode=query&scope=openid&state=12345

where i put

client_id = my client id of application

redirect_uri = http://localhost:44300/

 

When i run this in URL - 

It redirects and generate a code as below - 

http://localhost:44300/?code=0.ATgAF3nbUXb1HkOaQLeUokaWSaDUHgKDeP1CgsAFZkgcyFE4ANc.AQABAAIAAAD--DLA3VO7QrddgJg7Wevr0yCOxvjLrzbMJRqo2OGozyX8MWoiDZakXFKh8p4sOpCrFlGWQZktqNL40GxslL2FJuHDajss9FI0Bd3XV7PeATb9oHQw34i2oSHwji6EkGC5Hb32i7vzuhSPeUeHNBjW-lDCJDckRPlFAQePw3NTxCJQKPebNl935pwB8A-OP0-w-v2oYxn5ZvM5DpdigSrKqnMF3SLGWHcay-BjwesUBt1uQc0j8PisgRDrZGUk5l4fCmJsvDDFlWcf2444s-jOav-3GQd74aWY9egFYbZfNKCoJ5rEyG6Eio8ADs8e7zPiYX2B_HOq5GllliXCJqyjxXxHOeUst6dp8_DwlDaXUQCfvNgky9rW2EnwWpa5Z0dl9Q8-aQrexJ32JR9QLtl9Kr5LScdCTnWbaMdH5dn9Rn8YTzdPPASz1jGuVFoKraQ5lEwFjhyPnTwuOr176N-QfQRuvWi-vt-WeY36yENJ2yiJPbcOyfKG3k04jtBZaRp2VKzsq84QyieZ1Ka9xXPgoV1oOLWF6OX0T4Qzpf0bL13xRuCfbocQA_Z8TCZ_rmi2ZdYh3Tmv_UxOss-1g15qIAA&state=12345&session_state=0fe638b0-553d-4360-a390-df8f19415caa

 

Now i want to use this code as an authorization code in another function which will get refreshable access token on every call. 

 

I did the below code in Power Query for the above requirement -

 

let
body = "client_id=021ed4a0--XXXX-XXXX-XXXX-05664XXcc851&response_type=code&redirect_uri=http://localhost:44300/&response_mode=query&scope=openid&state=12345",
Data= Json.Document(Web.Contents("https://login.microsoftonline.com/common/oauth2/authorize/",
[Headers=[#"Content-Type"="application/x-www-form-urlencoded"],
Content=Text.ToBinary(body)
]))
in
Data

 

But when i run this code i got this error

 

Can anybody help me to achieve this .

 

Thanks in advance 

 

 

2 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Amar-Agnihotri, could you try the following.  In the advanced editor screen, separate the Json.Document and Web.Contents into different Applied Steps.  This will allow you to see the Binary file received from the Web.Contents call.  In the advanced editor screen you can also try to insert this new step using Binary.ToText.  It might take a bit of trial and error, but the objective it see the JSON Document text as if you were viewing it in Visual Studio.  This will help you understand or see these extra characters that the Json.Document is getting confused by.  If there is a pattern, the extra characters can be removed, and then turn the text back to binary file for the Json.Document step (or otherwise get what you want from the binary.text).

    I hope this helps you find the solution.

    Daryl



     

    • Amar-Agnihotri's avatar
      Amar-Agnihotri
      Resolver I

      Anonymous  Hi ! can you please suggest whether i can use the same code in Dataflows or not because i searched and found that the redirect_uri is creating an issue because redirect_uri will not be supported in Power Bi desktop . It is mainly used for native or web applications. Now i am thinking of using the same in Dataflows. Will that work ? I am not sure about that , though.