การใช้งานพื้นฐานของไลบรารีของไคลเอ็นต์ .NET มีดังนี้
// Initialize a GoogleAdsConfig instance.
GoogleAdsConfig config = new GoogleAdsConfig()
{
OAuth2Mode = OAuth2Flow.SERVICE_ACCOUNT,
OAuth2SecretsJsonPath = "PATH_TO_CREDENTIALS_JSON",
LoginCustomerId = "INSERT_LOGIN_CUSTOMER_ID_HERE"
};
// Initialize a GoogleAdsClient instance.
GoogleAdsClient client = new GoogleAdsClient(config);
// Create the required service.
CampaignServiceClient campaignService =
client.GetService(Services.V25.CampaignService);
// Make calls to the service client.
เริ่มต้นไคลเอ็นต์และบริการ
หากต้องการโต้ตอบกับ Google Ads API ให้กำหนดค่าและสร้างอินสแตนซ์ของ
GoogleAdsClientก่อน จากนั้นใช้เพื่อสร้างไคลเอ็นต์บริการ API ที่เฉพาะเจาะจง
ที่คุณต้องการ
สร้างอินสแตนซ์ GoogleAdsClient
คลาสที่สำคัญที่สุดในไลบรารี .NET ของ Google Ads API คือคลาส GoogleAdsClient
ซึ่งช่วยให้คุณสร้างไคลเอ็นต์บริการที่กำหนดค่าล่วงหน้าซึ่งใช้สำหรับ
การเรียก API ได้ หากต้องการกำหนดค่าออบเจ็กต์ GoogleAdsClient ให้สร้างออบเจ็กต์
GoogleAdsConfig แล้วตั้งค่าพร็อพเพอร์ตี้ที่จำเป็น ดูข้อมูลเพิ่มเติมได้ที่คู่มือการกำหนดค่า
// Initialize a GoogleAdsConfig instance.
GoogleAdsConfig config = new GoogleAdsConfig()
{
OAuth2Mode = OAuth2Flow.SERVICE_ACCOUNT,
OAuth2SecretsJsonPath = "PATH_TO_CREDENTIALS_JSON",
LoginCustomerId = "INSERT_LOGIN_CUSTOMER_ID_HERE"
};
// Initialize a GoogleAdsClient instance.
GoogleAdsClient client = new GoogleAdsClient(config);
// Modify the GoogleAdsClient configuration afterwards if needed.
client.Config.LoginCustomerId = "INSERT_UPDATED_LOGIN_CUSTOMER_ID_HERE";
สร้างบริการ
GoogleAdsClient มีGetServiceเมธอดที่ใช้สร้างไคลเอ็นต์บริการ API ได้
CampaignServiceClient campaignService = client.GetService(
Services.V25.CampaignService);
// Now make calls to CampaignService.
ไลบรารีมีคลาส Services ที่แสดงรายการ API ทั้งหมดที่รองรับ (โดยรุ่นย่อย เช่น v25.1 จะใช้การแจงนับเวอร์ชันหลัก
Services.V25) และบริการ เมธอด GetService ยอมรับออบเจ็กต์การแจงนับเหล่านี้
เป็นอาร์กิวเมนต์เมื่อสร้างบริการ เช่น หากต้องการสร้างอินสแตนซ์ของ CampaignServiceClient สำหรับเวอร์ชัน V25 ของ Google Ads API ให้เรียกใช้เมธอด GoogleAdsClient.GetService โดยมี Services.V25.CampaignService เป็นอาร์กิวเมนต์ ดังที่แสดงในตัวอย่างก่อนหน้า
การจัดการข้อผิดพลาด
การเรียก API ไม่ได้สำเร็จทุกครั้ง เซิร์ฟเวอร์จะแสดงข้อผิดพลาดหากการเรียก API ล้มเหลวด้วยเหตุผลบางประการ คุณควรบันทึกข้อผิดพลาดของ API และจัดการข้อผิดพลาดเหล่านั้น อย่างเหมาะสม
ระบบจะส่งอินสแตนซ์ GoogleAdsException เมื่อเกิดข้อผิดพลาดของ API โดยมี
รายละเอียดที่จะช่วยให้คุณทราบว่าเกิดข้อผิดพลาดใด
public void Run(GoogleAdsClient client, long customerId) { // Get the GoogleAdsService. GoogleAdsServiceClient googleAdsService = client.GetService( Services.V25.GoogleAdsService); // Create a query that will retrieve all campaigns. string query = @"SELECT campaign.id, campaign.name, campaign.network_settings.target_content_network FROM campaign ORDER BY campaign.id"; try { // Issue a search request. googleAdsService.SearchStream(customerId.ToString(), query, delegate (SearchGoogleAdsStreamResponse resp) { foreach (GoogleAdsRow googleAdsRow in resp.Results) { Console.WriteLine("Campaign with ID {0} and name '{1}' was found.", googleAdsRow.Campaign.Id, googleAdsRow.Campaign.Name); } } ); } catch (GoogleAdsException e) { Console.WriteLine("Failure:"); Console.WriteLine($"Message: {e.Message}"); Console.WriteLine($"Failure: {e.Failure}"); Console.WriteLine($"Request ID: {e.RequestId}"); throw; } }
ความปลอดภัยของ Thread
การแก้ไขสถานะการกำหนดค่าของอินสแตนซ์ GoogleAdsClient ที่แชร์ในหลายเธรดไม่ปลอดภัยต่อเธรด เนื่องจาก
การเปลี่ยนแปลงการกำหนดค่าที่คุณทำในอินสแตนซ์ในเธรดหนึ่งอาจส่งผลต่อบริการที่คุณสร้างในเธรดอื่นๆ
อย่างไรก็ตาม การดำเนินการแบบอ่านอย่างเดียว เช่น การรับอินสแตนซ์บริการใหม่จากอินสแตนซ์ GoogleAdsClient ที่ไม่เปลี่ยนแปลงและการเรียกใช้บริการหลายรายการแบบขนานจะปลอดภัยต่อเธรด
หากต้องการแยกการเปลี่ยนแปลงการกำหนดค่าต่อเธรด ให้สร้างอินสแตนซ์แยกต่างหาก
GoogleAdsClientต่องานหรือเธรดของ Worker
GoogleAdsClient client1 = new GoogleAdsClient();
GoogleAdsClient client2 = new GoogleAdsClient();
Task task1 = Task.Run(() => AddAdGroups(client1));
Task task2 = Task.Run(() => AddAdGroups(client2));
await Task.WhenAll(task1, task2);
public void AddAdGroups(GoogleAdsClient client)
{
// Perform operations with client.
}
ทำให้แอปพลิเคชันตอบสนองได้ดี
การเรียกเมธอด Google Ads API อาจใช้เวลาสักครู่จึงจะเสร็จสมบูรณ์ ทั้งนี้ขึ้นอยู่กับขนาดของคำขอ หากต้องการให้แอปพลิเคชันตอบสนองได้ดี ให้ทำตามขั้นตอนต่อไปนี้
ใช้Grpc.Coreไลบรารีสำหรับเฟรมเวิร์ก UI เดิม
หากคุณกำลังพัฒนาแอปพลิเคชันที่กำหนดเป้าหมายเป็น .NET Framework และใช้เทคโนโลยี UI รุ่นเดิม เช่น ASP.NET Web Forms หรือ WinForms คุณสามารถเปิดใช้ไลบรารีการรับส่ง Grpc.Core รุ่นเดิมได้โดยทำดังนี้
GoogleAdsConfig config = new GoogleAdsConfig();
config.UseGrpcCore = true;
GoogleAdsClient client = new GoogleAdsClient(config);
ใช้วิธีการแบบไม่พร้อมกัน
คุณสามารถใช้วิธีการแบบอะซิงโครนัสเพื่อให้แอปพลิเคชันตอบสนองได้ ตัวอย่างมีดังนี้
ดึงข้อมูลรายการแคมเปญและป้อนข้อมูล ListView
private async void OnRetrieveCampaignsButtonClick(object sender, EventArgs e)
{
try
{
// Get the GoogleAdsService.
GoogleAdsServiceClient googleAdsService = client.GetService(
Services.V25.GoogleAdsService);
// Create a query that will retrieve all campaigns.
string query = @"SELECT
campaign.id,
campaign.name,
campaign.network_settings.target_content_network
FROM campaign
ORDER BY campaign.id";
List<ListViewItem> items = new List<ListViewItem>();
await googleAdsService.SearchStreamAsync(
customerId.ToString(),
query,
(SearchGoogleAdsStreamResponse resp) =>
{
foreach (GoogleAdsRow googleAdsRow in resp.Results)
{
ListViewItem item = new ListViewItem();
item.Text = googleAdsRow.Campaign.Id.ToString();
item.SubItems.Add(googleAdsRow.Campaign.Name);
items.Add(item);
}
}
);
listView1.Items.AddRange(items.ToArray());
}
catch (GoogleAdsException ex)
{
MessageBox.Show($"API Error: {ex.Message}");
}
}
อัปเดตงบประมาณแคมเปญและแสดงการแจ้งเตือนกล่องข้อความ
private async void OnUpdateBudgetButtonClick(object sender, EventArgs e)
{
try
{
// Get the CampaignBudgetService.
CampaignBudgetServiceClient budgetService = client.GetService(
Services.V25.CampaignBudgetService);
// Create the campaign budget.
CampaignBudget budget = new CampaignBudget()
{
Name = "Interplanetary Cruise Budget #" +
ExampleUtilities.GetRandomString(),
DeliveryMethod = BudgetDeliveryMethod.Standard,
AmountMicros = 500000
};
// Create the operation.
CampaignBudgetOperation budgetOperation = new CampaignBudgetOperation()
{
Create = budget
};
// Create the campaign budget asynchronously.
MutateCampaignBudgetsResponse response =
await budgetService.MutateCampaignBudgetsAsync(
customerId.ToString(),
new CampaignBudgetOperation[] { budgetOperation });
MessageBox.Show(response.Results[0].ResourceName);
}
catch (GoogleAdsException ex)
{
MessageBox.Show($"API Error: {ex.Message}");
}
}