如何post localPost for topicType OFFER?

How to post localPost for topicType OFFER?

我正尝试在 PHP.

中为 topicType 提供 Google 我的业务 localPost

我安装了 google/apiclient 2.7。 另外,我有来自 GMB 站点的客户端库,如下所示。 https://developers.google.com/my-business/samples/google-api-services-mybusiness-v4p7-php-rev-20200824-1.zip

我制作了以下源代码。

  $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)

但是,它给我以下错误信息。

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

你对此有什么建议吗?

此致,

我无法post提供的原因是我在设置endDate和endTime时没有重新初始化以下内容。

Google_Service_MyBusiness_Date
Google_Service_MyBusiness_TimeOfDay

所以,我可能会重新初始化如下。

  $date = new \Google_Service_MyBusiness_Date();
  $time = new  \Google_Service_MyBusiness_TimeOfDay()

此代码为我创建了报价 post。

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