6868 */
6969// [START analyticsdata_quickstart_oauth2]
7070public class QuickstartOAuth2Sample {
71- // Scopes for the generated OAuth2 credentials. The list here only contains the Google Ads API
72- // scope, but you can add multiple scopes if you want to use the credentials for other Google
73- // APIs.
71+ // Scopes for the generated OAuth2 credentials. The list here consists of just the read-only
72+ // Google Analytics Data API scope, but you can add multiple scopes if you want to use the
73+ // credentials for other Google APIs.
7474 private static final ImmutableList <String > SCOPES =
7575 ImmutableList .<String >builder ()
7676 .add ("https://www.googleapis.com/auth/analytics.readonly" )
@@ -90,6 +90,11 @@ public static void main(String... args) throws Exception {
9090 // a simple report
9191 // on the provided GA4 property id.
9292 static void sampleGetCredentialsAndRunReport (String propertyId ) throws Exception {
93+ // TODO(developer): Download the OAuth 2.0 client ID JSON from
94+ // https://console.cloud.google.com/apis/credentials for a Desktop or Web client, and save it in
95+ // the java-docs-samples/google-analytics-data directory in a file named oauth2.keys.json.
96+ // If using a Web client, register http://127.0.0.1 as an authorized redirect URI.
97+
9398 // Extracts the OAuth client information from the provided file.
9499 ClientId parsedClient = ClientId .fromStream (new FileInputStream ("./oauth2.keys.json" ));
95100 // Creates an anti-forgery state token as described here:
@@ -132,17 +137,19 @@ static void sampleGetCredentialsAndRunReport(String propertyId) throws Exception
132137 throw new IllegalStateException ("State does not match expected state" );
133138 }
134139
135- // Exchanges the authorization code for credentials and print the refresh token .
140+ // Exchanges the authorization code for credentials.
136141 UserCredentials userCredentials =
137142 userAuthorizer .getCredentialsFromCode (authorizationResponse .code , baseUri );
138143 System .out .printf ("Successfully obtained user credentials for scope(s): %s%n" , SCOPES );
139144
140- // Constructs a BetaAnalyticsDataClient using the UserCredentials obtained.
145+ // Constructs a BetaAnalyticsDataClient using a settings object that will use the
146+ // credentials obtained above instead of the Application Default Credentials.
147+ BetaAnalyticsDataSettings betaAnalyticsDataSettings =
148+ BetaAnalyticsDataSettings .newBuilder ()
149+ .setCredentialsProvider (FixedCredentialsProvider .create (userCredentials ))
150+ .build ();
141151 try (BetaAnalyticsDataClient analyticsData =
142- BetaAnalyticsDataClient .create (
143- BetaAnalyticsDataSettings .newBuilder ()
144- .setCredentialsProvider (FixedCredentialsProvider .create (userCredentials ))
145- .build ())) {
152+ BetaAnalyticsDataClient .create (betaAnalyticsDataSettings )) {
146153 RunReportRequest request =
147154 RunReportRequest .newBuilder ()
148155 .setProperty ("properties/" + propertyId )
0 commit comments