Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
Anonymous
Not applicable

Python script to load REST API output

We have an application that just made REST API available. 

We want to extract data from here and make nice dashboards in PowerBI. So the final goal for this is to be realtime. 

Now the token for the REST API expires every 60 minutes. So we made a python script that fetches the token. The final output is a json file. How can i feed this output directly in powerbi instead of saving the file to my local workstation? 

 

Sample Code: 

 

 

import ssl
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import requests.auth
import json
from json import dumps, loads
import logging
import argparse
import sys

#install python-requests
#from swagger import Swagger

# Disable all SSL Cert checking
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


################################################################################
# INPUT PARAMTERS

CLIENT_ID="xxxxxxx"
CLIENT_SECRET="xxxxxx"
CTF_URL="https://www.cxxx.com"
PROJECT_ID="xxxxxx" # you can use trackerID here as well
OUT_DIR="C:\RestApi Results"
################################################################################

SCOPE="urn:ctf:services:ctf urn:ctf:services:soap60" 
#grant_type=password&client_id=api-client&scope=$scope&username=$username&password=$password" $site_url/sf/auth/token

def getToken():
try:
post_data = {"grant_type": "password"
,"client_id": "api-client"
,"scope": SCOPE
,"username": CLIENT_ID
,"password": CLIENT_SECRET}

response = requests.post(CTF_URL + "/sf/auth/token", data=post_data, verify=False)
token_json = response.json()
return token_json["access_token"]
except (RuntimeError, TypeError, NameError, KeyError):
print "Invalid Token"
sys.exit(1)

 

def main():
url = CTF_URL+"/ctfrest/tracker/v1/artifacts?containerid="+PROJECT_ID+"&includeIconLinks=false&sortBy=-lastModifiedDate&offset=0&count=-1&fullPageInfo=true"
#url = CTF_URL+"ctfrest/foundation/v1/projects/"+PROJECT_ID
header = {"Authorization": "Bearer " + getToken() , "Content-Type": "application/json","Accept": "application/json", "If-Match":"*"}
response = requests.get(url, verify =False, headers=header)

print response.json()
with open(OUT_DIR+'data.json', 'w') as outfile:
json.dump(response.json(), outfile)

if __name__ == '__main__':
main()

 

2 REPLIES 2
v-chuncz-msft
Community Support
Community Support

@Anonymous,

 

You may take a look at Using Python in Power Query Editor and research The Advanced Editor.

Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

We have an application that just made REST API available. 

We want to extract data from here and make nice dashboards in PowerBI. So the final goal for this is to be realtime. 

Now the token for the REST API expires every 60 minutes. So we made a python script that fetches the token. The final output is a json file. How can i feed this output directly in powerbi instead of saving the file to my local workstation? 

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.