Code Monkey home page Code Monkey logo

data-catalog-dotnet-get-started's Introduction

services platforms author
data-catalog
dotnet
dvana

#Get started with Azure Data Catalog

This sample shows you how to Register, Search, and Delete a data asset using the Data Catalog REST API.

data-catalog-dotnet-get-started's People

Contributors

acomsmpbot avatar dvana avatar spelluru avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

data-catalog-dotnet-get-started's Issues

How to get a client ID?

It is not clear how to get a client ID to run the getting started program. I tried assigning the variable clientIDFromAzureAppRegistration using a client ID from an app that I already had from https://apps.dev.microsoft.com/#/appList. When I ran the getting started program, I was able to go through most of the authentication windows, but it failed with a window that said
"Sign In
Sorry, but we’re having trouble signing you in.
We received a bad request.
Additional technical information:
Correlation ID: 63b87501-9df5-44dd-9c74-de9f470bed12
Timestamp: 2016-11-21 20:07:14Z
AADSTS65005: The client application has requested access to resource 'https://api.azuredatacatalog.com'. This request has failed because the client has not specified this resource in its requiredResourceAccess list."
I do not know what to do next.

Application Token based Request returning not 401 Unauthorized

I have a background application which is supposed to connect to Data Catalog and fetch information related to a table. Since its a background application, I cannot have any user intervention to authenticate the Data Catalog request. So I am trying to authenticate using ClientID and ClientSecret instead.

`string authorityUri = "https://login.microsoftonline.com/{adtenanthere}";
string resourceUri = "https://datacatalog.azure.com";
string clientId = "{clientidhere}";
string clientSecret = "{clientsecrethere}";

AuthenticationContext authContext1 = new AuthenticationContext(authorityUri, false);
ClientCredential credential = new ClientCredential(clientId, clientSecret);
AuthenticationResult authResult = authContext1.AcquireTokenAsync(resourceUri, credential).Result;
string token = authResult.CreateAuthorizationHeader();`

Getting the token using this method is being successful, however when I use this token for the Data Catalog API request, the request returns "401 - Unauthorized: Access is denied due to invalid credentials. You do not have permission to view this directory or page using the credentials you supplied".

Please suggest what I could be missing here!

The remote server returned an error: (500) Internal Server Error.

I was trying to use the sample code to try and connect to my catalog, but I always get the Internal Server Error in SetRequestAndGetResponse method. What am, I doing wrong here?

using System;
using System.Text;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;

namespace ConsoleApplication
{
class Program
{
static string clientIDFromAzureAppRegistration = "5de08262-e46e-46e0-b3a5-b90de753d1ee";

    static AuthenticationResult authResult = null;

    //Note: This example uses the "DefaultCatalog" keyword to update the user's default catalog.  You may alternately
    //specify the actual catalog name.
    static string catalogName = "Sales Data Hub Catalog";

    static void Main(string[] args)
    {
        var id = RegisterDataAsset(SampleJson("vw_Fax"));
        Console.WriteLine("Registered data asset. Press Enter to continue");
        Console.ReadLine();

        // Get an asset
        var item = GetDataAsset(id);
        Console.WriteLine("Read data asset. Press Enter to continue");
        Console.ReadLine();

        //Search a name
        string searchTerm = "name:=OrdersSample";

        string searchJson = SearchDataAsset(searchTerm);

        //Save to search JSON so that you can examine the JSON
        //  The json is saved in the \bin\debug folder of the sample app path
        //  For example, C:\Projects\Data Catalog\Samples\Get started creating a Data Catalog app\bin\Debug\searchJson.txt
        File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "searchJson.txt", searchJson);

        Console.WriteLine(searchJson);

        Console.WriteLine();
        Console.WriteLine("Searched data asset. Press Enter to continue");
        Console.ReadLine();

        Console.WriteLine("Delete data asset. Press Enter to continue");

        //DeleteDataAsset(id, item["etag"].ToString());

        Console.ReadLine();
    }

    //Get access token:
    // To call a Data Catalog REST operation, create an instance of AuthenticationContext and call AcquireToken
    // AuthenticationContext is part of the Active Directory Authentication Library NuGet package
    // To install the Active Directory Authentication Library NuGet package in Visual Studio, 
    //  run "Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory" from the NuGet Package Manager Console.
    static AuthenticationResult AccessToken()
    {
        if (authResult == null)
        {
            //Resource Uri for Data Catalog API
            string resourceUri = "https://api.azuredatacatalog.com";

            //To learn how to register a client app and get a Client ID, see https://msdn.microsoft.com/en-us/library/azure/mt403303.aspx#clientID   
            string clientId = clientIDFromAzureAppRegistration;

            //A redirect uri gives AAD more details about the specific application that it will authenticate.
            //Since a client app does not have an external service to redirect to, this Uri is the standard placeholder for a client app.
            string redirectUri = "https://login.live.com/oauth20_desktop.srf";

            // Create an instance of AuthenticationContext to acquire an Azure access token
            // OAuth2 authority Uri
            string authorityUri = "https://login.windows.net/common/oauth2/authorize";
            AuthenticationContext authContext = new AuthenticationContext(authorityUri);

            // Call AcquireToken to get an Azure token from Azure Active Directory token issuance endpoint
            //  AcquireToken takes a Client Id that Azure AD creates when you register your client app.
            authResult = authContext.AcquireToken(resourceUri, clientId, new Uri(redirectUri), PromptBehavior.RefreshSession);
        }

        return authResult;
    }

    //Register data asset:
    // The Register Data Asset operation registers a new data asset 
    // or updates an existing one if an asset with the same identity already exists. 
    static string RegisterDataAsset(string json)
    {
        string dataAssetHeader = string.Empty;

        string fullUri = string.Format("https://api.azuredatacatalog.com/catalogs/{0}/views/tables?api-version=2016-03-30", catalogName);

        //Create a POST WebRequest as a Json content type
        HttpWebRequest request = System.Net.WebRequest.Create(fullUri) as System.Net.HttpWebRequest;
        request.KeepAlive = true;
        request.Method = "POST";

        try
        {
            var response = SetRequestAndGetResponse(request, json);

            //Get the Response header which contains the data asset ID
            //The format is: tables/{data asset ID} 
            dataAssetHeader = response.Headers["Location"];
        }
        catch(WebException ex)
        {
            
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.Status);
            if (ex.Response != null)
            {
                // can use ex.Response.Status, .StatusDescription
                if (ex.Response.ContentLength != 0)
                {
                    using (var stream = ex.Response.GetResponseStream())
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            Console.WriteLine(reader.ReadToEnd());
                        }
                    }
                }
            }
            return null;
        }
        return dataAssetHeader;
    }

    //Get data asset:
    // The Get Data Asset operation retrieves data asset by Id
    static JObject GetDataAsset(string assetUrl)
    {
        string fullUri = string.Format("{0}?api-version=2016-03-30", assetUrl);

        //Create a GET WebRequest as a Json content type
        HttpWebRequest request = WebRequest.Create(fullUri) as HttpWebRequest;

        request.KeepAlive = true;
        request.Method = "GET";
        request.Accept = "application/json;adc.metadata=full";

        try
        {
            var response = SetRequestAndGetResponse(request);
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                var itemPayload = reader.ReadToEnd();
                Console.WriteLine(itemPayload);
                return JObject.Parse(itemPayload);
            }
        }
        catch (WebException ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.Status);
            if (ex.Response != null)
            {
                // can use ex.Response.Status, .StatusDescription
                if (ex.Response.ContentLength != 0)
                {
                    using (var stream = ex.Response.GetResponseStream())
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            Console.WriteLine(reader.ReadToEnd());
                        }
                    }
                }
            }
        }

        return null;
    }

    //Search data asset:
    //The Search Data Asset operation searches over data assets based on the search terms provided.
    static string SearchDataAsset(string searchTerm)
    {
        string responseContent = string.Empty;

        //NOTE: To find the Catalog Name, sign into Azure Data Catalog, and choose User. You will see a list of Catalog names.          
        string fullUri =
            string.Format("https://api.azuredatacatalog.com/catalogs/{0}/search/search?searchTerms={1}&count=10&api-version=2016-03-30", catalogName, searchTerm);

        //Create a GET WebRequest
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUri);
        request.Method = "GET";

        try
        {
            //Get HttpWebResponse from GET request
            using (HttpWebResponse httpResponse = SetRequestAndGetResponse(request))
            {
                //Get StreamReader that holds the response stream
                using (StreamReader reader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
                {
                    responseContent = reader.ReadToEnd();
                }
            }
        }
        catch (WebException ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.Status);
            if (ex.Response != null)
            {
                // can use ex.Response.Status, .StatusDescription
                if (ex.Response.ContentLength != 0)
                {
                    using (var stream = ex.Response.GetResponseStream())
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            Console.WriteLine(reader.ReadToEnd());
                        }
                    }
                }
            }
            return null;
        }

        return responseContent;
    }

    //Delete data asset:
    // The Delete Data Asset operation deletes a data asset and all annotations (if any) attached to it. 
    static string DeleteDataAsset(string dataAssetUrl, string etag = null)
    {
        string responseStatusCode = string.Empty;

        //NOTE: To find the Catalog Name, sign into Azure Data Catalog, and choose User. You will see a list of Catalog names.          
        string fullUri = string.Format("{0}?api-version=2016-03-30", dataAssetUrl);

        //Create a DELETE WebRequest as a Json content type
        HttpWebRequest request = System.Net.WebRequest.Create(fullUri) as System.Net.HttpWebRequest;
        request.KeepAlive = true;
        request.Method = "DELETE";

        if (etag != null)
        {
            request.Headers.Add("If-Match", string.Format(@"W/""{0}""", etag));
        }

        try
        {
            //Get HttpWebResponse from GET request
            using (HttpWebResponse response = SetRequestAndGetResponse(request))
            {
                responseStatusCode = response.StatusCode.ToString();
            }
        }
        catch (WebException ex)
        {


            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.Status);
            if (ex.Response != null)
            {
                // can use ex.Response.Status, .StatusDescription
                if (ex.Response.ContentLength != 0)
                {
                    using (var stream = ex.Response.GetResponseStream())
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            Console.WriteLine(reader.ReadToEnd());
                        }
                    }
                }
            }
            return null;
        }

        return responseStatusCode;
    }

    static HttpWebResponse SetRequestAndGetResponse(HttpWebRequest request, string payload = null)
    {
        while (true)
        {
            //To authorize the operation call, you need an access token which is part of the Authorization header
            request.Headers.Add("Authorization", AccessToken().CreateAuthorizationHeader());
            //Set to false to be able to intercept redirects
            request.AllowAutoRedirect = false;

            if (!string.IsNullOrEmpty(payload))
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(payload);
                request.ContentLength = byteArray.Length;
                request.ContentType = "application/json";
                //Write JSON byte[] into a Stream

                request.GetRequestStream().Write(byteArray, 0, byteArray.Length);

            }
            else
            {
                request.ContentLength = 0;
            }

           // HttpWebResponse response = request.GetResponse() as HttpWebResponse;

            HttpWebResponse response;

            try
            {
                response = request.GetResponse() as HttpWebResponse;
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
            }

            // Requests to **Azure Data Catalog (ADC)** may return an HTTP 302 response to indicate
            // redirection to a different endpoint. In response to a 302, the caller must re-issue
            // the request to the URL specified by the Location response header. 
            if (response.StatusCode == HttpStatusCode.Redirect)
            {
                string redirectedUrl = response.Headers["Location"];
                HttpWebRequest nextRequest = WebRequest.Create(redirectedUrl) as HttpWebRequest;
                nextRequest.Method = request.Method;
                request = nextRequest;
            }
            else
            {
                return response;
            }
        }
    }

    static string SampleJson(string name)
    {
        return string.Format(@"
             {{ ""properties"" : {{
    ""fromSourceSystem"" : false,
    ""name"": ""{0}"",
    ""dataSource"": {{
        ""sourceType"": ""SQL Server"",
        ""objectType"": ""Table"",
    }},
    ""dsl"": {{
        ""protocol"": ""tds"",
        ""authentication"": ""windows"",
        ""address"": {{
            ""server"": ""ckmdpqk6jp.database.windows.net"",
            ""database"": ""msxppods"",
            ""schema"": ""dbo"",
            ""object"": ""{0}""
        }}
    }},
    ""lastRegisteredBy"": {{
        ""upn"": """",
        ""firstName"": """",
        ""lastName"": """"
    }}
}},
   ""annotations"" : {{
    ""schema"": {{
        ""properties"" : {{
            ""fromSourceSystem"" : false
        }}
    }}
   }}

   }}
   ", name);
         }
    }
}

Order key word for search api

How to make sure the order of search? I would like to use increasing start page to pull all the data, however, looks like many times api call would return different result sets.

Thanks a lot!

Microsoft.IdentityModel.Clients.ActiveDirectory

Latest version 4.3 does not work. Make sure you install version 3.17.0 from Nuget.

I also get a 401 when trying to use PostMan, I suspect there is something related to the Data Catalog API and versioning with the outward APIs as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.