【问题标题】:Get Access Token - Design Automation for Revit获取访问令牌 - Revit 的设计自动化
【发布时间】:2021-06-18 19:11:17
【问题描述】:

我已经创建了应用程序,能够调试源代码。

还收到了客户端 ID 和客户端密码。

我需要您的帮助来了解如何获取访问令牌。

基本上这部分 - enter image description here

如果您能提供一个关于如何发送 HTTP 请求的示例代码,那将非常有帮助,因为我是 Web API 的新手。

我已将此代码添加到我的解决方案中 -

enter image description here enter image description here

谢谢, 字符串

【问题讨论】:

    标签: autodesk-forge


    【解决方案1】:

    这是一个工作示例,如何获取访问令牌。 您需要将 NewtonSoft.Json nuget 包添加到您的项目中才能运行它。

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

    以及简单的用法:

    var token=await GetToken();
    

    这并不完美,我试图简化所有的时刻。

    【讨论】:

      猜你喜欢
      • 2021-06-29
      • 1970-01-01
      • 2019-03-13
      • 2021-12-12
      • 1970-01-01
      • 2017-11-11
      • 2021-05-13
      • 2020-04-22
      • 2020-07-08
      相关资源
      最近更新 更多