| 9005 |
manish.sha |
1 |
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
|
2 |
//
|
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
|
5 |
// 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
|
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
12 |
// See the License for the specific language governing permissions and
|
|
|
13 |
// limitations under the License.
|
|
|
14 |
|
|
|
15 |
package adwords.axis.v201306.reporting;
|
|
|
16 |
|
|
|
17 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
|
|
18 |
import com.google.api.ads.adwords.lib.jaxb.v201306.DownloadFormat;
|
|
|
19 |
import com.google.api.ads.adwords.lib.utils.ReportDownloadResponse;
|
|
|
20 |
import com.google.api.ads.adwords.lib.utils.ReportDownloadResponseException;
|
|
|
21 |
import com.google.api.ads.adwords.lib.utils.v201306.ReportDownloader;
|
|
|
22 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
|
|
23 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
|
|
24 |
import com.google.api.ads.common.lib.utils.Streams;
|
|
|
25 |
import com.google.api.client.auth.oauth2.Credential;
|
|
|
26 |
|
|
|
27 |
import java.io.File;
|
|
|
28 |
import java.io.FileOutputStream;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* This example downloads a criteria performance report with AWQL.
|
|
|
32 |
*
|
|
|
33 |
* Credentials and properties in {@code fromFile()} are pulled from the
|
|
|
34 |
* "ads.properties" file. See README for more info.
|
|
|
35 |
*
|
|
|
36 |
* @author Kevin Winter
|
|
|
37 |
*/
|
|
|
38 |
public class DownloadCriteriaReportWithAwql {
|
|
|
39 |
|
|
|
40 |
public static void main(String[] args) throws Exception {
|
|
|
41 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
|
|
42 |
// and can be used in place of a service account.
|
|
|
43 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
|
|
44 |
.forApi(Api.ADWORDS)
|
|
|
45 |
.fromFile()
|
|
|
46 |
.build()
|
|
|
47 |
.generateCredential();
|
|
|
48 |
|
|
|
49 |
// Construct an AdWordsSession.
|
|
|
50 |
AdWordsSession session = new AdWordsSession.Builder()
|
|
|
51 |
.fromFile()
|
|
|
52 |
.withOAuth2Credential(oAuth2Credential)
|
|
|
53 |
.build();
|
|
|
54 |
|
|
|
55 |
// Location to download report to.
|
|
|
56 |
String reportFile = System.getProperty("user.home") + File.separatorChar + "report.csv";
|
|
|
57 |
|
|
|
58 |
runExample(session, reportFile);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public static void runExample(AdWordsSession session, String reportFile) throws Exception {
|
|
|
62 |
// Create query.
|
|
|
63 |
String query = "SELECT CampaignId, AdGroupId, Id, Criteria, CriteriaType, " +
|
|
|
64 |
"Impressions, Clicks, Cost FROM CRITERIA_PERFORMANCE_REPORT " +
|
|
|
65 |
"WHERE Status IN [ACTIVE, PAUSED] " +
|
|
|
66 |
"DURING YESTERDAY";
|
|
|
67 |
|
|
|
68 |
try {
|
|
|
69 |
// Set the property api.adwords.reportDownloadTimeout or call
|
|
|
70 |
// ReportDownloader.setReportDownloadTimeout to set a timeout (in milliseconds)
|
|
|
71 |
// for CONNECT and READ in report downloads.
|
|
|
72 |
ReportDownloadResponse response =
|
|
|
73 |
new ReportDownloader(session).downloadReport(query, DownloadFormat.CSV);
|
|
|
74 |
FileOutputStream fos = new FileOutputStream(new File(reportFile));
|
|
|
75 |
Streams.copy(response.getInputStream(), fos);
|
|
|
76 |
fos.close();
|
|
|
77 |
System.out.println("Report successfully downloaded: " + reportFile);
|
|
|
78 |
} catch (ReportDownloadResponseException e) {
|
|
|
79 |
System.out.println("Report was not downloaded. " + e.toString());
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
}
|