【问题标题】:How to create local post on google my business page using php如何使用 php 在 google 我的业务页面上创建本地帖子
【发布时间】:2018-09-15 10:35:16
【问题描述】:

我需要使用 google 客户端 api 创建一个本地主机。我编写了以下代码来创建本地帖子。

$service = new Google_Service_MyBusiness($client);
$callToAction = new Google_Service_MyBusiness_CallToAction();
$callToAction->setActionType('LEARN_MORE');
$callToAction->setUrl('localhost.com');
$mediaItem = new Google_Service_MyBusiness_MediaItem();
$mediaItem->setMediaFormat('PHOTO');
$mediaItem->setSourceUrl('https://www.theanthem.com/images/usps_eddm_postcards.jpg');
$localPost = new Google_Service_MyBusiness_LocalPost();
$localPost->setLanguageCode('en-US');
$localPost->setSummary('Just a summary');
$localPost->setCallToAction($callToAction);
$localPost->setMedia($mediaItem);
$parent = sprintf('accounts/%d/locations/%d',
'116633170334837786295',
'8830395735149945322'
);
$service->accounts_locations_localPosts->create($parent, $localPost); 

但不幸的是,我遇到了以下错误。

我的错误:消息:调用 POST 时出错 https://mybusiness.googleapis.com/v4/accounts/9223372036854775807/locations/8830395735149945322/localPosts: (400) 请求包含无效参数。

我该如何解决这个问题?

【问题讨论】:

    标签: php google-api google-api-php-client


    【解决方案1】:

    我找到了答案:

    创建刷新令牌

     $request_url = "https://www.googleapis.com/oauth2/v4/token";
     $refresh_token = "*** Refresf Token ***";
     $params = [
                'client_id'     => "***your client id***",
                'client_secret' => "***your clinet secret id***",
                'refresh_token' => $refresh_token,
                'grant_type'    => "refresh_token"
              ];
      $curl = curl_init($request_url);
      curl_setopt($curl, CURLOPT_HEADER, true);
      curl_setopt($curl, CURLINFO_HEADER_OUT, true);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_POST, true);
      curl_setopt($curl, CURLOPT_HEADER,'Content-Type: application/x-www-form-urlencoded');
      $postData = "";
      //This is needed to properly form post the credentials object
      foreach($params as $k => $v) {
        $postData .= $k . '='.urlencode($v).'&';
      }
      $postData = rtrim($postData, '&');
      curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
      $json_response = curl_exec($curl);
      $response = (array) json_decode( $json_response );
      $myaccess_token = $response['access_token'];
    

    并创建帖子

    $location = 'accounts/accoutid/locations/location id';
    $api_end_point_url = 'https://mybusiness.googleapis.com/v4/'.$location.'/localPosts';
    $postfields = array(
    'topicType' => "STANDARD",
    'languageCode' => "en_US",
    'summary' => 'test post 123',
    );
    $data_string = json_encode($postfields); 
    $ch = curl_init();      
    curl_setopt($ch, CURLOPT_URL, $api_end_point_url);      
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);     
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $myaccess_token,'Content-Type: application/json'));
    $data1 = json_decode(curl_exec($ch), true);
    $http_code1 = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    

    【讨论】:

    • 我尝试了将数据发布到“Google 我的商家”的代码,但有些代码无法正常工作并让我出错。此位置无权创建/更新本地帖子。错误代码 403 你能帮我吗?
    • 嗨,您是否设法解决了这个问题,“此位置无权创建/更新本地帖子”?是否需要验证您的位置?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多