Google-ContentAPI

 view release on metacpan or  search on metacpan

README.pod  view on Meta::CPAN

=head1 NAME

Google::ContentAPI - Interact with Google's Content API for Shopping

=head1 DESCRIPTION

Add, modify and delete items from the Google Merchant Center platform via
the Content API for Shopping.

Authentication is done via Service Account credentials. See the following for details:
L<https://developers.google.com/shopping-content/v2/how-tos/service-accounts>

You will also need to create a Merchant Center Account:
L<https://developers.google.com/shopping-content/v2/quickstart>

For convenience, add your Merchant account ID to the *.json file provided by Google.
Your complete *.json file, after adding your merchant ID, will look something like this:

  {
    "endpoint": "https://www.googleapis.com/content/v2.1",
    "merchant_id": "123456789",
    "type": "service_account",
    "project_id": "content-api-194321",
    "private_key_id": "11b8e20c2540c788e98b49e623ae8167dc3e4a6f",
    "private_key": "-----BEGIN PRIVATE KEY-----
    ...
    -----END PRIVATE KEY-----\n",
    "client_email": "google@content-api.iam.gserviceaccount.com",
    "client_id": "999999999",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/google%40content-api.iam.gserviceaccount.com"
  }

=head1 SYNOPSIS

  use Google::ContentAPI;
  use Data::Dumper;

  my $google = Google::ContentAPI->new({
      endpoint => 'https://www.googleapis.com/content/v2.1',
      debug => 0,
      config_file => 'content-api-key.json',
      config_json => $json_text,
      merchant_id => '123456789',
  });

  my ($result, $products, $batch_id, $product_id);

  # get account auth info (merchantId)
  $result = $google->get(
    resource => 'accounts',
    method   => 'authinfo'
  );
  print "$result->{code} ". ($result->{code} eq '200' ? 'success' : 'failure') ."\n";
  print "authinfo: \n". Dumper $result;

  # get status of your merchant center account
  $result = $google->get(
      resource => 'accountstatuses',
      method   => 'get',
      id => $google->{merchant_id} # your merchant ID unless working with multi-client account
  );
  print "$result->{code} ". ($result->{code} eq '200' ? 'success' : 'failure') ."\n";
  print "Account status: \n". Dumper $result;

  # list status of multi-client accounts (MCA)
  # This will fail with response code 403 if the account is not a multi-client account.
  $result = $google->get(
      resource => 'accountstatuses',
      method   => 'list',
      params   => ['maxResults' => 10]
  );
  print "$result->{code} ". ($result->{code} eq '200' ? 'success' : 'failure') ."\n";
  print "Account status: \n". Dumper $result;

  # list products

  $result = $google->get(
      resource => 'products',
      method   => 'list',
  );
  print "$result->{code} ". ($result->{code} eq '200' ? 'success' : 'failure') ."\n";
  print "Products list: \n". Dumper $result;

  # insert a product

  $result = $google->post(
      resource => 'products',
      method   => 'insert',
      body => {
        contentLanguage => 'en',
        targetCountry => 'US',
        channel => 'online',
        offerId => '333333',
        title => 'Item title',
        description => 'The item description',
        link => 'http://www.google.com',
        imageLink => 'https://www.google.com/images/logo.png',
        availability => 'in stock',
        condition => 'new',

README.pod  view on Meta::CPAN

      };
  }

  $result = $google->post(
      resource => 'products',
      method   => 'batch',
      body => { entries => $products }
  );
  print "$result->{code} ". ($result->{code} eq '200' ? 'success' : 'failure') ."\n";
  print Dumper $result;

  # batch delete:

  $products = [];
  $batch_id = 0;
  foreach my $product_id ('211203'..'211205') {
      push @$products, {
          batchId => ++$batch_id,
          merchantId => $google->{merchant_id},
          method => 'delete', # insert / get / delete
          productId => 'online:en:US:'. $product_id, # for get / delete
      };
  }

  $result = $google->post(
      resource => 'products',
      method   => 'batch',
      body => { entries => $products }
  );
  print "$result->{code} ". ($result->{code} eq '200' ? 'success' : 'failure') ."\n";

  # list status of products

  $result = $google->get(
      resource => 'productstatuses',
      method   => 'list',
      params   => [
          'maxResults' => 10
      ]
  );
  print "$result->{code} ". ($result->{code} eq '200' ? 'success' : 'failure') ."\n";
  print "Product status: \n". Dumper $result;

  # get status of a specific product

  $product_id = '333333';
  $result = $google->get(
      resource => 'productstatuses',
      method   => 'get',
      id => 'online:en:US:'. $product_id
  );
  print "$result->{code} ". ($result->{code} eq '200' ? 'success' : 'failure') ."\n";
  print "Product status: \n". Dumper $result;

=head1 METHODS AND FUNCTIONS

=head2 new()

Create a new Google::ContentAPI object

=head3 endpoint

API endpoint to use.
v2.1 endpoint is 'https://www.googleapis.com/content/v2.1'.
v2.0 endpoint is 'https://www.googleapis.com/content/v2'.
If not provided, default to v2.0 endpoint for backwards compatibility.

=head3 debug

Displays API debug information

=head3 config_file

Path and filename of external C<.json> config file.
Either config_file or config_json must be provided. If both config_file
and config_json are provided, config_file will be used.

=head3 config_json

Text containing contents of the json config. Useful if you want to
store the json config in another resource such as a database.
Either C<config_file> or C<config_json> must be provided. If both C<config_file>
and C<config_json> are provided, C<config_file> will be used.

=head3 merchant_id

optional if merchant_id is specified in json config

=head2 ACCOUNTS

=head3 authinfo

Returns information about the authenticated user.

=head2 ACCOUNTSTATUSES

=head3 list

Lists the the status of accounts in a Multi-Client account.
This will fail with response code C<403> if the account is not a multi-client account.

=head3 get

Retrieves the status of your Merchant Center account.

=head2 PRODUCTS

=head3 custombatch

Retrieves, inserts, and deletes multiple products in a single request.

=head3 insert

Uploads a product to your Merchant Center account. If an item with the
same channel, contentLanguage, offerId, and targetCountry already exists,
this method updates that entry.

=head3 list

Lists the products in your Merchant Center account.

=head3 get

Retrieves a product from your Merchant Center account.

=head3 delete



( run in 0.808 second using v1.01-cache-2.11-cpan-13bb782fe5a )