Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.

Commit d944192

Browse files
committed
[based-on-1.7 branch] Updating 11 cmdline samples to 1.7
1 parent 46ec62f commit d944192

File tree

52 files changed

+535
-756
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+535
-756
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
3+
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
4+
<listEntry value="/adsense-cmdline-sample/src/main/java/com/google/api/services/samples/adsense/cmdline/AdSenseSample.java"/>
5+
</listAttribute>
6+
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
7+
<listEntry value="1"/>
8+
</listAttribute>
9+
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
10+
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.google.api.services.samples.adsense.cmdline.AdSenseSample"/>
11+
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="adsense-cmdline-sample"/>
12+
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
13+
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Djava.util.logging.config.file=${project_loc:adsense-cmdline-sample}/logging.properties"/>
14+
</launchConfiguration>

adsense-cmdline-sample/logging.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Properties file which configures the operation of the JDK logging facility.
22
# The system will look for this config file to be specified as a system property:
3-
# -Djava.util.logging.config.file=${project_loc}/logging.properties
3+
# -Djava.util.logging.config.file=${project_loc:adsense-cmdline-sample}/logging.properties
44

55
# Set up the console handler (uncomment "level" to show more fine-grained messages)
66
handlers = java.util.logging.ConsoleHandler

adsense-cmdline-sample/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@
126126
<dependency>
127127
<groupId>com.google.apis</groupId>
128128
<artifactId>google-api-services-adsense</artifactId>
129-
<version>v1.1-1.3.3-beta</version>
129+
<version>v1.1-rev3-1.4.0-beta</version>
130130
</dependency>
131131
<dependency>
132132
<groupId>com.google.apis-samples</groupId>
133133
<artifactId>shared-sample-cmdline</artifactId>
134-
<version>1.1.0</version>
134+
<version>1.2.0</version>
135135
</dependency>
136136
</dependencies>
137137
<properties>

adsense-cmdline-sample/src/main/java/com/google/api/services/samples/adsense/cmdline/AdSenseSample.java

+18-18
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@
1414

1515
package com.google.api.services.samples.adsense.cmdline;
1616

17-
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
18-
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
19-
import com.google.api.client.http.HttpResponseException;
17+
import com.google.api.client.auth.oauth2.Credential;
18+
import com.google.api.client.http.HttpTransport;
2019
import com.google.api.client.http.javanet.NetHttpTransport;
21-
import com.google.api.client.http.json.JsonHttpRequest;
22-
import com.google.api.client.http.json.JsonHttpRequestInitializer;
20+
import com.google.api.client.json.JsonFactory;
2321
import com.google.api.client.json.jackson.JacksonFactory;
2422
import com.google.api.services.adsense.Adsense;
25-
import com.google.api.services.adsense.AdsenseRequest;
23+
import com.google.api.services.adsense.AdsenseScopes;
2624
import com.google.api.services.adsense.model.Accounts;
2725
import com.google.api.services.adsense.model.AdClients;
2826
import com.google.api.services.adsense.model.AdUnits;
2927
import com.google.api.services.adsense.model.CustomChannels;
30-
import com.google.api.services.samples.shared.cmdline.ClientCredentials;
3128
import com.google.api.services.samples.shared.cmdline.oauth2.LocalServerReceiver;
3229
import com.google.api.services.samples.shared.cmdline.oauth2.OAuth2Native;
3330

31+
import java.io.IOException;
32+
import java.util.Arrays;
33+
3434
/**
3535
* A sample application that runs multiple requests against the AdSense Management API.
3636
* These include:
@@ -50,8 +50,12 @@
5050
*/
5151
public class AdSenseSample {
5252

53-
private static final String SCOPE = "https://www.googleapis.com/auth/adsense.readonly";
54-
53+
/** Global instance of the HTTP transport. */
54+
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
55+
56+
/** Global instance of the JSON factory. */
57+
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
58+
5559
// Request parameters.
5660
private static final int MAX_LIST_PAGE_SIZE = 50;
5761
private static final int MAX_REPORT_PAGE_SIZE = 50;
@@ -63,13 +67,14 @@ public class AdSenseSample {
6367
*/
6468
private static Adsense initializeAdsense() throws Exception {
6569
// Authorization.
66-
GoogleAccessProtectedResource accessProtectedResource =
67-
OAuth2Native.authorize(new LocalServerReceiver(), null, "google-chrome", SCOPE);
70+
Credential credential = OAuth2Native.authorize(
71+
HTTP_TRANSPORT, JSON_FACTORY, new LocalServerReceiver(),
72+
Arrays.asList(AdsenseScopes.ADSENSE_READONLY));
6873

6974
// Set up AdSense Management API client.
7075
Adsense adsense = Adsense.builder(new NetHttpTransport(), new JacksonFactory())
7176
.setApplicationName("Google-AdSenseSample/1.1")
72-
.setHttpRequestInitializer(accessProtectedResource)
77+
.setHttpRequestInitializer(credential)
7378
.build();
7479

7580
return adsense;
@@ -120,13 +125,8 @@ public static void main(String[] args) {
120125
} else {
121126
System.out.println("No ad clients found, unable to run remaining methods.");
122127
}
123-
} catch (GoogleJsonResponseException e) {
124-
// Message already includes parsed response.
125-
System.err.println(e.getMessage());
126-
} catch (HttpResponseException e) {
127-
// Message doesn't include parsed response.
128+
} catch (IOException e) {
128129
System.err.println(e.getMessage());
129-
System.err.println(e.getResponse().parseAsString());
130130
}
131131
} catch (Throwable t) {
132132
t.printStackTrace();

adsense-cmdline-sample/src/main/java/com/google/api/services/samples/adsense/cmdline/GenerateReport.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.google.api.services.adsense.Adsense;
1818
import com.google.api.services.adsense.Adsense.Reports.Generate;
1919
import com.google.api.services.adsense.model.AdsenseReportsGenerateResponse;
20-
import com.google.api.services.adsense.model.AdsenseReportsGenerateResponseHeaders;
2120

2221
import java.text.DateFormat;
2322
import java.text.SimpleDateFormat;
@@ -75,7 +74,7 @@ public static void run(Adsense adsense, String adClientId) throws Exception {
7574

7675
if ((response.getRows() != null) && !response.getRows().isEmpty()) {
7776
// Display headers.
78-
for (AdsenseReportsGenerateResponseHeaders header : response.getHeaders()) {
77+
for (AdsenseReportsGenerateResponse.Headers header : response.getHeaders()) {
7978
System.out.printf("%25s", header.getName());
8079
}
8180
System.out.println();

adsense-cmdline-sample/src/main/java/com/google/api/services/samples/adsense/cmdline/GenerateReportWithPaging.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.google.api.services.adsense.Adsense;
1818
import com.google.api.services.adsense.Adsense.Reports.Generate;
1919
import com.google.api.services.adsense.model.AdsenseReportsGenerateResponse;
20-
import com.google.api.services.adsense.model.AdsenseReportsGenerateResponseHeaders;
2120

2221
import java.text.DateFormat;
2322
import java.text.SimpleDateFormat;
@@ -124,8 +123,8 @@ public static void run(Adsense adsense, String adClientId, int maxReportPageSize
124123
* Displays the headers for the report.
125124
* @param headers The list of headers to be displayed.
126125
*/
127-
private static void displayHeaders(List<AdsenseReportsGenerateResponseHeaders> headers) {
128-
for (AdsenseReportsGenerateResponseHeaders header : headers) {
126+
private static void displayHeaders(List<AdsenseReportsGenerateResponse.Headers> headers) {
127+
for (AdsenseReportsGenerateResponse.Headers header : headers) {
129128
System.out.printf("%25s", header.getName());
130129
}
131130
System.out.println();
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
3+
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
4+
<listEntry value="/books-cmdline-sample/src/main/java/com/google/api/services/samples/books/cmdline/BooksSample.java"/>
5+
</listAttribute>
6+
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
7+
<listEntry value="1"/>
8+
</listAttribute>
9+
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
10+
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.google.api.services.samples.books.cmdline.BooksSample"/>
11+
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="books-cmdline-sample"/>
12+
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
13+
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Djava.util.logging.config.file=${project_loc:books-cmdline-sample}/logging.properties"/>
14+
</launchConfiguration>

books-cmdline-sample/instructions.html

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ <h3>Checkout Instructions</h3>
2626

2727
<pre><code>cd <i>[someDirectory]</i>
2828
hg clone https://code.google.com/p/google-api-java-client.samples/ google-api-java-client-samples
29-
cd google-api-java-client-samples/shared/shared-sample-cmdline
30-
<i>[editor]</i> src/main/java/com/google/api/services/samples/shared/cmdline/ClientCredentials.java
31-
mvn source:jar install
32-
cd ../../books-cmdline-sample
29+
cd google-api-java-client-samples/books-cmdline-sample
30+
<i>[editor]</i> src/main/java/com/google/api/services/samples/books/cmdline/ClientCredentials.java
3331
mvn compile
3432
mvn -q exec:java -Dexec.args=Google</code></pre>
3533

books-cmdline-sample/logging.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Properties file which configures the operation of the JDK logging facility.
22
# The system will look for this config file to be specified as a system property:
3-
# -Djava.util.logging.config.file=${project_loc}/logging.properties
3+
# -Djava.util.logging.config.file=${project_loc:books-cmdline-sample}/logging.properties
44

55
# Set up the console handler (uncomment "level" to show more fine-grained messages)
66
handlers = java.util.logging.ConsoleHandler

books-cmdline-sample/pom.xml

+1-6
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,7 @@
126126
<dependency>
127127
<groupId>com.google.apis</groupId>
128128
<artifactId>google-api-services-books</artifactId>
129-
<version>v1-1.3.0-beta</version>
130-
</dependency>
131-
<dependency>
132-
<groupId>com.google.apis-samples</groupId>
133-
<artifactId>shared-sample-cmdline</artifactId>
134-
<version>1.1.0</version>
129+
<version>v1-rev2-1.4.0-beta</version>
135130
</dependency>
136131
</dependencies>
137132
<properties>

books-cmdline-sample/src/main/java/com/google/api/services/samples/books/cmdline/BooksSample.java

+4-13
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
package com.google.api.services.samples.books.cmdline;
1616

17-
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
18-
import com.google.api.client.http.HttpResponseException;
1917
import com.google.api.client.http.javanet.NetHttpTransport;
2018
import com.google.api.client.http.json.JsonHttpRequest;
2119
import com.google.api.client.http.json.JsonHttpRequestInitializer;
@@ -25,11 +23,9 @@
2523
import com.google.api.services.books.Books.Volumes.List;
2624
import com.google.api.services.books.BooksRequest;
2725
import com.google.api.services.books.model.Volume;
28-
import com.google.api.services.books.model.VolumeSaleInfo;
29-
import com.google.api.services.books.model.VolumeVolumeInfo;
3026
import com.google.api.services.books.model.Volumes;
31-
import com.google.api.services.samples.shared.cmdline.ClientCredentials;
3227

28+
import java.io.IOException;
3329
import java.net.URLEncoder;
3430
import java.text.NumberFormat;
3531

@@ -74,8 +70,8 @@ public void initialize(JsonHttpRequest request) {
7470

7571
// Output results.
7672
for (Volume volume : volumes.getItems()) {
77-
VolumeVolumeInfo volumeInfo = volume.getVolumeInfo();
78-
VolumeSaleInfo saleInfo = volume.getSaleInfo();
73+
Volume.VolumeInfo volumeInfo = volume.getVolumeInfo();
74+
Volume.SaleInfo saleInfo = volume.getSaleInfo();
7975
System.out.println("==========");
8076
// Title.
8177
System.out.println("Title: " + volumeInfo.getTitle());
@@ -170,13 +166,8 @@ public static void main(String[] args) {
170166
queryGoogleBooks(jsonFactory, query);
171167
// Success!
172168
return;
173-
} catch (GoogleJsonResponseException e) {
174-
// message already includes parsed response
169+
} catch (IOException e) {
175170
System.err.println(e.getMessage());
176-
} catch (HttpResponseException e) {
177-
// message doesn't include parsed response
178-
System.err.println(e.getMessage());
179-
System.err.println(e.getResponse().parseAsString());
180171
}
181172
} catch (Throwable t) {
182173
t.printStackTrace();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2011 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.services.samples.books.cmdline;
16+
17+
/**
18+
* API key found in the <a href="https://code.google.com/apis/console">Google apis console</a>.
19+
*
20+
* <p>
21+
* Once at the Google apis console, click on "Add project...". If you've already set up a project,
22+
* you may use that one instead, or create a new one by clicking on the arrow next to the project
23+
* name and click on "Create..." under "Other projects". For each API you want to use, click on the
24+
* status switch to flip it to "ON", and agree to the terms of service. Finally, click on "API
25+
* Access". Look for the section at the bottom called "Simple API Access".
26+
* </p>
27+
*
28+
* @author Ravi Mistry
29+
*/
30+
public class ClientCredentials {
31+
32+
/** Value of the "API key" shown under "Simple API Access". */
33+
public static final String KEY = null;
34+
35+
public static void errorIfNotSpecified() {
36+
if (KEY == null) {
37+
System.err.println("Please enter your API key in " + ClientCredentials.class);
38+
System.exit(1);
39+
}
40+
}
41+
}

chromewebstore-cmdline-sample/.classpath

-9
This file was deleted.

chromewebstore-cmdline-sample/.project

-23
This file was deleted.

chromewebstore-cmdline-sample/.settings/org.eclipse.jdt.core.prefs

-12
This file was deleted.

0 commit comments

Comments
 (0)