शुरू करें

इस गाइड में, Google Ads API .NET लाइब्रेरी का इस्तेमाल शुरू करने के बारे में खास जानकारी दी गई है.

इंस्टॉल करना

क्लाइंट लाइब्रेरी बाइनरी, NuGet का इस्तेमाल करके डिस्ट्रिब्यूट की जाती हैं. क्लाइंट लाइब्रेरी का इस्तेमाल करने के लिए, अपने प्रोजेक्ट में Google.Ads.GoogleAds पैकेज में Nuget का रेफ़रंस जोड़ें.

अनुमति देने की सुविधा सेट अप करें

अपने एपीआई कॉल को अनुमति देने के लिए, आपको लाइब्रेरी में अपना Client-ID, क्लाइंट सीक्रेट, रीफ़्रेश टोकन, और डेवलपर टोकन बताना होगा.

अगर आपके क्रेडेंशियल पहले से मौजूद हैं...

  • GitHub की App.config फ़ाइल में मौजूद configSections नोड और GoogleAdsApi नोड को, अपनी App.config / Web.config फ़ाइल में कॉपी करें.GoogleAdsApi अगर आपने पैकेज इंस्टॉल करने के लिए NuGet का इस्तेमाल किया है, तो ये नोड आपकी App.config / Web.config फ़ाइल में अपने-आप शामिल हो जाएंगे.
  • अपने ऐप्लिकेशन के App.config / Web.config में डेवलपर टोकन, क्लाइंट आईडी, क्लाइंट सीक्रेट, और रीफ़्रेश टोकन शामिल करें. GitHub में शामिल App.config फ़ाइल के बारे में अच्छी तरह से बताया गया है. हालांकि, ज़्यादा जानकारी और दूसरी कॉन्फ़िगरेशन सेटिंग इस्तेमाल करने के लिए, कॉन्फ़िगरेशन गाइड भी देखी जा सकती है.

अगर आपको क्रेडेंशियल जनरेट करने हैं...

  • अगर आपके पास पहले से डेवलपर टोकन नहीं है, तो उसे पाने के लिए डेवलपर टोकन गाइड देखें.
  • क्लाइंट आईडी, क्लाइंट सीक्रेट, और रीफ़्रेश टोकन जनरेट करने के लिए, OAuth डेस्कटॉप ऐप्लिकेशन फ़्लो गाइड का पालन करें.
  • GitHub की App.config फ़ाइल में मौजूद configSections नोड और GoogleAdsApi नोड को, अपनी App.config / Web.config फ़ाइल में कॉपी करें.GoogleAdsApi अगर आपने पैकेज इंस्टॉल करने के लिए 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.V17.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 का तरीका उपलब्ध है. इसका इस्तेमाल Google Ads की सेवा बनाने के लिए किया जा सकता है.

CampaignServiceClient campaignService = client.GetService(Services.V17.CampaignService);
// Now make calls to CampaignService.

हम एक Services क्लास देते हैं, जिसमें काम करने वाले एपीआई के सभी वर्शन और सेवाओं की सूची होती है. सेवा बनाते समय, GetService वाले तरीके में इन एन्युमरेशन ऑब्जेक्ट को तर्क के तौर पर स्वीकार किया जाता है. उदाहरण के लिए, Google Ads API के वर्शन V17 के लिए CampaignServiceClient का इंस्टेंस बनाने के लिए, आपको GoogleAdsClient.GetService तरीके को आर्ग्युमेंट के तौर पर Services.V17.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 Framework ऐप्लिकेशन में फ़्रीज़ होने से बचना

सिंक्रोनस तरीकों की वजह से, .NET फ़्रेमवर्क के कुछ ऐप्लिकेशन फ़्रीज़ हो सकते हैं. इसका एक सामान्य उदाहरण है, किसी WinForm ऐप्लिकेशन के इवेंट हैंडलर तरीके से एपीआई कॉल करना.

इस समस्या को हल करने के दो तरीके हैं:

  1. लेगसी Grpc लाइब्रेरी का इस्तेमाल करें.

    आपके पास डिफ़ॉल्ट Grpc.Net.Client लाइब्रेरी के बजाय, लेगसी Grpc.Core लाइब्रेरी का इस्तेमाल करने के लिए, GoogleAdsConfig की UseGrpcCore प्रॉपर्टी को सेट करने का विकल्प है. .NET फ़्रेमवर्क के ऐप्लिकेशन पर इस तरीके की बहुत ज़्यादा जांच नहीं की गई है, इसलिए हो सकता है कि इससे समस्या हल न हो. यहां एक सैंपल स्निपेट दिया गया है:

    GoogleAdsConfig config = new GoogleAdsConfig();
    config.UseGrpcCore = true;
    GoogleAdsClient client = new GoogleAdsClient(config);
    
  2. एसिंक्रोनस तरीकों का इस्तेमाल करें.

    फ़्रीज़ से बचने के लिए, एसिंक्रोनस तरीकों का इस्तेमाल किया जा सकता है. यहां कुछ उदाहरण दिए गए हैं:

    SearchStream

    SearchStream() को कॉल किया जाता है और नतीजों को सूची के तौर पर अपने-आप दिखाया जाता है.

    private async void button1_Click(object sender, EventArgs e)
    {
    // Get the GoogleAdsService.
    GoogleAdsServiceClient googleAdsService = client.GetService(
        Services.V17.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.V17.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);
    }
    

एक साथ काम नहीं करने वाली प्रोसेस रद्द की जा रही हैं

एक साथ काम नहीं करने वाली प्रोग्रामिंग के लिए, callSettings पैरामीटर का इस्तेमाल करके CancellationToken को पास किया जा सकता है:

CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.CancelAfter(3000);
CallSettings callSettings = CallSettings.FromCancellationToken(cancellationTokenSource.Token);

string query = "SELECT campaign.name FROM campaign";
var request = new SearchGoogleAdsStreamRequest()
{
    CustomerId = customerId.ToString(),
    Query = query,
};

GoogleAdsServiceClient googleAdsService = client.GetService(
    Services.V17.GoogleAdsService);

googleAdsService.SearchStream(request,
    delegate (SearchGoogleAdsStreamResponse resp)
    {
        foreach (GoogleAdsRow googleAdsRow in resp.Results)
        {
            // Process the row.
        }
    }, callSettings
);

गड़बड़ी ठीक करना

हर एपीआई कॉल पूरा नहीं होता. अगर किसी वजह से आपके एपीआई कॉल फ़ेल हो जाते हैं, तो सर्वर गड़बड़ी दिखा सकता है. एपीआई की गड़बड़ियों को कैप्चर करना और उन्हें सही तरीके से हैंडल करना ज़रूरी है.

एपीआई की कोई गड़बड़ी होने पर, GoogleAdsException इंस्टेंस दिखता है. इसमें ज़्यादा जानकारी दी गई है, ताकि आप गड़बड़ी का पता लगा सकें:

// Get the CampaignService.
CampaignServiceClient campaignService = client.GetService(Services.V17.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}");
}