How to post localPost for topicType OFFER?

251 views Asked by At

I am trying to make Google My Business localPost for topicType offer in PHP.

I installed google/apiclient 2.7. Also, I have client library from GMB site as following. https://developers.google.com/my-business/samples/google-api-services-mybusiness-v4p7-php-rev-20200824-1.zip

I made following source code.

  $offer = new \Google_Service_MyBusiness_LocalPostOffer();
  $offer->setCouponCode($couponCode);
  $offer->setTermsConditions($termsCondition);
  $offer->setRedeemOnlineUrl($redeemOnlineUrl);

  $schedule = new \Google_Service_MyBusiness_TimeInterval();
  $date = new \Google_Service_MyBusiness_Date();
  $date->setDay(6);
  $date->setMonth(10);
  $date->setYear(2020);

  $time = new  \Google_Service_MyBusiness_TimeOfDay();
  $time->setHours(10);
  $time->setMinutes(0);
  $time->setMinutes(0);
  $time->setSeconds(0);
  $time->setNanos(0);

  $schedule->setStartDate($date);
  $schedule->setStartTime($time);
  $date->setDay(6);
  $date->setMonth(10);
  $date->setYear(2020);
  $time->setHours(19);
  $time->setMinutes(0);
  $time->setSeconds(0);
  $time->setNanos(0);
  $schedule->setEndDate($date);
  $schedule->setEndTime($time);
  $event = new \Google_Service_MyBusiness_LocalPostEvent();
  $event->setTitle($title);
  $event->setSchedule($schedule);

  $post = new \Google_Service_MyBusiness_LocalPost();
  $post->setSummary($summary);
  $post->setOffer($offer);
  $post->setEvent($event);
  $post->setTopicType("OFFER");

  $obj = new \Google_Service_MyBusiness($this->getClient('mybusiness'));
  $obj->accounts_locations_localPosts->create($accountLocation,$post)

However, it give me following error message.

{
  "error": {
    "code": 400,
    "message": "Request contains an invalid argument.",
    "errors": [
      {
        "message": "Request contains an invalid argument.",
        "domain": "global",
        "reason": "badRequest"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}

Do you have any suggetion for this?

Best regards,

2

There are 2 answers

0
Leo S On BEST ANSWER

The reason why I could not post offer is I did not reinitialise the followings when I set endDate and endTime.

Google_Service_MyBusiness_Date
Google_Service_MyBusiness_TimeOfDay

So, I might reinitialize as followings.

  $date = new \Google_Service_MyBusiness_Date();
  $time = new  \Google_Service_MyBusiness_TimeOfDay()
0
prabodhan pradhan On

This code creates offer post for me.

$service = new Google_Service_Mybusiness($client);
$post_body = new \Google_Service_MyBusiness_LocalPost;
$post_body->setLanguageCode('en');
$post_body->setSummary('test offer_1');

$offer = new \Google_Service_MyBusiness_LocalPostOffer;
$offer->setCouponCode('TEST-OFFE-CODE');
$offer->setRedeemOnlineUrl('https://www.example.com');
$offer->setTermsConditions('Offer not valid, it only test offer');
$post_body->setOffer($offer);

$event = new \Google_Service_MyBusiness_LocalPostEvent;
$event->setTitle('Test Offer post');
$schedule= new \Google_Service_MyBusiness_TimeInterval;

$startdate = new \Google_Service_MyBusiness_TimeInterval;
$date= new \Google_Service_MyBusiness_Date;
$date->setYear('2021');
$date->setMonth('10');
$date->setDay('02');
$schedule->setStartDate($date);

$starttime = new \Google_Service_MyBusiness_TimeRange;
$time = new \Google_Service_MyBusiness_TimeOfDay;
$time->setHours('9');
$time->setMinutes('0');
$time->setSeconds('0');
$time->setNanos('0');
$schedule->setStartTime($time);

$endDate = new \Google_Service_MyBusiness_TimeInterval;
$date= new \Google_Service_MyBusiness_Date;
$date->setYear('2021');
$date->setMonth('10');
$date->setDay('03');
$schedule->setEndDate($date);

$endtime = new \Google_Service_MyBusiness_TimeRange;
$time = new \Google_Service_MyBusiness_TimeOfDay;
$time->setHours('11');
$time->setMinutes('0');
$time->setSeconds('0');
$time->setNanos('0');
$schedule->setEndTime($time);

$event->setSchedule($schedule);
$post_body->setEvent($event);
$post_body->setTopicType('OFFER');

$media = new \Google_Service_MyBusiness_MediaItem;
$media->setMediaFormat('PHOTO');
$media->setSourceUrl('https://image.shutterstock.com/image-photo/white-transparent-leaf-on-mirror-260nw-1029171697.jpg');
$post_body->setMedia($media);
$accounts = $service->accounts->listAccounts()->getAccounts();
$locations = $service->accounts_locations->listAccountsLocations($accounts[0]['name']);
$post = $service->accounts_locations_localPosts->create($locations[0]['name'], $post_body);