.NET Quickstart

Complete the steps described in the rest of this page, and in about five minutes you'll have a simple .NET console application that makes requests to the Groups Settings API.

Prerequisites

To run this quickstart, you'll need:

  • Visual Studio 2013 or later.
  • Access to the internet and a web browser.
  • A G Suite domain with API access enabled.
  • A Google account in that domain with administrator privileges.
  • A group within the domain.

Step 1: Turn on the Groups Settings API

  1. Use this wizard to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials.
  2. On the Add credentials to your project page, click the Cancel button.
  3. At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button.
  4. Select the Credentials tab, click the Create credentials button and select OAuth client ID.
  5. Select the application type Other, enter the name "Groups Settings API Quickstart", and click the Create button.
  6. Click OK to dismiss the resulting dialog.
  7. Click the file_download (Download JSON) button to the right of the client ID.
  8. Move this file to your working directory and rename it client_secret.json.

Step 2: Prepare the project

  1. Create a new Visual C# Console Application project in Visual Studio.
  2. Open the NuGet Package Manager Console, select the package source nuget.org, and run the following command:
Install-Package Google.Apis.Groupssettings.v1

Step 3: Set up the sample

  1. Drag client_secret.json (downloaded in Step 1) into your Visual Studio Solution Explorer.
  2. Select client_secret.json, and then go to the Properties window and set the Copy to Output Directory field to Copy always.
  3. Replace the contents of Program.cs with the following code:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Groupssettings.v1;
using Google.Apis.Groupssettings.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GroupsSettingsQuickstart
{
    class Program
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/groupssettings-dotnet-quickstart.json
        static string[] Scopes = { GroupssettingsService.Scope.AppsGroupsSettings };
        static string ApplicationName = "Groups Settings API .NET Quickstart";

        static void Main(string[] args)
        {
            UserCredential credential;

            using (var stream =
                new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/groupssettings-dotnet-quickstart.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            Console.WriteLine("Enter the email address of a Google Group in your "
                + "domain:");
            String email = Console.ReadLine();

            // Create Groups Settings API service.
            var service = new GroupssettingsService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = ApplicationName,
                });

            // Define parameters of request.
            GroupsResource.GetRequest request = service.Groups.Get(email);
            request.Alt = GroupssettingsBaseServiceRequest<Groups>.AltEnum.Json;

            // Get group settings.
            Groups groups = request.Execute();
            Console.WriteLine("{0} - {1}", groups.Email, groups.Description);

            Console.Read();

        }
    }
}

Step 4: Run the sample

Build and run the sample by clicking Start in the Visual Studio toolbar.

The first time you run the sample, it will prompt you to authorize access:

  1. The sample will attempt to open a new window or tab in your default browser. If this fails, copy the URL from the console and manually open it in your browser.

    If you are not already logged into your Google account, you will be prompted to log in. If you are logged into multiple Google accounts, you will be asked to select one account to use for the authorization.

  2. Click the Accept button.
  3. The sample will proceed automatically, and you may close the window/tab.

Notes

  • Authorization information is stored on the file system, so subsequent executions will not prompt for authorization.
  • The authorization flow in this example is designed for a command-line application. For information on how to perform authorization in a web application, see the web applications section of the library's OAuth 2.0 guide.

Further reading

Troubleshooting

Could not load file or assembly 'Microsoft.Threading.Tasks'

This error can occur when using an outdated or incorrectly installed version of the Microsoft.Bcl.Async package. Reinstall the package using the following command in the NuGet Package Manager Console:

Update-Package Microsoft.Bcl.Async -reinstall

You will be prompted to restart Visual Studio to complete the installation.

Send feedback about...

Groups Settings API
Groups Settings API