klappy
klappy

Reputation: 35

Get bearer Token for Azure Blockchain project

I'm trying to create an application that automatically sends data to my Smartcontract on my Azure Blockchain Workbench.

The problem is, I do not understand how to get the bearer token. There is an example online where I can yee how to call the API with GET and POST requests. But I have to submit a client app ID, a client secret, and a resource ID. Where do I get them from?

thanks a lot for your help and ideas !!

class Program
{
    public static readonly string AUTHORITY = "https://login.microsoftonline.com/XXX";
    public static readonly string WORKBENCH_API_URL = "https://XXX-api.azurewebsites.net";
    public static readonly string RESOURCE = "XXX";
    public static readonly string CLIENT_APP_Id = "XXX";
    public static readonly string CLIENT_SECRET = "XXX";

    static async Task Main(string[] args)
    {
        AuthenticationContext authenticationContext = new AuthenticationContext(AUTHORITY);
        ClientCredential clientCredential = new ClientCredential(CLIENT_APP_Id, CLIENT_SECRET);

        // Sample API Call
        try
        {
            // Getting the token, it is recommended to call AcquireTokenAsync before every Workbench API call
            // The library takes care of refreshing the token when it expires
            var result = await authenticationContext.AcquireTokenAsync(RESOURCE, clientCredential).ConfigureAwait(false);

            Console.WriteLine(result.AccessToken);

            // Using token to call Workbench's API
            //HttpClient client = new HttpClient();
            //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
            //client.DefaultRequestHeaders
            //                .Accept
            //                .Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //// Get Users
            //var response = await client.GetAsync($"{WORKBENCH_API_URL}/api/v1/contracts");
            //var users = await response.Content.ReadAsStringAsync();


            var client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("Bearer", result.AccessToken);
            var content = await client.GetStringAsync($"{WORKBENCH_API_URL}/api/v1/contracts");


            Console.WriteLine(content);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
}

}

Upvotes: 1

Views: 133

Answers (1)

Jim Xu
Jim Xu

Reputation: 23111

According to my test, when we successfully created Azure blockchain workbench, we need to configure Azure AD when we access Azure blockchain workbench at the first time and we will create Azure AD application at the same time. The resource is the application ID or the app url of the Azure AD application. For more details, please refer to the document.

For example

  1. Access Azure Blockchain workbench

  2. Configure Azure AD enter image description here enter image description here

  3. Create a Service Principal to Access Workbench API

cd; Invoke-WebRequest -Uri https://aka.ms/createWorkbenchServicePrincipalScript -OutFile createWorkbenchServicePrincipal.ps1
./createWorkbenchServicePrincipal.ps1 -TenantName <the tenant you use above> -WorkbenchAppId <the appid you copy> -MakeAdmin (optional)

enter image description here

  1. Get token
Method: POST
URL: https://login.microsoftonline.com/<tenant id>/oauth2/token
Headers: Content-Type: application/x-www-form-urlencoded

Body:
     grant_type: client_credentials 
     client_id: <sp client id>
     client_secret:<sp client secret>
     resource: <the app id>

enter image description here

  1. Call rest api
URL: {WORKBENCH_API_URL}/api/v1/users
Headers: Authorization Bearer <access_token>

enter image description here

Upvotes: 1

Related Questions