Community
BIM 360 API Forum
Welcome to Autodesk’s BIM 360 API Forums. Share your knowledge, ask questions, and explore popular BIM 360 API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

3-legged OAuth token (Revit Addin - APS)

1 REPLY 1
Reply
Message 1 of 2
jw.vanasselt
240 Views, 1 Reply

3-legged OAuth token (Revit Addin - APS)

Hi all,


I try to build a Revit Add-in and want get the 3-Legged OAuth token.

I can't get the "code" because the script is run inside revit.

 

Is there a way to get the code without login?

 

 

public static Token ThreeLegged_Get(string clientId, string clientSecret, string callbackUri)
{
    
    using (RestClient client = new RestClient(new RestClientOptions(Paths.AuthenticationV2Url) { MaxTimeout = 2000 }))
    {
        RestRequest request = new RestRequest("token", Method.Post);

        string key = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{clientId}:{clientSecret}"));

        // Headers
        request.AddHeader("Content-Type", ContentType.FormUrlEncoded);
        request.AddHeader("Accept", ContentType.Json);
        request.AddHeader("Authorization", "Basic " + key);

        // Parameters
        request.AddParameter("grant_type", "authorization_code");
        request.AddParameter("scope", "data:read data:write");
        request.AddParameter("response_type", "code");                
        request.AddParameter("redirect_uri", callbackUri);
        request.AddParameter("code", "");



        RestResponse response = client.ExecuteAsync(request).Result;


        Debug.WriteLine(JToken.Parse(response.Content).ToString(Newtonsoft.Json.Formatting.Indented));

        return JsonSerializer.Deserialize<Token>(response.Content);

    }
}

 

1 REPLY 1
Message 2 of 2
rbigare
in reply to: jw.vanasselt

Hi JW, 

 

I'm not sure about the context of your code - so take my response with a pinch of salt. But in my Revit plugins I personally show a separate Authentication WPF window as a dialog at the very start of the plugin, with an embedded webviewer redirecting to the Autodesk authentication adress. See below. When the dialog is closed, you can refer to the code in the rest of your plugin.

 

 

       public Auth()
        {
            InitializeComponent();
            this.Loaded += Auth_Loaded;
        }

        public string Code { get; internal set; }

        private async void Auth_Loaded(object sender, RoutedEventArgs e)
        {
            string client_id = "YOUR CLIENT ID";
            string redirect_uri = "http://localhost:8080/authenticate";
            string auth_url = $"https://developer.api.autodesk.com/authentication/" +
                $"v2/authorize?response_type=code" +
                $"&client_id={client_id}" +
                $"&redirect_uri={redirect_uri}" +
                $"&scope=data:create%20data:read%20data:write";


            Uri uri = new Uri(auth_url);
            await web.EnsureCoreWebView2Async();
            web.CoreWebView2.Navigate(auth_url);

            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://localhost:8080/");
            listener.Start();

            Code = await HandleIncomingRequestsAsync(listener);

            listener.Stop();
            this.DialogResult = true;
            this.Close();
        }

        private async Task<string> HandleIncomingRequestsAsync(HttpListener listener)
        {
            string code = null;
            while (true)
            {
                HttpListenerContext context = await listener.GetContextAsync();
                HttpListenerRequest request = context.Request;
                HttpListenerResponse response = context.Response;

                if (request.Url.AbsolutePath == "/authenticate" && request.HttpMethod == "GET")
                {
                    // Handle the authentication callback here
                    // You can retrieve the authorization code from request.QueryString["code"]
                    // For example:
                    code = request.QueryString["code"];
                    // Call your method to handle the authorization code

                    // Respond to the client
                    string responseString = "Authentication successful! You can close this window.";
                    byte[] buffer = Encoding.UTF8.GetBytes(responseString);
                    response.ContentLength64 = buffer.Length;
                    response.OutputStream.Write(buffer, 0, buffer.Length);

                    response.OutputStream.Close();
                    return code;

                }
            }
        }

    }

 

 

 

Hope this helps

Romain 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report