Forum Discussion
Anonymous
7 years agoNot applicable
Connecting to Box
My company uses Box to store all its files. My PowerBI file references excel documents which are stored on Box. Currently the work around we are using is to download (or sync) the files to our pers...
bryce-haboian85
1 year agoFrequent Visitor
Hi Anonymous,
As for OAuth 2.0 with JSON Web Tokens (Server Authentication), I haven't had much luck due to encryption issues. However, if you are able to generate a Box App with the Client Credentials Grant auth type, here is a snippet I've had success with:
let
base_url = "https://api.box.com",
// Define the parameters (you can modify these as inputs)
client_id = "#####",
client_secret = "#####",
grant_type = "#####",
box_subject_type = "#####",
box_subject_id = "#####",
// Construct the request body in JSON format by concatenating the variables
body =
"{" &
"""client_id"": """ & client_id & """, " &
"""client_secret"": """ & client_secret & """, " &
"""grant_type"": """ & grant_type & """, " &
"""box_subject_type"": """ & box_subject_type & """, " &
"""box_subject_id"": """ & box_subject_id & """ " &
"}",
// Send the POST request with the constructed JSON body
jsonResponse_auth = Json.Document(Web.Contents(base_url, [
RelativePath = "/oauth2/token",
Headers = [#"Content-Type"="application/json"],
Content = Text.ToBinary(body)
])),
// Extract only the "access_token" from the JSON response
access_token = jsonResponse_auth[access_token],
// Get File Information
file_id = "#####",
relative_path = "/2.0/files/" & file_id,
jsonResponse = Json.Document(Web.Contents(base_url, [
RelativePath = relative_path, Headers = [#"Content-Type"="application/json", #"authorization"="Bearer " & access_token]
])),
// Extract modified at
modified_at = jsonResponse[modified_at],
modified_at_record = Table.FromList({modified_at}, Splitter.SplitByNothing(), {"Modified At"}),
// Convert text to datetime (remove timezone)
#"Change Type" = Table.TransformColumns(modified_at_record, {{"Modified At", each
let
Position = Text.PositionOfAny(_, {"-", "+"}, Occurrence.Last),
CleanedText = Text.Middle(_, 0, Position) // Fixed: use Position directly
in
DateTime.FromText(CleanedText)
}}),
#"Changed Type" = Table.TransformColumnTypes(#"Change Type",{{"Modified At", type datetime}})
in
#"Changed Type"
In my case, I needed to call the Get file information endpoint to retrieve when the file was last modified at. Also, for publishing this to the PBI portal, I needed to add this connection to my gateway with the Authentication method set to "Anonymous", "Skip test connection" box checked, and "Privacy level" set to "Organizational".
Unsure if you are still having this issue but I thought I'd share my own experience.