बुनियादी इस्तेमाल

क्लाइंट लाइब्रेरी का बुनियादी इस्तेमाल इस तरह किया जाता है:


// Initialize a GoogleAdsConfig class.
GoogleAdsConfig config = new GoogleAdsConfig()
{
    DeveloperToken = "******",
    OAuth2Mode = OAuth2Flow.SERVICE_ACCOUNT,
    OAuth2SecretsJsonPath = "PATH_TO_CREDENTIALS_JSON",
    LoginCustomerId = ******
};

// Initialize a GoogleAdsClient class.
GoogleAdsClient client = new GoogleAdsClient(config);

// Create the required service.
CampaignServiceClient campaignService =
    client.GetService(Services.V21.CampaignService);

// Make more calls to service class.

GoogleAdsClient इंस्टेंस बनाना

Google Ads API की .NET लाइब्रेरी में सबसे अहम क्लास, GoogleAdsClient क्लास है. इसकी मदद से, पहले से कॉन्फ़िगर किया गया एक ऐसा सर्विस क्लास बनाया जा सकता है जिसका इस्तेमाल एपीआई कॉल करने के लिए किया जा सकता है. GoogleAdsClient ऑब्जेक्ट को कॉन्फ़िगर करने के लिए, आपको GoogleAdsConfig ऑब्जेक्ट बनाना होगा और ज़रूरी सेटिंग सेट करनी होंगी. ज़्यादा जानने के लिए, कॉन्फ़िगरेशन गाइड देखें.


// Initialize a GoogleAdsConfig class.
GoogleAdsConfig config = new GoogleAdsConfig()
{
    DeveloperToken = "******",
    OAuth2Mode = OAuth2Flow.SERVICE_ACCOUNT,
    OAuth2SecretsJsonPath = "PATH_TO_CREDENTIALS_JSON",
    LoginCustomerId = ******
};

// Initialize a GoogleAdsClient class.
GoogleAdsClient client = new GoogleAdsClient(config);

// Modify the GoogleAdsClient afterwards.

client.Config.LoginCustomerId = ******;

कोई सेवा बनाना

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 के साथ आर्ग्युमेंट के तौर पर कॉल करना होगा. जैसा कि पिछले उदाहरण में दिखाया गया है.

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

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

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

public void Run(GoogleAdsClient client, long customerId)
{
    // 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";

    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;
    }
}
      

थ्रेड सेफ़्टी

एक 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.
  ...
}

अपने ऐप्लिकेशन को रिस्पॉन्सिव बनाए रखना

Google Ads API के एपीआई तरीके के कॉल पूरे होने में कुछ समय लग सकता है. यह इस बात पर निर्भर करता है कि अनुरोध कितने बड़े हैं. अपने ऐप्लिकेशन को रिस्पॉन्सिव बनाए रखने के लिए, हमारा सुझाव है कि आप यह तरीका अपनाएं:

Grpc.Core लाइब्रेरी का इस्तेमाल करना

अगर आपको .NET Framework को टारगेट करने वाला कोई ऐप्लिकेशन डेवलप करना है और उसमें ASP.NET Web Forms या WinForms ऐप्लिकेशन जैसी लेगसी टेक्नोलॉजी का इस्तेमाल करना है, तो हमारा सुझाव है कि आप लेगसी Grpc.Core लाइब्रेरी का इस्तेमाल इस तरह करें:

   GoogleAdsConfig config = new GoogleAdsConfig();
   config.UseGrpcCore = true;
   GoogleAdsClient client = new GoogleAdsClient(config);
   ```

### Use the asynchronous methods {: #asynchronous}

You can use asynchronous methods to keep your application responsive. Here are a
couple of examples.

#### Retrieve the list of campaigns, and populate a `ListView`

```c#
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<ListViewItem> items = new List<ListViewItem>();
    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());
}
```

#### Update a campaign budget and display a Message box alert

```c#
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<MutateCampaignBudgetsResponse> t = budgetService.MutateCampaignBudgetsAsync(
        customerId.ToString(), new CampaignBudgetOperation[] { budgetOperation });

    await t;
    MutateCampaignBudgetsResponse response = t.Result;
    MessageBox.Show(response.Results[0].ResourceName);
}
```