power bi api_如何以编程方式访问Power BI REST API
power bi api
In this article, I am going to explain how to use the Power BI REST API to programmatically access the Power BI Service features. I’ll also discuss the basis of REST API and how to use the APIs using Postman. Using the Power BI REST API, you can programmatically manage the administrative services like getting a list of dashboards and workspaces, refreshing a dataset, copying a dashboard from one workspace to another, and so on.
在本文中,我将解释如何使用Power BI REST API以编程方式访问Power BI Service功能。 我还将讨论REST API的基础,以及如何使用Postman使用API。 使用Power BI REST API,您可以以编程方式管理管理服务,例如获取仪表板和工作区列表,刷新数据集,将仪表板从一个工作区复制到另一个工作区,等等。
In order to securely access the Power BI REST API, we first need to request an embed token using which we can call the APIs and execute our functions. Although several operations can be performed using the REST APIs, in this article, we will specifically investigate how to authorize an application and call the APIs using Postman.
为了安全地访问Power BI REST API,我们首先需要请求一个嵌入令牌,我们可以使用该令牌来调用API并执行我们的功能。 尽管可以使用REST API执行若干操作,但在本文中,我们将专门研究如何授权应用程序并使用Postman调用API。
什么是REST API? (What is a REST API?)
The abbreviation of REST stands for Representational State Transfer, and API stands for Application Programming Interface. Let us assume that you are trying to find the football scores for your favorite team. You open the browser, search for your team, and then hit enter, and the results are displayed on your screen as expected. A REST API also works in a similar fashion. Using the REST API, you can search for some items, and then the results will be returned from the service you initiated the request.
REST的缩写代表再表象小号泰特贸易交接,并且API代表A pplication P 我方法在AGC覆盖整个院落。 让我们假设您正在尝试查找最喜欢的球队的足球得分。 您打开浏览器,搜索您的团队,然后按Enter键,结果将按预期显示在屏幕上。 REST API也以类似的方式工作。 使用REST API,您可以搜索一些项目,然后结果将从您发起请求的服务中返回。
The REST API consists of simple anatomy as follows:
REST API包含以下简单的解剖结构:
- The Endpoint: This is the URL of the resource to which we are going to place the requests. For example, https://api.powerbi.com/v1.0/myorg/ 端点 :这是我们要向其发出请求的资源的URL。 例如,https://api.powerbi.com/v1.0/myorg/
- The Method: Although there are multiple methods to call a REST API, the most common ones are GET and POST. The GET is used to retrieve information while the POST is used to send information to the resource on the web 方法 :尽管有多种方法可以调用REST API,但最常见的方法是GET和POST。 GET用于检索信息,而POST用于将信息发送到Web上的资源
- The Headers: The authentication mode and the content types 标头 :身份验证模式和内容类型
- The Data (Body): Textual data in the form of JSON, which needs to be sent to the resource 数据(正文) :JSON形式的文本数据,需要发送到资源
In this article, we will be using a tool known as Postman for making the API calls. A postman is a free tool using which you can make GET or POST calls to various URLs and save the results in a collection. You can download the tool from the official website of Postman.
在本文中,我们将使用称为Postman的工具进行API调用。 邮递员是一个免费工具,您可以使用它来对各种URL进行GET或POST调用并将结果保存在集合中。 您可以从Postman的官方网站下载该工具。
注册Power BI的应用程序 (Register an application for Power BI)
In order to use the Power BI REST API, we need to create an application in the Power BI Developer’s portal. This app will provide us with a reusable Client ID and a Client Secret key, using which we can generate the embed token required for the REST API.
为了使用Power BI REST API,我们需要在Power BI开发人员门户中创建一个应用程序。 该应用程序将为我们提供可重用的客户端ID和客户端**,使用它们我们可以生成REST API所需的嵌入令牌。
Navigate to https://dev.powerbi.com/apps and login with your account credentials.
导航到https://dev.powerbi.com/apps并使用您的帐户凭据登录。

Once you sign in with your credentials, you will see your name appear in Step 1. Click on Next to continue.
使用凭据登录后,您将看到您的姓名出现在步骤1中。单击“ 下一步”继续。

In the next step, we need to register the application. Provide an Application Name for the app and choose an application type. There are two types of applications – native and server-side. For the purpose of this tutorial, I’m going to use the Server-side web application. Provide a Home Page URL and a Redirect URL, as shown in the figure below.
下一步,我们需要注册该应用程序。 提供应用程序的应用程序名称 ,然后选择应用程序类型。 有两种类型的应用程序-本机和服务器端。 就本教程而言,我将使用服务器端Web应用程序 。 提供一个主页URL和一个重定向URL,如下图所示。

If you scroll below, you can see that there are several API Access permissions that we need to define our app with. Since we are going to read the resources, for now, let’s just provide the basic read access required for reading the datasets, dashboards, reports, and the workspaces in the account. Click on Register once done.
如果您在下面滚动,则可以看到需要使用多个API访问权限来定义我们的应用。 由于我们现在将读取资源,因此,我们仅提供读取帐户中的数据集,仪表板,报告和工作区所需的基本读取访问权限。 完成后单击“ 注册” 。

Once you click on Register, the app will be created, and you will receive an application key and an application secret as well. Copy both the credentials to a secure location as we will be using it in our next steps while calling the Power BI REST API.
单击注册后,将创建该应用程序,并且您还将收到一个应用程序**和一个应用程序**。 将两个凭据都复制到一个安全位置,因为在调用Power BI REST API的下一步中将使用它。
授权用户 (Authorize User)
Once the app has been created, the next step is to authorize the user to the application using the Application ID and retrieve the Access Token. This Access Token will be used in the next step to call the other APIs.
创建应用程序后,下一步就是使用应用程序ID授权用户使用该应用程序并检索访问令牌。 下一步将使用此访问令牌来调用其他API。
In order to authorize the user, we need to allow the user to sign in to the app and retrieve the access token. Let us prepare the sign-in URL first. The URL is https://login.microsoftonline.com/common/oauth2/authorize, and it accepts the following parameters.
为了授权用户,我们需要允许用户登录该应用并检索访问令牌。 让我们首先准备登录URL。 该URL是https://login.microsoftonline.com/common/oauth2/authorize ,它接受以下参数。
- ClientID 客户编号
- ResponseType ResponseType
- RedirectURI 重定向URI
- ResponseMode ResponseMode
- Scope 范围
- State 州
A sample for the entire URL would be something like below.
整个URL的示例如下所示。
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
https://login.microsoftonline.com/common/oauth2/authorize?client_id= <<客户端ID>&response_type = code&redirect_uri = http:// localhost / redirect /&response_mode = query&scope = openid&state = 12345
Paste the URL in the browser and hit enter. A dialog box appears that displays the user consent required by the application to allow the necessary permissions. Click on Accept.
将网址粘贴到浏览器中,然后按Enter。 出现一个对话框,显示应用程序要求的用户同意,以允许必要的权限。 点击接受 。

Once the user is authorized, the page will be redirected to the Redirect URI mentioned in the API request. Ideally, this request has a code in it that can be used as the authorization method for securely calling the APIs. Since we do not have any web application running on our localhost, we will receive an error page like below. If you see the Requested URL section, there is some text after the word code. Copy the entire text and store it securely.
用户获得授权后,页面将被重定向到API请求中提到的重定向URI。 理想情况下,此请求中包含代码,可用作安全调用API的授权方法。 由于我们的本地主机上没有运行任何Web应用程序,因此我们将收到如下错误页面。 如果看到“ 请求的URL”部分,则单词code后面会有一些文本。 复制整个文本并安全地存储。
生成访问令牌 (Generate Access Token)
Now that we have obtained the code, let us go ahead and generate the access token. In order to generate the Access Token, we need to call an endpoint using the Post method with specific information that will return the access token. The resource URL is https://login.microsoftonline.com/common/oauth2/v2.0/token. The following fields need to be passed in the body of the request. We will be using Postman to call the endpoint.
现在我们已经获得了代码,让我们继续并生成访问令牌。 为了生成访问令牌,我们需要使用Post方法调用具有特定信息的端点,这些信息将返回访问令牌。 资源URL是https://login.microsoftonline.com/common/oauth2/v2.0/token 。 需要在请求正文中传递以下字段。 我们将使用Postman呼叫端点。
- ClientID 客户编号
- ClientSecret 客户秘密
- Scope 范围
- Code 码
- RedirectURL 重定向网址
- GrantScope GrantScope
When you call the above endpoint, the results are returned as follows. The access_token is the value that we need to use while calling the Power BI REST API. Copy and store the value of the access token to a secure location.
当您调用上述端点时,返回结果如下。 access_token是我们在调用Power BI REST API时需要使用的值。 将访问令牌的值复制并存储到安全位置。

获取报告列表 (Get List of Reports)
Now that we have obtained the access token, we can call the Power BI REST API and fetch the details as required. Let us try to get a list of all the reports in my personal workspace. The endpoint to fetch the list of reports is “https://api.powerbi.com/v1.0/myorg/reports”. You can refer to the official documentation from Microsoft regarding the available endpoints.
现在我们已经获得了访问令牌,我们可以调用Power BI REST API并根据需要获取详细信息。 让我们尝试获取我的个人工作区中所有报告的列表。 提取报告列表的端点是“ https://api.powerbi.com/v1.0/myorg/reports”。 您可以参考Microsoft的有关可用端点的官方文档 。
In order to call this endpoint, we need to pass the access token as an authorization method in the header of the GET request. You can refer to the figure below and generate your GET request.
为了调用此端点,我们需要在GET请求的标头中将访问令牌作为授权方法传递。 您可以参考下图并生成GET请求。
As you can see in the figure above, we have fetched a list of all the available reports in the workspace in a JSON format.
如上图所示,我们已经以JSON格式获取了工作区中所有可用报告的列表。
获取数据集列表 (Get List of Datasets)
Let us now try to fetch a list of all the datasets available in my workspace using the same method. The URL for fetching the list of datasets is “https://api.powerbi.com/v1.0/myorg/datasets”.
现在让我们尝试使用相同的方法来获取工作空间中所有可用数据集的列表。 用于获取数据集列表的URL是“ https://api.powerbi.com/v1.0/myorg/datasets”。
结论 (Conclusion)
In this article, I have explained the basics of how to use the Power BI REST API to manage the Power BI resources programmatically. Over the next few articles, I’ll discuss in more depth about the other methods that can be used by the Power BI REST API to manage various other resources in the Power BI workspaces.
在本文中,我解释了如何使用Power BI REST API来以编程方式管理Power BI资源的基础知识。 在接下来的几篇文章中,我将更深入地讨论Power BI REST API可用于管理Power BI工作区中各种其他资源的其他方法。
翻译自: https://www.sqlshack.com/how-to-access-power-bi-rest-apis-programmatically/
power bi api