Configure your App

You must instantiate a client to make requests to the API. All requests to the Groups Settings API require authentication.

The following code demonstrates how to configure your client and authenticate using OAuth 2.0 for native applications.

Java


adminSDK/groups-settings/quickstart/src/main/java/GroupsSettingsQuickstart.java
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.groupssettings.Groupssettings;
import com.google.api.services.groupssettings.model.Groups;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;

public class GroupsSettingsQuickstart {
    private static final String APPLICATION_NAME = "Google Admin SDK Group Settings API Java Quickstart";
    private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
    private static final String TOKENS_DIRECTORY_PATH = "tokens";

    /**
     * Global instance of the scopes required by this quickstart.
     * If modifying these scopes, delete your previously saved tokens/ folder.
     */
    private static final List<String> SCOPES = Collections.singletonList(
            "https://www.googleapis.com/auth/apps.groups.settings");
    private static final String CREDENTIALS_FILE_PATH = "/credentials.json";

    /**
     * Creates an authorized Credential object.
     * @param HTTP_TRANSPORT The network HTTP Transport.
     * @return An authorized Credential object.
     * @throws IOException If the credentials.json file cannot be found.
     */
    private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.
        InputStream in = GroupsSettingsQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }

    public static void main(String... args) throws IOException, GeneralSecurityException {
        // Build a new authorized API client service.
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        Groupssettings service = new Groupssettings.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();

        // API is ready to use

        if (args.length == 0) {
            System.out.println("No group email specified.");
            return;
        }
        try {
            String groupEmail = args[0];
            Groups settings = service.groups().get(groupEmail).execute();
            System.out.println(String.format("Description: %s", settings.getDescription()));
        } catch (Exception e) {
            // TODO(developer) - Handle errors
            e.printStackTrace();
        }
    }
}

.NET

adminSDK/groups-settings/AdminSDKGroupsSettingsQuickstart/AdminSDKGroupsSettingsQuickstart.cs
using Google.Apis.Auth.OAuth2;
using Google.Apis.Groupssettings.v1;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.IO;
using System.Threading;
using Groups = Google.Apis.Groupssettings.v1.Data.Groups;

namespace AdminSDKGroupsSettingsQuickstart
{
	class Program
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/groupssettings_v1-dotnet-quickstart.json
        static string[] Scopes = { "https://www.googleapis.com/auth/apps.groups.settings"};
        static string ApplicationName = "Groups Settings API .NET Quickstart";

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

            using (var stream =
                new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
            {
                // The file token.json stores the user's access and refresh tokens, and is created
                // automatically when the authorization flow completes for the first time.
                string credPath = "token.json";
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.FromStream(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: {0}", credPath);
            }

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

            // Service ready to use

            if (args.Length == 0)
            {
                Console.WriteLine("No group email specified.");
                return;
            }

            String groupEmail = args[0];
            try
            {
                Groups settings = service.Groups.Get(groupEmail).Execute();
                Console.Write("Description: {0}", settings.Description);
            }
            catch (Exception err)
            {
                // TODO(developer) - handle exception
                Console.Error.WriteLine(err);
            }

        }
    }
}