电源Bi UpdateDatasourceWithHttpMessagesAsync操作返回了无效的状态码'BadRequest'

时间:2020-11-01 17:55:10

标签: c# azure powerbi

我正在尝试使用方法UpdateDatasourceWithHttpMessagesAsync更新Power Bi中的数据集凭据。但是我遇到了问题操作返回了无效的状态代码“ BadRequest” 。我在Azure API中添加了以下权限。请帮忙。

enter image description here

public static HttpStatusCode UpdateCredentials(string datasetId, string reportId, string userName, string password, string workspaceId)
    {
        var tokenCredentials = GetTokenCredentials();

        var updateDatasourceRequest = new UpdateDatasourceRequest()
        {
            CredentialDetails = new CredentialDetails()
            {
                Credentials = "{\'credentialData\':[{\'name\':\'username\', \'value\':\' " + userName + "\'},{\'name\':\'password\', \'value\':\'" + password + "\'}]}",
                CredentialType = "Basic",
                EncryptedConnection = "Encrypted",
                EncryptionAlgorithm = "None",
                PrivacyLevel = "None"
            }
        };

        string restUrlDatasetToUpdate = ApiUrl + "v1.0/myorg/datasets/" + datasetId + "/";
        string restUrlDatasetDefaultGateway = restUrlDatasetToUpdate + "Default.GetBoundGatewayDataSources";
        string jsonDefaultGateway = ExecuteGetRequest(restUrlDatasetDefaultGateway, tokenCredentials.Item2);
        Gateway defaultGateway = (JsonConvert.DeserializeObject<GatewayCollection>(jsonDefaultGateway)).value[0];
        string restUrlPatchCredentials = ApiUrl + "v1.0/myorg/" + "gateways/" + defaultGateway.gatewayId + "/" + "datasources/" + defaultGateway.id + "/";
        DataSourceCredentials dataSourceCredentials =
        new DataSourceCredentials
        {
            credentialType = "Basic",
            basicCredentials = new BasicCredentials
            {
                Username = userName,
                Password = password
            }
        }; 
        using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials.Item1))
        {
            var res = client.Gateways.UpdateDatasourceWithHttpMessagesAsync(defaultGateway.gatewayId, defaultGateway.id, updateDatasourceRequest).Result;//facing issue here                
            return res.Response.StatusCode;
        }

    }

0 个答案:

没有答案