शुरू करें

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

इंस्टॉल करना

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

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

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

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

अगर आपके पास क्रेडेंशियल पहले से हैं, तो

  • GoogleAdsApi नोड और GoogleAdsApi सेक्शन को कॉपी करें App.config फ़ाइल से इसमें configSections नोड GitHub आपकी App.config / Web.config फ़ाइल में. अगर आपने तो ये नोड अपने आप आपके App.config / Web.config फ़ाइल.
  • डेवलपर टोकन, क्लाइंट आईडी, क्लाइंट सीक्रेट, और रीफ़्रेश टोकन शामिल करें आपके ऐप्लिकेशन के App.config / Web.config में.

App.config GitHub में शामिल फ़ाइल के बारे में अच्छी तरह से बताया गया है, लेकिन आप चाहें, तो कॉन्फ़िगरेशन गाइड इसके बारे में ज़्यादा जानने और क्लाइंट लाइब्रेरी को कॉन्फ़िगर करने के वैकल्पिक तरीकों का इस्तेमाल करने के लिए, जैसे कि एनवायरमेंट वैरिएबल.

एपीआई कॉल करें

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

// 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 तरीका उपलब्ध कराता है. इसका इस्तेमाल करके, विज्ञापन सेवा.

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 Framework ऐप्लिकेशन, फ़्रीज़ करो. इसका एक सामान्य उदाहरण किसी इवेंट हैंडलर तरीके से एपीआई कॉल करना है भी मिलती है.

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

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

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

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

    gRPC सहायता पेज पर आपको लेगसी Grpc.Core लाइब्रेरी और डिफ़ॉल्ट Grpc.Net.Client लाइब्रेरी.

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

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

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

एपीआई की कोई गड़बड़ी होने पर, 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}");
}