Skip to main content
タグを編集
リンク
本文から 195 文字削除
ソース リンク
  static async Task Main( string[] args )
    {
        GoogleCredential credential;
        try
        {
            using ( FileStream stream = new FileStream( credentialPath, FileMode.Open, FileAccess.Read ) )
            {
                credential = GoogleCredential.FromStream( stream )
                    .CreateScoped( "https://www.googleapis.com/auth/admin.directory.user",
                                   "https://www.googleapis.com/auth/admin.directory.rolemanagement.readonly",
                                   "https://www.googleapis.com/auth/admin.directory.rolemanagement",
                                   "https://www.googleapis.com/auth/admin.directory.groupreadonly" );
                    
            }

            

            // 認証トークンの取得
            string accessToken = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();

            // RestSharp クライアントの初期化              
            restClient = new RestClient();

            // ユーザー情報の取得(こちらは問題なく取得できます。)
            JObject user = await GetUser( accessToken, "[email protected]" );

            // 企業のロールの取得(エラーになり取得失敗します。)
            string roleId = "67382669507297285";
            JObject assignments = await GetRole( accessToken, customerId, roleId );
            
        }
        catch(Exception ex)
        {
            string errorMessage = $"{ex.Message}/{ex.InnerException?.Message}";
            Console.WriteLine( errorMessage );
        }            
    }

    /// <summary>
    /// GoogleのUserを取得します。
    /// </summary>
    /// <param name="accessToken"></param>
    /// <param name="restClient"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    private static async Task<JObject> GetUser(string accessToken, string userId)
    {
        try
        {                                
            string directoryApiUrl = "https://admin.googleapis.com/admin/directory/v1/users";
            string url = $"{directoryApiUrl}/{userId}";

            RestRequest request = new RestRequest( url, Method.Get );
            request.AddHeader( "Authorization", $"Bearer {accessToken}" );
            
            RestResponse response = await restClient.ExecuteAsync( request );
            if(!response.IsSuccessful)
            {
                throw new Exception( $"{response.StatusCode},{response.Content}" );
            }

            JObject result = JObject.Parse( response.Content );
            return result;
        }
        catch(Exception ex ) 
        {
            throw new Exception( "Userの取得でエラーが発生しました。", ex );
        }
    }

    /// <summary>
    /// Googleのロールを取得します。
    /// </summary>
    /// <param name="accessToken"></param>
    /// <param name="restClient"></param>
    /// <param name="customer"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    private static async Task<JObject> GetRole(string accessToken, string customer, string roleId)
    {
        try
        {   
            string url = $"https://admin.googleapis.com/admin/directory/v1/customer/{customer}/roles/{roleId}";

            RestRequest request = new RestRequest( url, Method.Get );
            request.AddHeader( "Authorization", $"Bearer {accessToken}" );
            
            RestResponse response = await restClient.ExecuteAsync( request );
            if ( !response.IsSuccessful )
            {
                JObject error = JObject.Parse( response.Content );
                throw new Exception( $"{response.StatusCode},{response.Content}" );
            }

            JObject result = JObject.Parse( response.Content );
            return result;
        }
        catch ( Exception ex )
        {
            throw new Exception( "Roleの取得でエラーが発生しました。", ex );
        }
    }
  static async Task Main( string[] args )
    {
        GoogleCredential credential;
        try
        {
            using ( FileStream stream = new FileStream( credentialPath, FileMode.Open, FileAccess.Read ) )
            {
                credential = GoogleCredential.FromStream( stream )
                    .CreateScoped( "https://www.googleapis.com/auth/admin.directory.user",
                                   "https://www.googleapis.com/auth/admin.directory.rolemanagement.readonly",
                                   "https://www.googleapis.com/auth/admin.directory.rolemanagement",
                                   "https://www.googleapis.com/auth/admin.directory.group" );
                    
            }

            

            // 認証トークンの取得
            string accessToken = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();

            // RestSharp クライアントの初期化              
            restClient = new RestClient();

            // ユーザー情報の取得(こちらは問題なく取得できます。)
            JObject user = await GetUser( accessToken, "[email protected]" );

            // 企業のロールの取得(エラーになり取得失敗します。)
            string roleId = "67382669507297285";
            JObject assignments = await GetRole( accessToken, customerId, roleId );
            
        }
        catch(Exception ex)
        {
            string errorMessage = $"{ex.Message}/{ex.InnerException?.Message}";
            Console.WriteLine( errorMessage );
        }            
    }

    /// <summary>
    /// GoogleのUserを取得します。
    /// </summary>
    /// <param name="accessToken"></param>
    /// <param name="restClient"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    private static async Task<JObject> GetUser(string accessToken, string userId)
    {
        try
        {                                
            string directoryApiUrl = "https://admin.googleapis.com/admin/directory/v1/users";
            string url = $"{directoryApiUrl}/{userId}";

            RestRequest request = new RestRequest( url, Method.Get );
            request.AddHeader( "Authorization", $"Bearer {accessToken}" );
            
            RestResponse response = await restClient.ExecuteAsync( request );
            if(!response.IsSuccessful)
            {
                throw new Exception( $"{response.StatusCode},{response.Content}" );
            }

            JObject result = JObject.Parse( response.Content );
            return result;
        }
        catch(Exception ex ) 
        {
            throw new Exception( "Userの取得でエラーが発生しました。", ex );
        }
    }

    /// <summary>
    /// Googleのロールを取得します。
    /// </summary>
    /// <param name="accessToken"></param>
    /// <param name="restClient"></param>
    /// <param name="customer"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    private static async Task<JObject> GetRole(string accessToken, string customer, string roleId)
    {
        try
        {   
            string url = $"https://admin.googleapis.com/admin/directory/v1/customer/{customer}/roles/{roleId}";

            RestRequest request = new RestRequest( url, Method.Get );
            request.AddHeader( "Authorization", $"Bearer {accessToken}" );
            
            RestResponse response = await restClient.ExecuteAsync( request );
            if ( !response.IsSuccessful )
            {
                JObject error = JObject.Parse( response.Content );
                throw new Exception( $"{response.StatusCode},{response.Content}" );
            }

            JObject result = JObject.Parse( response.Content );
            return result;
        }
        catch ( Exception ex )
        {
            throw new Exception( "Roleの取得でエラーが発生しました。", ex );
        }
    }
  static async Task Main( string[] args )
    {
        GoogleCredential credential;
        try
        {
            using ( FileStream stream = new FileStream( credentialPath, FileMode.Open, FileAccess.Read ) )
            {
                credential = GoogleCredential.FromStream( stream )
                    .CreateScoped( "https://www.googleapis.com/auth/admin.directory.user.readonly",
                                   "https://www.googleapis.com/auth/admin.directory.rolemanagement.readonly");
                    
            }

            

            // 認証トークンの取得
            string accessToken = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();

            // RestSharp クライアントの初期化              
            restClient = new RestClient();

            // ユーザー情報の取得(こちらは問題なく取得できます。)
            JObject user = await GetUser( accessToken, "[email protected]" );

            // 企業のロールの取得(エラーになり取得失敗します。)
            string roleId = "67382669507297285";
            JObject assignments = await GetRole( accessToken, customerId, roleId );
            
        }
        catch(Exception ex)
        {
            string errorMessage = $"{ex.Message}/{ex.InnerException?.Message}";
            Console.WriteLine( errorMessage );
        }            
    }

    /// <summary>
    /// GoogleのUserを取得します。
    /// </summary>
    /// <param name="accessToken"></param>
    /// <param name="restClient"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    private static async Task<JObject> GetUser(string accessToken, string userId)
    {
        try
        {                                
            string directoryApiUrl = "https://admin.googleapis.com/admin/directory/v1/users";
            string url = $"{directoryApiUrl}/{userId}";

            RestRequest request = new RestRequest( url, Method.Get );
            request.AddHeader( "Authorization", $"Bearer {accessToken}" );
            
            RestResponse response = await restClient.ExecuteAsync( request );
            if(!response.IsSuccessful)
            {
                throw new Exception( $"{response.StatusCode},{response.Content}" );
            }

            JObject result = JObject.Parse( response.Content );
            return result;
        }
        catch(Exception ex ) 
        {
            throw new Exception( "Userの取得でエラーが発生しました。", ex );
        }
    }

    /// <summary>
    /// Googleのロールを取得します。
    /// </summary>
    /// <param name="accessToken"></param>
    /// <param name="restClient"></param>
    /// <param name="customer"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    private static async Task<JObject> GetRole(string accessToken, string customer, string roleId)
    {
        try
        {   
            string url = $"https://admin.googleapis.com/admin/directory/v1/customer/{customer}/roles/{roleId}";

            RestRequest request = new RestRequest( url, Method.Get );
            request.AddHeader( "Authorization", $"Bearer {accessToken}" );
            
            RestResponse response = await restClient.ExecuteAsync( request );
            if ( !response.IsSuccessful )
            {
                JObject error = JObject.Parse( response.Content );
                throw new Exception( $"{response.StatusCode},{response.Content}" );
            }

            JObject result = JObject.Parse( response.Content );
            return result;
        }
        catch ( Exception ex )
        {
            throw new Exception( "Roleの取得でエラーが発生しました。", ex );
        }
    }
本文から 1 文字削除
ソース リンク
ソース リンク
読み込んでいます