获取Azure区块链项目的不记名令牌

时间:2019-11-27 10:46:12

标签: c# azure smartcontracts azure-blockchain-workbench

我正在尝试创建一个应用程序,该应用程序自动将数据发送到Azure区块链工作台上的Smartcontract。

问题是,我不了解如何获取承载令牌。在线上有一个示例,我可以在其中通过GET和POST请求调用API。但是我必须提交一个客户端应用程序ID,一个客户端密钥和一个资源ID。我从哪里得到的?

非常感谢您的帮助和想法!

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

}

1 个答案:

答案 0 :(得分:1)

根据我的测试,当我们成功创建Azure区块链工作台时,我们需要在首次访问Azure区块链工作台时配置Azure AD,并且我们将同时创建Azure AD应用程序。资源是Azure AD应用程序的应用程序ID或应用程序URL。有关更多详细信息,请参阅document

例如

  1. Access Azure Blockchain workbench

  2. 配置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. 获取令牌
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. 调用rest api
URL: {WORKBENCH_API_URL}/api/v1/users
Headers: Authorization Bearer <access_token>

enter image description here