[英]Get Access Token - Design Automation for Revit
I have created the app, was able to debug the source code.我已经创建了应用程序,能够调试源代码。
Also received Client ID and Client secret.还收到了客户端 ID 和客户端密码。
I need your help to understand how to get access token.我需要您的帮助来了解如何获取访问令牌。
Basically this part - enter image description here基本上这部分 -在此处输入图像描述
It will be really helpful if you can provide a sample code on how to send the HTTP request as I am novice to web API's.如果您可以提供有关如何发送 HTTP 请求的示例代码,这将非常有帮助,因为我是 web API 的新手。
I have added this code to my solution -我已将此代码添加到我的解决方案中 -
enter image description here enter image description here在此处输入图像描述 在此处输入图像描述
Thanks, STR谢谢, STR
Here is a working example, how to obtain an Access token.这是一个工作示例,如何获取访问令牌。 You need to add NewtonSoft.Json nuget package to your project to run it.
您需要将 NewtonSoft.Json nuget package 添加到您的项目中以运行它。
public class TokenModel
{
[JsonProperty("access_token")]
public string AccessToken;
}
public async Task<string> GetToken()
{
var credentials = new Dictionary<string, string>();
credentials.Add("client_id", "YOUR_CLIENT_ID");
credentials.Add("client_secret", "YOUR_CLIENT_SECRET");
credentials.Add("grant_type", "YOUR_GRANT_TYPE");
credentials.Add("scope", "YOUR_SCOPE");
var content = new FormUrlEncodedContent(credentials);
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://developer.api.autodesk.com");
var response = await client.PostAsync("/authentication/v1/authenticate", content);
if (response.IsSuccessStatusCode)
{
var responseData = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<TokenModel>(responseData).AccessToken;
}
}
}
And simple usage:和简单的用法:
var token=await GetToken();
It is not perfect, I've tried to simplify all the moments.它并不完美,我试图简化所有的时刻。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.