इस गाइड में, Google Ads API की .NET लाइब्रेरी का इस्तेमाल शुरू करने के बारे में खास जानकारी दी गई है.
इंस्टॉल करना
क्लाइंट लाइब्रेरी के बाइनरी, NuGet का इस्तेमाल करके डिस्ट्रिब्यूट किए जाते हैं. क्लाइंट लाइब्रेरी का इस्तेमाल करने के लिए, अपने प्रोजेक्ट में Google.Ads.GoogleAds
पैकेज में NuGet रेफ़रंस जोड़ें.
अनुमति सेट अप करना
अपने एपीआई कॉल को अनुमति देने के लिए, आपको लाइब्रेरी में अपना क्लाइंट आईडी, क्लाइंट सीक्रेट, रीफ़्रेश टोकन, और डेवलपर टोकन डालना होगा.
अगर आपको क्रेडेंशियल जनरेट करने हैं
- अगर आपके पास डेवलपर टोकन नहीं है, तो डेवलपर टोकन गाइड में दिए गए निर्देशों का पालन करके, डेवलपर टोकन पाएं.
- क्लाइंट आईडी, क्लाइंट सीक्रेट, और रीफ़्रेश टोकन जनरेट करने के लिए, OAuth डेस्कटॉप ऐप्लिकेशन फ़्लो गाइड का पालन करें.
अगर आपके पास पहले से ही क्रेडेंशियल हैं
- GitHub में मौजूद
App.config
फ़ाइल से,GoogleAdsApi
नोड औरconfigSections
नोड के नीचे मौजूदGoogleAdsApi
सेक्शन को कॉपी करके, अपनीApp.config
/Web.config
फ़ाइल में चिपकाएं. अगर आपने NuGet का इस्तेमाल करके पैकेज इंस्टॉल किया है, तो ये नोड आपकीApp.config
/Web.config
फ़ाइल में अपने-आप जुड़ जाएंगे. - अपने ऐप्लिकेशन के
App.config
/Web.config
में डेवलपर टोकन, क्लाइंट आईडी, क्लाइंट सीक्रेट, और रीफ़्रेश टोकन शामिल करें.
GitHub में शामिल App.config
फ़ाइल के बारे में पूरी जानकारी दी गई है. हालांकि, ज़्यादा जानने के लिए कॉन्फ़िगरेशन गाइड भी देखी जा सकती है. साथ ही, क्लाइंट लाइब्रेरी को कॉन्फ़िगर करने के लिए, एनवायरमेंट वैरिएबल जैसे अन्य तरीकों का इस्तेमाल किया जा सकता है.
एपीआई कॉल करना
क्लाइंट लाइब्रेरी का बुनियादी इस्तेमाल इस तरह किया जाता है:
// Create a Google Ads client.
GoogleAdsClient client = new GoogleAdsClient();
// Create the required service.
CampaignServiceClient campaignService =
client.GetService(Services.V21.CampaignService);
// Make more calls to service class.
GoogleAdsClient इंस्टेंस बनाना
Google Ads API की .NET लाइब्रेरी में सबसे अहम क्लास, GoogleAdsClient
क्लास है. इसकी मदद से, पहले से कॉन्फ़िगर की गई एक ऐसी सेवा क्लास बनाई जा सकती है जिसका इस्तेमाल एपीआई कॉल करने के लिए किया जा सकता है. GoogleAdsClient
एक डिफ़ॉल्ट कंस्ट्रक्टर उपलब्ध कराता है. यह आपके ऐप्लिकेशन के App.config
/ Web.config
में दी गई सेटिंग का इस्तेमाल करके, उपयोगकर्ता ऑब्जेक्ट बनाता है. कॉन्फ़िगरेशन के विकल्पों के लिए, कॉन्फ़िगरेशन गाइड देखें.
// Create a new GoogleAdsClient with the App.config settings.
GoogleAdsClient user = new GoogleAdsClient();
कोई सेवा बनाना
GoogleAdsClient
एक GetService
तरीका उपलब्ध कराता है, जिसका इस्तेमाल करके Ads सेवा बनाई जा सकती है.
CampaignServiceClient campaignService = client.GetService(Services.V21.CampaignService);
// Now make calls to CampaignService.
हम एक Services
क्लास उपलब्ध कराते हैं. इसमें एपीआई के सभी वर्शन और सेवाएं शामिल होती हैं. GetService
वाला तरीका, सेवा बनाते समय इन इन्यूमरेशन ऑब्जेक्ट को आर्ग्युमेंट के तौर पर स्वीकार करता है. उदाहरण के लिए, Google Ads API के V21
वर्शन के लिए CampaignServiceClient
का इंस्टेंस बनाने के लिए, आपको GoogleAdsClient.GetService
तरीके को Services.V21.CampaignService
आर्ग्युमेंट के तौर पर कॉल करना होगा. जैसा कि पिछले उदाहरण में दिखाया गया है.
थ्रेड सेफ़्टी
एक GoogleAdsClient
इंस्टेंस को कई थ्रेड के बीच शेयर करना सुरक्षित नहीं है. ऐसा इसलिए, क्योंकि एक थ्रेड में किसी इंस्टेंस पर किए गए कॉन्फ़िगरेशन में बदलाव, दूसरी थ्रेड पर बनाई गई सेवाओं पर असर डाल सकते हैं. GoogleAdsClient
इंस्टेंस से नए सेवा इंस्टेंस पाना और एक साथ कई सेवाओं को कॉल करना जैसे ऑपरेशन, थ्रेड-सेफ़ होते हैं.
मल्टीथ्रेड ऐप्लिकेशन कुछ ऐसा दिखेगा:
GoogleAdsClient client1 = new GoogleAdsClient();
GoogleAdsClient client2 = new GoogleAdsClient();
Thread userThread1 = new Thread(addAdGroups);
Thread userThread2 = new Thread(addAdGroups);
userThread1.start(client1);
userThread2.start(client2);
userThread1.join();
userThread2.join();
public void addAdGroups(object data) {
GoogleAdsClient client = (GoogleAdsClient) data;
// Do more operations here.
...
}
.NET फ़्रेमवर्क ऐप्लिकेशन में फ़्रीज़ होने की समस्या से बचना
सिंक्रोनस तरीकों की वजह से, आपके कुछ .NET Framework ऐप्लिकेशन फ़्रीज़ हो सकते हैं. इसका एक सामान्य उदाहरण, WinForm ऐप्लिकेशन के इवेंट हैंडलर तरीके से एपीआई कॉल करना है.
इस समस्या को हल करने के दो तरीके हैं:
लेगसी Grpc लाइब्रेरी का इस्तेमाल करें.
डिफ़ॉल्ट
Grpc.Net.Client
लाइब्रेरी के बजाय लेगसीGrpc.Core
लाइब्रेरी का इस्तेमाल करने के लिए,GoogleAdsConfig
कीUseGrpcCore
प्रॉपर्टी सेट की जा सकती है. इस तरीके को .NET फ़्रेमवर्क ऐप्लिकेशन पर अच्छी तरह से टेस्ट नहीं किया गया है. इसलिए, हो सकता है कि इससे समस्या हल न हो. यहां एक सैंपल स्निपेट दिया गया है:GoogleAdsConfig config = new GoogleAdsConfig(); config.UseGrpcCore = true; GoogleAdsClient client = new GoogleAdsClient(config);
एसिंक्रोनस तरीकों का इस्तेमाल करें.
फ़्रीज़ होने से बचने के लिए, एसिंक्रोनस तरीकों का इस्तेमाल किया जा सकता है. यहां कुछ उदाहरण दिए गए हैं:
SearchStream
SearchStream()
को कॉल किया जाता है और नतीजों को सूची के तौर पर दिखाया जाता है.private async void button1_Click(object sender, EventArgs e) { // Get the GoogleAdsService. GoogleAdsServiceClient googleAdsService = client.GetService( Services.V21.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
items = new List (); Task t = googleAdsService.SearchStreamAsync(customerId.ToString(), query, delegate (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); } } ); await t; listView1.Items.AddRange(items.ToArray()); } कैंपेन का बजट
CampaignBudget कॉल बनाया जाता है. साथ ही, नए बजट का संसाधन नाम,
MessageBox
सूचना का इस्तेमाल करके दिखाया जाता है.private async void button2_Click(object sender, EventArgs e) { // Get the BudgetService. CampaignBudgetServiceClient budgetService = client.GetService( Services.V21.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. Task
t = budgetService.MutateCampaignBudgetsAsync( customerId.ToString(), new CampaignBudgetOperation[] { budgetOperation }); await t; MutateCampaignBudgetsResponse response = t.Result; MessageBox.Show(response.Results[0].ResourceName); }
गड़बड़ी ठीक करना
यह ज़रूरी नहीं है कि हर एपीआई कॉल पूरा हो. अगर किसी वजह से आपके एपीआई कॉल पूरे नहीं हो पाते, तो सर्वर गड़बड़ियां दिखा सकता है. एपीआई की गड़बड़ियों को कैप्चर करना और उन्हें सही तरीके से ठीक करना ज़रूरी है.
एपीआई से जुड़ी गड़बड़ी होने पर, GoogleAdsException
इंस्टेंस थ्रो किया जाता है. इसमें गड़बड़ी के बारे में जानकारी होती है, ताकि आपको यह पता चल सके कि क्या गड़बड़ी हुई है:
// Get the CampaignService.
CampaignServiceClient campaignService = client.GetService(Services.V21.CampaignService);
// Create a campaign for update.
Campaign campaignToUpdate = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
// More fields to update.
// ...
};
// Create the operation.
CampaignOperation operation = new CampaignOperation()
{
Update = campaignToUpdate,
UpdateMask = FieldMasks.AllSetFieldsOf(campaignToUpdate)
};
try
{
// Update the campaign.
MutateCampaignsResponse response = campaignService.MutateCampaigns(
customerId.ToString(), new CampaignOperation[] { operation });
// Display the results.
// ...
}
catch (GoogleAdsException e)
{
Console.WriteLine("Failure:");
Console.WriteLine($"Message: {e.Message}");
// Can examine to get more error details.
Console.WriteLine($"Failure: {e.Failure}");
// Can be shared with Google for further troubleshooting.
Console.WriteLine($"Request ID: {e.RequestId}");
}