This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. We're covering it all. You won't want to miss it.
Learn moreLevel up your Power BI skills this month - build one visual each week and tell better stories with data! Get started
this is new code. please help. erro 401!
Considera este código como final pf
# Importar bibliotecas necessárias
import requests
import json
import pandas as pd
import msal
import os
import time
# Especificar Parâmetros
# Service Principal
client_id = '6b2c7699-67e4-446b-85c5-785cde6cac61'
client_secret = 'O4k8Q~bPhbRsH2JzlQkJ0JKT6K4EjFfHJfSy6dtL'
# Tenant Specification
tenant_id = '10dd0fe4-58c7-43c5-a7c3-2a1bf4aae087'
authority_url = 'https://login.microsoftonline.com/' + tenant_id
scope = ['https://analysis.windows.net/powerbi/api/.default']
# Report Specification
workspace_id = '817d8d31-721c-4526-bffd-7fb26042ac62'
report_id = '216452d6-d109-431c-a30c-d424c7f857e8'
base_url = 'https://api.powerbi.com/v1.0/myorg/groups/' + workspace_id + '/reports/' + report_id
format = 'PDF'
# Report File Specification
path = ''
# Use MSAL para obter o token
app = msal.ConfidentialClientApplication(client_id, authority=authority_url, client_credential=client_secret)
result = app.acquire_token_for_client(scopes=scope)
if 'access_token' in result:
access_token = result['access_token']
header = {'Content-Type': 'application/json', 'Authorization': f'Bearer {access_token}'}
# Exportar relatório para PDF
export_url = base_url + '/ExportTo'
body = {"format": format}
api_call = requests.post(export_url, headers=header, json=body)
print(api_call)
if api_call.status_code == 202: # 202 indica que a exportação foi aceita
export_id = api_call.json()['id']
url_status = base_url + f'/exports/{export_id}'
# Aguardar até que a exportação tenha sucesso
while True:
status_call = requests.get(url_status, headers=header)
status = status_call.json().get('status', '')
if status == 'Succeeded':
break
elif status == 'Failed':
print("A exportação falhou.")
break
# Aguardar um curto período antes de verificar novamente o status
time.sleep(1)
# Se a exportação foi bem-sucedida, salvar o PDF
if status == 'Succeeded':
url_save = base_url + f'/exports/{export_id}/file'
api_call = requests.get(url_save, headers=header)
if api_call.status_code == 200:
completename = os.path.join(path, f'report_{report_id}.pdf')
with open(completename, 'wb') as f:
f.write(api_call.content)
print(f"Relatório salvo como PDF: {completename}")
else:
print("Erro ao salvar o PDF.")
else:
print(f"Erro na solicitação de exportação: {api_call.status_code}, {api_call.text}")
else:
print(f"Erro ao obter token de acesso: {result.get('error_description', result)}")
That code looks better. At what point do you get the 401?
I can get the token, after the token it gives me the error. Don't you have to have a pro license? thanks
your code covers the partial process of requesting a PPTX export, not PDF. It also misses all the other steps - monitoring rendering progress and then fetching the result of the rendering.
Correct your code and add the missing steps - or use Power Automate which hides all that complexity from you.
Sign up to receive a private message when registration opens and key events begin.
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 1 | |
| 1 | |
| 1 |
| User | Count |
|---|---|
| 7 | |
| 4 | |
| 3 | |
| 2 | |
| 2 |