Postman es una herramienta valiosa para trabajar con API, especialmente cuando se realizan pruebas y solicitudes ad hoc fuera de una solución de producción automatizada. En términos de dónde un desarrollador de Power BI puede encontrar útil a Postman, se encuentra en algún lugar entre «Try it» from the documentation functionality and a more production-worthy solution that incorporates tools such as Azure DevOps, Logic Apps / Power Automate, a custom Power BI connector, and more.
The ideas in this publication extend a original post by Carl de Souza. Carl shows how to get an OAuth2 access token, but he does it with hard-coded values. Additional API requests use the token from the original response, but you also manually provide this token to subsequent API calls.
The following steps assume that you are already familiar with the basics of both Postman and the Power BI API. Sign up for a Power BI Azure Active Directory app si aún no tiene uno, y también debe tener el scope apropiado definido para sus tareas deseadas.
Add variables
Extension of the Postman technique to use variables en lugar de valores codificados, hace que el esfuerzo be más automático y conveniente. En particular, al pasar el token de acceso a una variable para su reutilización en otras solicitudes de API, se elimina una copia y pegado manual » [token]”Cada vez que se realiza una nueva solicitud después de que expira un token anterior.
En última instancia, el target es proporcionar un token de acceso a cualquier solicitud de API REST de Power BI sin tener que colocar manualmente el token de autorización en la solicitud. Lograr esto se basa en una variable de sesión (llamada temp_access_token in this case as seen in the screenshot).
To obtain this result, the first step is to create a new Environment in Postman. Then add five variables to replace the hard-coded values seen in Carl's original post. I've used environment variables so I can use them across multiple collections, but narrowing the scope to collection variables would work too.
Four variables are used in the request body to get the access token from https://login.microsoftonline.com/common/oauth2/token. These are just local session variables (specify Current value but do do not add sensitive data to Initial value).
1) nombre de Username - Organizational Power BI user
2) password - Power BI organization password
3) Identificación del client - Your ID Azure Active Directory application
4) client_secret - Secret of your Azure Active Directory application
NOTE: Postman does not currently have a way to protect, hide, or mask variable values. After working with Microsoft products like Azure Key Vault, you feel so vulnerable just storing a password in a variable. However, for use with the grant type of OAuth = password, session variables are a step up from encoding them in the body of a request. This is especially true if you are using Postman for teams and your workspace is in sync. Just keeping this data in Current value, at least it stays local.
The fifth variable, temp_access_token, is not specified manually like the other four. Instead you will use code in the Postman Tests tab to write the access token to that variable. Tests es un área para colocar código JavaScript que se ejecuta después de que ocurre la solicitud.
Get a Token
The initial API request gets the access token used in subsequent calls.
Add a new one Send application https://login.microsoftonline.com/common/oauth2/token
for Headers, add:
Key = Content-Type
Value = application / x-www-form-urlencoded
for Body, which includes the variable values inside {{}}, add:
grant_type = password & username = {{username}} & password = {{password}} & client_id = {{client_id}} & client_secret = {{client_secret}} & resource = https: //analysis.windows.net/powerbi/api
What follows is the most useful part. The code here saves you from having to manually copy and paste the bearer token in additional requests.
In Tests, add:
var token; try {var response = JSON.parse (responseBody);
token = response.access_token;
pm.environment.set ("temp_access_token", token);
} catch (err) {console.warn (err.message); }
The code used in Tests parses the JSON response and assigns the access_token provided by Microsoft to the temp_access_token variable.
A successful request will show the access_token and other data.
It will also write the access token to the appropriate variable.
Use the token in subsequent requests
Additional requests will depend on what you want to do with the Power BI API. However, the common step will involve passing the authorization header with the token stored as a variable.
for Headers, add:
Key = Authorization Value = Bearer {{temp_access_token}}
For example, I can use this setting to expand all workspace data in my tenant using the management / groups endpoint. Solo necesito el método, la Url y el encabezado de autorización dinámica. No se requiere copiar / pegar manualmente.
You could even extend this method to include additional variables for relative endpoint URLs and more!
If you have any questions, comment below and let me know if you use Postman and find this method helpful.
[embedyt] https://www.youtube.com/playlist?list=PLyWe5-Lx84g1JrDY6Zh_dfV2oO-Cy3frl&layout=gallery[/embedyt]
Payment links
Como asociado de Amazon, gano con las compras que califican.