[Azure DataCatalog]: Read the data from Azure data catalog glossary Unauthorized error?

618 views Asked by At

I was try to post the excel data in azure data catalog. i was written in console Application.

my code is

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;
}

I want to get the list of glossary from my azure datacatalog. But it always returning Unauthorized error. "The remote server returned an error: (403) Forbidden."

my error is

enter image description here

3

There are 3 answers

0
umasankar On BEST ANSWER

The Data Catalog contains only delegate permission. But, I tried using Application permission. So, It throws Unauthorized after I changed it into user login based (Delegated Permission).

Now it's fixed.

Thanks for sharing the answer @Monica and @Max

0
Monica On

You'll need to use to get the correct token: string authorityUri = "https://login.windows.net/common/oauth2/authorize";

Hope this helps, Monica

0
max On

make sure you are in the list of glossary admins.

Also ADC has great code samples in github which does what you want to do, check it out: https://github.com/Azure-Samples/data-catalog-bulk-import-glossary