|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.analytics; |
| 18 | + |
| 19 | +/* This application demonstrates the usage of the Analytics Admin API using |
| 20 | +service account credentials. For more information on service accounts, see |
| 21 | +https://cloud.google.com/iam/docs/understanding-service-accounts |
| 22 | +
|
| 23 | +The following document provides instructions on setting service account |
| 24 | +credentials for your application: |
| 25 | +https://cloud.google.com/docs/authentication/production |
| 26 | +
|
| 27 | +In a nutshell, you need to: |
| 28 | +
|
| 29 | +1. Create a service account and download the key JSON file. |
| 30 | +https://cloud.google.com/docs/authentication/production#creating_a_service_account |
| 31 | +
|
| 32 | +2. Provide service account credentials using one of the following options: |
| 33 | +- set the GOOGLE_APPLICATION_CREDENTIALS environment variable, the API |
| 34 | +client will use the value of this variable to find the service account key |
| 35 | +JSON file. |
| 36 | +
|
| 37 | +https://cloud.google.com/docs/authentication/production#setting_the_environment_variable |
| 38 | +
|
| 39 | +OR |
| 40 | +
|
| 41 | +- manually pass the path to the service account key JSON file to the API client |
| 42 | +by specifying the keyFilename parameter in the constructor. |
| 43 | +https://cloud.google.com/docs/authentication/production#passing_the_path_to_the_service_account_key_in_code |
| 44 | +
|
| 45 | +To run this sample using Maven: |
| 46 | + cd java-analytics-admin/samples/snippets |
| 47 | + mvn install |
| 48 | + mvn exec:java -Dexec.mainClass="com.example.analytics.QuickstartSample" |
| 49 | +*/ |
| 50 | + |
| 51 | +// [START analytics_admin_quickstart] |
| 52 | + |
| 53 | +import com.google.analytics.admin.v1beta.Account; |
| 54 | +import com.google.analytics.admin.v1beta.AnalyticsAdminServiceClient; |
| 55 | +import com.google.analytics.admin.v1beta.AnalyticsAdminServiceClient.ListAccountsPage; |
| 56 | +import com.google.analytics.admin.v1beta.AnalyticsAdminServiceClient.ListAccountsPagedResponse; |
| 57 | +import com.google.analytics.admin.v1beta.ListAccountsRequest; |
| 58 | + |
| 59 | +public class QuickstartSample { |
| 60 | + |
| 61 | + public static void main(String... args) throws Exception { |
| 62 | + listAccounts(); |
| 63 | + } |
| 64 | + |
| 65 | + // This is an example snippet that calls the Google Analytics Admin API and lists all Google |
| 66 | + // Analytics accounts available to the authenticated user. |
| 67 | + static void listAccounts() throws Exception { |
| 68 | + // Instantiates a client using default credentials. |
| 69 | + // See https://cloud.google.com/docs/authentication/production for more information |
| 70 | + // about managing credentials. |
| 71 | + try (AnalyticsAdminServiceClient analyticsAdmin = AnalyticsAdminServiceClient.create()) { |
| 72 | + // Calls listAccounts() method of the Google Analytics Admin API and prints |
| 73 | + // the response for each account. |
| 74 | + ListAccountsPagedResponse response = |
| 75 | + analyticsAdmin.listAccounts(ListAccountsRequest.newBuilder().build()); |
| 76 | + for (ListAccountsPage page : response.iteratePages()) { |
| 77 | + for (Account account : page.iterateAll()) { |
| 78 | + System.out.printf("Account name: %s%n", account.getName()); |
| 79 | + System.out.printf("Display name: %s%n", account.getDisplayName()); |
| 80 | + System.out.printf("Country code: %s%n", account.getRegionCode()); |
| 81 | + System.out.printf("Create time: %s%n", account.getCreateTime().getSeconds()); |
| 82 | + System.out.printf("Update time: %s%n", account.getUpdateTime().getSeconds()); |
| 83 | + System.out.println(); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// [END analytics_admin_quickstart] |
0 commit comments