[Azure DataCatalog]:从Azure数据目录词汇表中读取数据未经授权的错误?

时间:2017-09-29 05:29:59

标签: c# azure azure-data-catalog

我试图在天蓝色数据目录中发布excel数据。我是在控制台应用程序中编写的。

我的代码是

static void Main()
{
    string DefaultCatalog = "DefaultCatalog";
    string DefaultGlossary = "DefaultGlossary";
    string fullUri = string.Format("https://api.azuredatacatalog.com/catalogs/{0}/glossaries/{1}/terms?api-version=2016-03-30", 
                    DefaultCatalog, DefaultGlossary);
    HttpWebRequest request = WebRequest.Create(fullUri) as HttpWebRequest;
    request.KeepAlive = true;
    request.Method = "GET";
    request.Accept = "application/json;adc.metadata=full";
    request.Headers.Add("Authorization", AccessToken().Result.CreateAuthorizationHeader());
    request.AllowAutoRedirect = false;
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    if (response != null && response.StatusCode == HttpStatusCode.Redirect)
    {
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            var itemPayload = reader.ReadToEnd();
            JToken terms;
            JObject.Parse(itemPayload).TryGetValue("value", out terms);
            if (terms != null)
            {
                var r = JsonConvert.DeserializeObject<JArray>(terms.ToString());
            }
        }
    }
}

static async Task<AuthenticationResult> AccessToken()
{
    string clientId = "MyClientId";
    string client_secret = "MyClientSecret";
    string tenentId = "MytenentId";
    if (_authResult == null)
    {
        // Resource Uri for Data Catalog API
        string resourceUri = "https://api.azuredatacatalog.com/";
        string redirectUri = "https://login.live.com/oauth20_desktop.srf";
        string authorityUri = "https://login.windows.net/MytenentId/oauth2/authorize";
        AuthenticationContext authContext = new AuthenticationContext(authorityUri);

        _authResult = await authContext.AcquireTokenAsync(resourceUri, clientId, new Uri(redirectUri), new PlatformParameters(PromptBehavior.Always));

        //ClientCredential cc = new ClientCredential(clientId, client_secret);
        //_authResult = await authContext.AcquireTokenAsync(resourceUri, cc);

    }
    return _authResult;
}

我想从我的azure datacatalog中获取词汇表列表。但它总是返回未经授权的错误。 “远程服务器返回错误:(403)禁止。”

我的错误是

enter image description here

3 个答案:

答案 0 :(得分:1)

您需要使用以获取正确的令牌: string authorityUri =&#34; https://login.windows.net/common/oauth2/authorize&#34 ;;

希望这有帮助, 莫妮卡

答案 1 :(得分:1)

确保您位于词汇表管理员列表中。

此外,ADC在github中有很棒的代码示例,可以执行您想要的操作,请查看: https://github.com/Azure-Samples/data-catalog-bulk-import-glossary

答案 2 :(得分:0)

仅具有委托权限的数据目录。 但是,我尝试使用应用程序权限。因此,它被抛弃了Unauthorized.After,我被改为基于用户登录(委托权限)。

现在已修复。

感谢分享答案的@Monica和@Max