我正在尝试以编程方式将语义模型绑定到网关。 我发现下面的rest api使它看起来可行,但我遇到了一个我似乎还无法解决的问题。
https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/bind-to-gateway-in-group#code-try-0
下面是我尝试使用的代码,我希望它能够成功完成并绑定网关。
public static void BindGatewaytoDataset(string groupId, string datasetId, string bearer)
{
var client = new RestClient($"https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets/{datasetId}/Default.BindToGateway");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + bearer);
var gatewayObjectID = new Guid("GATEWAYOBJECTID");
var datasourceObjectIds = new Guid("DATASOURCE");
request.AddJsonBody(new
{
gatewayObjectId = gatewayObjectID,
datasourceObjectIds = datasourceObjectIds
});
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
// The request was successful
Console.WriteLine("Gateway assigned to dataset successfully.");
}
else
{
// The request failed
Console.WriteLine($"Failed to assign gateway to dataset: {response.StatusCode}");
}
}
我收到的错误似乎与 datasourceObjectIds 和 api 希望它成为一个列表有关。 下面是那个错误。
{"error":{"code":"BadRequest","message":"Bad Request","details":[{"message":"Error converting value \"DATASOURCEGUID\" to type 'System.Collections.Generic.IList`1[System.Guid]'. Path 'datasourceObjectIds', line 1, position 118.","target":"bindToGatewayRequest.datasourceObjectIds"}]}}
当我问这个问题时,我找到了答案,所以我将其发布在这里,以防其他人像我一样陷入困境。
将 var 数据源 id 更改为:
var datasourceObjectIds = new List { new Guid("DATASOURCE GUID") };