C# 获取Azure区块链项目的承载令牌
我正在尝试创建一个应用程序,自动将数据发送到Azure区块链工作台上的Smartcontract 问题是,我不明白如何获得不记名代币。在网上有一个例子,我可以了解如何使用GET和POST请求调用API。但我必须提交一个客户端应用程序ID、客户端密码和资源ID。我从哪里获得它们 非常感谢你的帮助和想法C# 获取Azure区块链项目的承载令牌,c#,azure,smartcontracts,azure-blockchain-workbench,C#,Azure,Smartcontracts,Azure Blockchain Workbench,我正在尝试创建一个应用程序,自动将数据发送到Azure区块链工作台上的Smartcontract 问题是,我不明白如何获得不记名代币。在网上有一个例子,我可以了解如何使用GET和POST请求调用API。但我必须提交一个客户端应用程序ID、客户端密码和资源ID。我从哪里获得它们 非常感谢你的帮助和想法 class Program { public static readonly string AUTHORITY = "https://login.microsoftonline.com/XX
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);
}
}
}
}根据我的测试,当我们成功创建Azure区块链工作台时,我们需要在第一次访问Azure区块链工作台时配置Azure AD,同时我们将创建Azure AD应用程序。资源是Azure AD应用程序的应用程序ID或应用程序url。有关更多详细信息,请参阅 比如说
cd;调用WebRequest-Urihttps://aka.ms/createWorkbenchServicePrincipalScript -OutFile createWorkbenchServicePrincipal.ps1
./createWorkbenchServicePrincipal.ps1-租户名称-WorkbenchHappid-MakeAdmin(可选)
方法:POST
网址:https://login.microsoftonline.com//oauth2/token
标题:内容类型:application/x-www-form-urlencoded
正文:
授予\类型:客户端\凭据
客户识别码:
客户机密:
资源:
URL:{WORKBENCH\u API\u URL}/API/v1/users
Headers:Authorization Bearer但还有一件事,我无法列出与该持有者签订合同的操作。当我使用我的个人承载(从Workbench站点上的网络流量复制)时,我可以列出所有操作。您知道如何以用户身份获取承载令牌吗?
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)
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>
URL: {WORKBENCH_API_URL}/api/v1/users
Headers: Authorization Bearer <access_token>