क्लाइंट लाइब्रेरी

Merchant API के साथ इंटरैक्ट करने के इस तरीके में, पहले से बनी हुई लाइब्रेरी का इस्तेमाल किया जाता है. ये लाइब्रेरी, अनुरोध करने और जवाबों को मैनेज करने की बुनियादी जानकारी को शामिल करती हैं. इससे एपीआई के साथ इंटरैक्ट करने का तरीका ज़्यादा आसान हो जाता है. साथ ही, यह तरीका अक्सर भाषा के हिसाब से सही होता है. ये पुष्टि करने, अनुरोध करने, और जवाब को क्रमबद्ध/अक्रमबद्ध करने के साथ-साथ अन्य बॉयलरप्लेट कोड को मैनेज करते हैं.

पहला चरण. पुष्टि करने की सुविधा सेट अप करना और कोड सैंपल कॉन्फ़िगर करना

निर्देशों के लिए, Merchant API के लिए पुष्टि करने की सुविधा और कॉन्फ़िगरेशन के सैंपल सेट अप करना लेख पढ़ें.

दूसरा चरण. क्लाइंट लाइब्रेरी और कोड सैंपल डाउनलोड और इंस्टॉल करना

क्लाइंट लाइब्रेरी को डाउनलोड और इंस्टॉल करने के साथ-साथ GitHub से कोड के सैंपल इस्तेमाल करने के निर्देशों के लिए, Google Merchant API के Python सैंपल देखें.

तीसरा चरण. डेवलपर के रूप में पंजीकृत करें

Merchant API का इस्तेमाल करने के लिए, आपको डेवलपर की संपर्क जानकारी रजिस्टर करनी होगी.

रजिस्ट्रेशन से ये काम किए जा सकते हैं:

  • इस तरीके का इस्तेमाल करके, Merchant Center खाते के लिए तकनीकी संपर्क बनाया जाता है. इसके लिए, किसी उपयोगकर्ता को API developer भूमिका असाइन की जाती है. इससे Google, डेवलपर को एपीआई और डेवलपर की ओर से इस्तेमाल की जा रही सुविधाओं के बारे में अहम अपडेट भेज पाता है. जैसे, सेवा से जुड़ी सूचनाएं और नई सुविधाओं के बारे में जानकारी. यह जानकारी, डेवलपर के अलावा अन्य लोगों के लिए कम काम की हो सकती है.
  • इससे एक से ज़्यादा कारोबारी खातों को मैनेज किया जा सकता है. इसके लिए, आपको कई बार रजिस्टर नहीं करना पड़ता. रजिस्टर करने पर, Merchant API से पुष्टि करने के लिए इस्तेमाल किया गया Google Cloud प्रोजेक्ट आईडी, आपके Merchant Center खाते से जुड़ जाता है. इस खाते में तकनीकी संपर्क (API developer) होते हैं. इस तरह, आपको उन सभी कारोबारी या कंपनी के खातों के लिए अहम अपडेट मिल सकते हैं जिन्हें आपने मैनेज किया है. हालांकि, इसके लिए ज़रूरी है कि पुष्टि, रजिस्टर किए गए Google Cloud प्रोजेक्ट से की गई हो.

रजिस्टर करते समय, रजिस्ट्रेशन पेज पर दी गई ज़रूरी शर्तों और पाबंदियों का पालन करें.

क्लाइंट लाइब्रेरी का इस्तेमाल करके अपने प्रोजेक्ट को रजिस्टर करने के तरीके का उदाहरण देखने के लिए, Python का इस्तेमाल करके डेवलपर का ईमेल पता देकर, Google Cloud प्रोजेक्ट रजिस्टर करें का सैंपल देखें:

Python

# -*- coding: utf-8 -*-
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This example registers a GCP project with a developer email."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import DeveloperRegistrationServiceClient
from google.shopping.merchant_accounts_v1 import RegisterGcpRequest


def register_gcp(account_id: str, developer_email: str) -> None:
  """Registers the GCP project used to call the Merchant API with a developer email.

  Args:
    account_id: The ID of your Merchant Center account.
    developer_email: The email address of the developer to register.
  """
  # Get OAuth credentials.
  credentials = generate_user_credentials.main()

  # Create a client to the Developer Registration Service.
  client = DeveloperRegistrationServiceClient(credentials=credentials)

  # The name has the format: accounts/{account}/developerRegistration
  name = f"accounts/{account_id}/developerRegistration"

  # Create the request to register the GCP project.
  request = RegisterGcpRequest(
      name=name,
      developer_email=developer_email,
  )

  # Make the API call and handle potential errors.
  try:
    print("Sending RegisterGcp request:")
    response = client.register_gcp(request=request)
    print("Registered GCP project successfully:")
    print(response)
  except RuntimeError as e:
    print(f"An error occurred: {e}")


if __name__ == "__main__":

  # Your Merchant Center account ID.
  # This can be found in the Merchant Center UI.
  _account_id = configuration.Configuration().read_merchant_info()

  # The developer email to associate with the GCP project.
  _developer_email = "YOUR_EMAIL_HERE"

  register_gcp(_account_id, _developer_email)

चरण 4. डेवलपर के संपर्कों और अनुमतियों को मैनेज करना

रजिस्टर करने पर:

  • अगर ईमेल पता, Merchant Center खाते के किसी उपयोगकर्ता का है, तो उस उपयोगकर्ता को API_DEVELOPER की भूमिका असाइन की जाती है.
  • अगर ईमेल पता किसी मौजूदा उपयोगकर्ता का नहीं है, तो उस पते पर न्योता भेजा जाता है. API_DEVELOPER भूमिका के साथ नए उपयोगकर्ता के तौर पर जोड़े जाने के लिए, न्योता पाने वाले व्यक्ति को न्योता स्वीकार करना होगा.

हमारा सुझाव है कि शुरुआती रजिस्ट्रेशन के बाद, एक से ज़्यादा डेवलपर जोड़ें और उन्हें ऐक्सेस के ज़्यादा अधिकार दें.

चौथा चरण. अतिरिक्त अनुमतियां देना

अहम सूचनाएं पाने के लिए, API_DEVELOPER भूमिका ज़रूरी है. हालांकि, इसके पास Merchant Center में बहुत कम अनुमतियां होती हैं. इस उपयोगकर्ता को अन्य एपीआई कॉल करने या Merchant Center के यूज़र इंटरफ़ेस (यूआई) में सेटिंग मैनेज करने की अनुमति देने के लिए, आपको उसे अतिरिक्त भूमिकाएं देनी होंगी. जैसे, STANDARD या ADMIN. ज़्यादा जानकारी के लिए, ऐक्सेस के टाइप देखें.

accounts.users.patch तरीके का इस्तेमाल करके, किसी उपयोगकर्ता के ऐक्सेस के अधिकारों को अपडेट किया जा सकता है.

सैंपल कोड में दिखाया गया है कि किसी उपयोगकर्ता को ADMIN और API_DEVELOPER, दोनों भूमिकाएं देने के लिए उसे कैसे अपडेट किया जाता है. इससे उन्हें खाते को पूरी तरह से मैनेज करने का ऐक्सेस मिल जाता है. इसका मतलब है कि उन्हें एपीआई से जुड़े ईमेल भी मिलेंगे.

Python

# -*- coding: utf-8 -*-
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A module to update a user."""


from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.protobuf import field_mask_pb2
from google.shopping.merchant_accounts_v1 import AccessRight
from google.shopping.merchant_accounts_v1 import UpdateUserRequest
from google.shopping.merchant_accounts_v1 import User
from google.shopping.merchant_accounts_v1 import UserServiceClient

FieldMask = field_mask_pb2.FieldMask

_ACCOUNT = configuration.Configuration().read_merchant_info()


def update_user(user_email, user_access_right):
  """Updates a user to make it an admin of the MC account."""

  credentials = generate_user_credentials.main()

  client = UserServiceClient(credentials=credentials)

  # Create user name string
  name = "accounts/" + _ACCOUNT + "/users/" + user_email

  user = User(name=name, access_rights=[user_access_right])

  field_mask = FieldMask(paths=["access_rights"])

  try:
    request = UpdateUserRequest(user=user, update_mask=field_mask)

    print("Sending Update User request")
    response = client.update_user(request=request)
    print("Updated User Name below")
    print(response.name)
  except RuntimeError as e:
    print(e)


if __name__ == "__main__":
  # Modify this email to update the right user
  email = "USER_MAIL_ACCOUNT"
  access_right = AccessRight.ADMIN
  update_user(email, access_right)


चौथा चरण. बैकअप डेवलपर जोड़ना

अगर आपका प्राइमरी डेवलपर कॉन्टैक्ट आपके संगठन को छोड़ देता है, तो एपीआई का ऐक्सेस बाधित हो सकता है. इससे बचने के लिए, आपको कम से कम एक बैकअप डेवलपर जोड़ना चाहिए.

accounts.users.create तरीके का इस्तेमाल करके, किसी उपयोगकर्ता को जोड़ा जा सकता है. इसके अलावा, accounts.users.patch तरीके का इस्तेमाल करके, किसी मौजूदा उपयोगकर्ता की जानकारी अपडेट की जा सकती है. हमारा सुझाव है कि इस उपयोगकर्ता को ADMIN और API_DEVELOPER, दोनों भूमिकाएं असाइन करें.

चरण 5. प्रॉडक्ट का प्राइमरी डेटा सोर्स बनाना

किसी प्रॉडक्ट को जोड़ने से पहले, आपके पास प्रॉडक्ट का प्राइमरी डेटा सोर्स होना चाहिए. डेटा सोर्स बनाने वाले सैंपल कोड के लिए, Python की मदद से, एक ऐसा प्राइमरी प्रॉडक्ट डेटा सोर्स बनाने का सैंपल देखें जो कई भाषाओं के साथ काम करता हो लेख पढ़ें.

Python

# -*- coding: utf-8 -*-
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This class demonstrates how to create a Primary product datasource all `feedLabel` and `contentLanguage` combinations.

This works only for API primary feeds.
"""

from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_datasources_v1 import CreateDataSourceRequest
from google.shopping.merchant_datasources_v1 import DataSource
from google.shopping.merchant_datasources_v1 import DataSourcesServiceClient
from google.shopping.merchant_datasources_v1 import PrimaryProductDataSource

_ACCOUNT = configuration.Configuration().read_merchant_info()
_PARENT = f"accounts/{_ACCOUNT}"


def create_primary_product_data_source_multiple_languages():
  """Creates a `DataSource` resource."""

  # Gets OAuth Credentials.
  credentials = generate_user_credentials.main()

  # Creates a client.
  client = DataSourcesServiceClient(credentials=credentials)

  # Creates a PrimaryProductDataSource.
  primary_datasource = PrimaryProductDataSource()
  primary_datasource.countries = ["GB"]

  # Creates a DataSource and populates its attributes.
  data_source = DataSource()
  data_source.display_name = "Example Multiple Languages Primary DataSource"
  data_source.primary_product_data_source = primary_datasource

  # Creates the request.
  request = CreateDataSourceRequest(parent=_PARENT, data_source=data_source)

  # Makes the request and catches and prints any error messages.
  try:
    response = client.create_data_source(request=request)
    print(f"DataSource successfully created: {response}")
  except RuntimeError as e:
    print("DataSource creation failed")
    print(e)


if __name__ == "__main__":
  create_primary_product_data_source_multiple_languages()


इस डेटा सोर्स को Merchant Center के यूज़र इंटरफ़ेस (यूआई) में देखा जा सकता है. ज़्यादा जानकारी के लिए, डेटा सोर्स टैब को ढूंढने का तरीका लेख पढ़ें.

छठा चरण. एक प्रॉडक्ट डालें

डेटा सोर्स बनाने के बाद, उसमें कोई प्रॉडक्ट डालने की कोशिश करें. क्लाइंट लाइब्रेरी की मदद से ऐसा करने का तरीका बताने वाला सैंपल कोड देखने के लिए, Python की मदद से प्रॉडक्ट इनपुट का सैंपल डालें लेख पढ़ें.

Python

# -*- coding: utf-8 -*-
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A module to insert a Product Input."""

from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping import merchant_products_v1
from google.shopping.merchant_products_v1 import Availability
from google.shopping.merchant_products_v1 import Condition
from google.shopping.type import Price

_ACCOUNT = configuration.Configuration().read_merchant_info()
_PARENT = f"accounts/{_ACCOUNT}"

# You can only insert products into datasource types of Input "API" and
# "FILE", and of Type "Primary" or "Supplemental."
_DATA_SOURCE = "[INSERT_DATA_SOURCE_HERE]"
_DATA_SOURCE_NAME = f"accounts/{_ACCOUNT}/dataSources/{_DATA_SOURCE}"


def create_product_input():
  """Creates a `ProductInput` resource."""

  # Creates a shipping setting
  price = Price()
  price.amount_micros = 33_450_000
  price.currency_code = "GBP"

  shipping_option_1 = merchant_products_v1.Shipping()
  shipping_option_1.price = price
  shipping_option_1.country = "GB"
  shipping_option_1.service = "1st class post"

  price2 = Price()
  price2.amount_micros = 33_450_000
  price2.currency_code = "EUR"

  shipping_option_2 = merchant_products_v1.Shipping()
  shipping_option_2.price = price2
  shipping_option_2.country = "FR"
  shipping_option_2.service = "2nd class post"

  # Sets product attributes. Make sure to replace these values with your own.
  attributes = merchant_products_v1.ProductAttributes()
  attributes.title = "A Tale of Two Cities"
  attributes.description = "A classic novel about the French Revolution"
  attributes.link = "https://exampleWebsite.com/tale-of-two-cities.html"
  attributes.image_link = "https://exampleWebsite.com/tale-of-two-cities.jpg"
  attributes.price = price
  attributes.availability = Availability.IN_STOCK
  attributes.condition = Condition.NEW
  attributes.google_product_category = "Media > Books"
  attributes.gtins = ["9780007350896"]
  attributes.shipping = [shipping_option_1, shipping_option_2]

  return merchant_products_v1.ProductInput(
      content_language="en",
      feed_label="GB",
      offer_id="sku123",
      product_attributes=attributes,
  )


def insert_product_input():
  """Inserts the specified `ProductInput` resource."""

  # Gets OAuth Credentials.
  credentials = generate_user_credentials.main()

  # Creates a client.
  client = merchant_products_v1.ProductInputsServiceClient(
      credentials=credentials
  )

  # Creates the request.
  request = merchant_products_v1.InsertProductInputRequest(
      parent=_PARENT,
      # If this product is already owned by another datasource, when
      # re-inserting, the new datasource will take ownership of the product.
      product_input=create_product_input(),
      data_source=_DATA_SOURCE_NAME,
  )

  # Makes the request and catches and prints any error messages.
  try:
    response = client.insert_product_input(request=request)
    # The last part of the product name will be the product ID assigned to a
    # product by Google. Product ID has the format
    # `contentLanguage~feedLabel~offerId`
    print(f"Input successful: {response}")
  except RuntimeError as e:
    print("Input failed")
    print(e)
    # After the product is inserted, the product ID will be returned in the
    # response. We recommend that you check the Merchant Center to ensure that
    # the product is approved and visible to users before using the product ID
    # in any downstream processes.


if __name__ == "__main__":
  insert_product_input()


सातवां चरण. अपने प्रॉडक्ट की लिस्टिंग बनाना

आपके डेटा सोर्स में मौजूद प्रॉडक्ट की सूची बनाने वाले सैंपल कोड के लिए, Python का इस्तेमाल करके प्रॉडक्ट की सूची बनाने का सैंपल देखें.

Python

# -*- coding: utf-8 -*-
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A module to list Products."""

from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping import merchant_products_v1

_ACCOUNT = configuration.Configuration().read_merchant_info()
_PARENT = f"accounts/{_ACCOUNT}"


def list_products():
  """Lists the `Product` resources for a given account."""

  # Gets OAuth Credentials.
  credentials = generate_user_credentials.main()

  # Creates a client.
  client = merchant_products_v1.ProductsServiceClient(
      credentials=credentials
  )

  # Creates the request. Set the page size to the maximum value.
  request = merchant_products_v1.ListProductsRequest(
      parent=_PARENT, page_size=1000
  )

  # Makes the request and catches and prints any error messages.
  try:
    response = client.list_products(request=request)
    for product in response:
      print(product)
    print("List request successful!")
  except RuntimeError as e:
    print("List request failed")
    print(e)


if __name__ == "__main__":
  list_products()