Exemplo n.º 1
0
        private static CommandExecutionContext ConstructExecutionContext(String baseUri, String clientId, String clientSecret, String oauthToken, String oauthRefreshToken, Boolean interactiveMode)
        {
            if (oauthToken == null)
            {
                if (String.IsNullOrWhiteSpace(baseUri))
                {
                    throw new Exception("'baseUri' is not specified: Please, use -b command line option or set ONSHAPE_BASE_URI environment variable");
                }
                if (String.IsNullOrWhiteSpace(clientId))
                {
                    throw new Exception("'clientId' is not specified: Please, set ONSHAPE_CLIENT_ID environment variable");
                }
                if (String.IsNullOrWhiteSpace(clientSecret))
                {
                    throw new Exception("'clientSecret' is not specified: Please, set ONSHAPE_CLIENT_SECRET environment variable");
                }

                // Read refresh token from registry
                oauthRefreshToken = Utils.GetRegistry(Constants.REFRESH_TOKEN_KEY_NAME);

                if (String.IsNullOrEmpty(oauthRefreshToken))
                {
                    // Authenticate as Onshape Application
                    OnshapeOAuth onshapeOAuth = new OnshapeOAuth("https://oauth.onshape.com", clientId, clientSecret);
                    Console.WriteLine("Opening browser window for Onshape authentication...");
                    onshapeOAuth.AuthenticateBlocking();
                    oauthToken        = onshapeOAuth.AccessToken;
                    oauthRefreshToken = onshapeOAuth.RefreshToken;
                    Utils.SetRegistry(Constants.REFRESH_TOKEN_KEY_NAME, oauthRefreshToken);
                }
            }

            // Initialize Onshape client
            OnshapeClient client = new OnshapeClient {
                AccessToken = oauthToken, RefreshToken = oauthRefreshToken, BaseUri = baseUri, ClientId = clientId, ClientSecret = clientSecret
            };

            // Refresh token if needed
            if (oauthToken == null)
            {
                Task <string> t = client.GetRefreshedOAuthToken();
                try {
                    t.Wait(Constants.TOKEN_REFRESH_TIME_OUT);
                    client.AccessToken = t.Result;
                }
                catch (Exception)
                {
                    Console.WriteLine("Error: token refresh failed");
                }
            }

            // Construct command execution context
            return(new CommandExecutionContext {
                BaseURL = baseUri, InteractiveMode = interactiveMode, OAuthToken = oauthToken, OAuthRefreshToken = oauthRefreshToken, Client = client
            });
        }
        private OnshapeClient GetClient()
        {
            String baseUri      = Environment.GetEnvironmentVariable(Constants.ONSHAPE_BASE_URI);
            String clientId     = Environment.GetEnvironmentVariable(Constants.ONSHAPE_CLIENT_ID);
            String clientSecret = Environment.GetEnvironmentVariable(Constants.ONSHAPE_CLIENT_SECRET);
            String oauthToken   = Environment.GetEnvironmentVariable(Constants.ONSHAPE_OAUTH_TOKEN);

            // Read refresh token from registry
            String oauthRefreshToken = Utils.GetRegistry(Constants.REFRESH_TOKEN_KEY_NAME);

            // Authenticate as Onshape Application
            OnshapeOAuth onshapeOAuth = new OnshapeOAuth(baseUri, clientId, clientSecret);

            Console.WriteLine("Opening browser window for Onshape authentication...");
            onshapeOAuth.AuthenticateBlocking();
            oauthToken        = onshapeOAuth.AccessToken;
            oauthRefreshToken = onshapeOAuth.RefreshToken;
            Utils.SetRegistry(Constants.REFRESH_TOKEN_KEY_NAME, oauthRefreshToken);

            // Initialize Onshape client
            OnshapeClient client = new OnshapeClient {
                AccessToken = oauthToken, RefreshToken = oauthRefreshToken, BaseUri = baseUri, ClientId = clientId, ClientSecret = clientSecret
            };

            /* Refresh token if needed
             * if (oauthToken == null)
             * {
             *  Task<string> t = client.GetRefreshedOAuthToken();
             *  try
             *  {
             *      t.Wait(Constants.TOKEN_REFRESH_TIME_OUT);
             *      client.AccessToken = t.Result;
             *  }
             *  catch (Exception)
             *  {
             *      Console.WriteLine("Error: token refresh failed");
             *  }
             * }*/

            return(client);
        }
Exemplo n.º 3
0
        private static CommandExecutionContext ConstructExecutionContext(String baseUri, String clientId, String clientSecret, String oauthToken, String oauthRefreshToken, Boolean interactiveMode)
        {
            if (oauthToken == null)
            {
                if (String.IsNullOrWhiteSpace(baseUri))
                {
                    throw new Exception("'baseUri' is not specified: Please, use -b command line option or set ONSHAPE_BASE_URI environment variable");
                }
                if (String.IsNullOrWhiteSpace(clientId))
                {
                    throw new Exception("'clientId' is not specified: Please, set ONSHAPE_CLIENT_ID environment variable");
                }
                if (String.IsNullOrWhiteSpace(clientSecret))
                {
                    throw new Exception("'clientSecret' is not specified: Please, set ONSHAPE_CLIENT_SECRET environment variable");
                }

                // Read refresh token from registry
                oauthRefreshToken = Utils.GetRegistry(Constants.REFRESH_TOKEN_KEY_NAME);

                if (String.IsNullOrEmpty(oauthRefreshToken))
                {
                    // Authenticate as Onshape Application
                    OnshapeOAuth onshapeOAuth = new OnshapeOAuth(baseUri, clientId, clientSecret);
                    Console.WriteLine("Opening browser window for Onshape authentication...");
                    onshapeOAuth.AuthenticateBlocking();
                    oauthToken = onshapeOAuth.AccessToken;
                    oauthRefreshToken = onshapeOAuth.RefreshToken;
                    Utils.SetRegistry(Constants.REFRESH_TOKEN_KEY_NAME, oauthRefreshToken);
                }
            }

            // Initialize Onshape client
            OnshapeClient client = new OnshapeClient { AccessToken = oauthToken, RefreshToken = oauthRefreshToken, BaseUri = baseUri, ClientId = clientId, ClientSecret = clientSecret };

            // Refresh token if needed
            if (oauthToken == null)
            {
                Task<string> t = client.GetRefreshedOAuthToken();
                try {
                    t.Wait(Constants.TOKEN_REFRESH_TIME_OUT);
                    client.AccessToken = t.Result;
                }
                catch (Exception) 
                {
                    Console.WriteLine("Error: token refresh failed");
                }
            }

            // Construct command execution context
            return new CommandExecutionContext { BaseURL = baseUri, InteractiveMode = interactiveMode, OAuthToken = oauthToken, OAuthRefreshToken = oauthRefreshToken, Client = client };
        }