Sau khi thiết lập xong mọi thứ, hãy đảm bảo bạn có thể gửi mẫu yêu cầu API Search Ads 360. Mã mẫu sau đây minh hoạ cách gửi yêu cầu không đồng bộ để biết danh sách các chiến dịch thuộc nhà quảng cáo của bạn. Nếu yêu cầu thành công, bạn sẽ nhận được phản hồi từ API Search Ads 360 cho biết rằng báo cáo đã được tạo nhưng chưa sẵn sàng (vì các yêu cầu không đồng bộ cần thêm các yêu cầu khác để tải một báo cáo xuống).
Bạn cần chỉ định mã truy cập OAuth 2.0 và mã công ty quảng cáo của riêng bạn trong yêu cầu. Cách tìm mã đại lý:
- Truy cập vào giao diện người dùng Search Ads 360.
- Mã đại lý và mã nhận dạng nhà quảng cáo của bạn sẽ xuất hiện trong URL, ngay sau thẻ
Tiền tố
ay=
vàav=
. Ví dụ:
https://searchads.google.com/ds/cm/cm/cm/cm#campaigns.ay=123456789012345678;av=123456789012345678;
JSON
POST https://www.googleapis.com/doubleclicksearch/v2/reports Authorization: Bearer your OAuth 2.0 access token Content-type: application/json { "reportScope": { "agencyId": "your agency ID", "advertiserId": "your advertiser ID" }, "reportType": "campaign", "columns": [ { "columnName": "campaignId" }, { "columnName": "campaign" } ], "downloadFormat": "csv", "maxRowsPerFile": 6000000, "statisticsCurrency": "agency" }
Hãy sử dụng tập lệnh tiện ích của Search Ads 360 để gửi yêu cầu này
Để gửi yêu cầu POST JSON thô, bạn có thể sử dụng sa360Api.py tập lệnh như sau:
- Sao chép đối tượng JSON mẫu (mọi thông tin nằm trong khoảng từ đến bao gồm cả hai đối tượng này)
dấu ngoặc nhọn) vào tệp văn bản mới có tên
request.txt
. - Thay đổi mã đại lý trong mã JSON thành mã đại lý của riêng bạn.
- Hãy xoá mọi nhận xét, chẳng hạn như
// The date column segments the report by individual days.
- Tập hợp thông tin đăng nhập OAuth 2.0 của bạn thành một chuỗi được phân tách bằng dấu phẩy dưới dạng
sau:
client-ID,client-secret,refresh-token
(Đây chính là chuỗi màsa360Api.py
xuất ra khi bạn chạysa360Api.py --login
như mô tả trong phần Thiết lập Uỷ quyền.) - Gọi
sa360Api.py
như sau:
sa360Api.py --cred CREDENTIALS --server API-method --post < request.txt
Trong lệnh trên, hãy thay thế chuỗi bạn đã tập hợp ở bước trước choCREDENTIALS
.
Thay thế tên của phương thức POST trong hộp bên dưới choAPI-method
.Ví dụ:
sa360Api.py --cred 123456789123.apps.googleusercontent.com,ABCDEFGHIJKLMNOPQR_abcdef,1/HIJklM01OPQR23NOP456rst890uvw --server https://www.googleapis.com/doubleclicksearch/v2/reports/generate --post < request.txt
Java
import com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.services.doubleclicksearch.Doubleclicksearch; import com.google.api.services.doubleclicksearch.model.ReportApiColumnSpec; import com.google.api.services.doubleclicksearch.model.ReportRequest; import com.google.api.services.doubleclicksearch.model.ReportRequest.ReportScope; import com.google.api.services.doubleclicksearch.model.ReportRequest.TimeRange; import java.io.IOException; import java.util.Arrays; /** * Creates a campaign report request, submits the report, and returns the report ID. */ private static String createReport(Doubleclicksearch service) throws IOException { try { return service.reports().request(createSampleRequest()).execute().getId(); } catch (GoogleJsonResponseException e) { System.err.println("Report request was rejected."); for (ErrorInfo error : e.getDetails().getErrors()) { System.err.println(error.getMessage()); } System.exit(e.getStatusCode()); return null; // Unreachable code. } } /** * Returns a simple static request that lists the ID and name of all * campaigns under agency 20100000000000895 and advertiser 21700000000011523. * Substitute your own agency ID and advertiser IDs for the IDs in this sample. */ private static ReportRequest createSampleRequest() { return new ReportRequest() .setReportScope(new ReportScope() .setAgencyId(20100000000000895L) // Replace with your ID .setAdvertiserId(21700000000011523L)) // Replace with your ID .setReportType("campaign") .setColumns(Arrays.asList( new ReportApiColumnSpec[] { new ReportApiColumnSpec().setColumnName("campaignId"), new ReportApiColumnSpec().setColumnName("campaign") })) .setTimeRange(new TimeRange() .setStartDate("2014-05-01") .setEndDate("2014-05-01")) .setDownloadFormat("csv") .setStatisticsCurrency("usd") .setMaxRowsPerFile(5000000); }
.NET
using System; using System.Collections.Generic; using System.IO; using System.Threading; using Google.Apis.Auth.OAuth2; using Google.Apis.Auth.OAuth2.Flows; using api = Google.Apis.Doubleclicksearch.v2; /// <summary> /// Creates a campaign report request, submits the report, and returns the report ID. /// </summary> /// <param name="service">Search Ads API service.</param> private static string CreateReport(api.DoubleclicksearchService service) { var req = service.Reports.Request(CreateSampleRequest()); var report = req.Execute(); Console.WriteLine("Created report: ID={0}", report.Id); return report.Id; } /// <summary> /// Returns a simple static request that lists the ID and name of all /// campaigns under agency 20100000000000895 and advertiser 21700000000011523. /// Substitute your own agency ID and advertiser IDs for the IDs in this sample. /// </summary> private static api.Data.ReportRequest CreateSampleRequest() { return new api.Data.ReportRequest { ReportScope = new api.Data.ReportRequest.ReportScopeData { AgencyId = 20100000000000895, AdvertiserId = 21700000000011523, }, ReportType = "campaign", Columns = new List<api.Data.ReportRequest.ColumnsData> { new api.Data.ReportRequest.ColumnsData { ColumnName = "campaignId", }, new api.Data.ReportRequest.ColumnsData { ColumnName = "campaign", }, }, TimeRange = new api.Data.ReportRequest.TimeRangeData { StartDate = "2014-01-01", EndDate = "2014-01-31", }, DownloadFormat = "csv", StatisticsCurrency = "usd", MaxRowsPerFile = 5000000, }; }
Python
def generate_report(service): """Generate and print sample report. Args: service: An authorized Doubleclicksearch service. See Set Up Your Application. """ request = service.reports().request( body = { "reportScope": { "agencyId": "your agency ID", "advertiserId": "your advertiser ID" }, "reportType": "campaign", "columns": [ { "columnName": "campaignId" }, { "columnName": "campaign" } ], "downloadFormat": "csv", "maxRowsPerFile": 6000000, "statisticsCurrency": "agency" } ) pprint.pprint(request.execute())