微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 Google My Business API 创建帖子 - Google Apps Script

如何解决如何使用 Google My Business API 创建帖子 - Google Apps Script

当我尝试在 Google Apps 脚本中使用 GMB API 创建帖子时,我不明白为什么会出现以下错误。我正在关注此文档 https://developers.google.com/my-business/content/posts-data

{

 "error": {
    "code": 400,"message": "Request contains an invalid argument.","status": "INVALID_ARGUMENT","details": [
      {
        "@type": "type.googleapis.com/google.mybusiness.v4.ValidationError","errorDetails": [
          {
            "code": 2,"field": "summary","message": "Standard local post must have at least a media or summary."
          }
        ]
      }
    ]
  }
}

这是我的脚本

function callToActionPost() {
  var url = 'https://mybusiness.googleapis.com/v4/accounts/123/locations/456/localPosts';


  var options = {
    headers: { Authorization: "Bearer " + getGMBService_().getAccesstoken() },method: 'POST',muteHttpExceptions: true,languageCode: "en",topicType: "STANDARD",summary: "New Release!",callToAction: {
      actionType: "ORDER",url: "https://www.artivem.com/"
    },media: {
      sourceUrl: "https://untappd.akamaized.net/photos/2021_04_16/ccff4c358e362ce3c4835fcc94549a8f_640x640.jpg",mediaFormat: "PHOTO"
    }
  };

  var response = UrlFetchApp.fetch(url,options);
  Logger.log(response);
}

我尝试了以下改编,但没有奏效

function callToActionPost() {
      var url = 'https://mybusiness.googleapis.com/v4/accounts/123/locations/456/localPosts';
    
    
      var options = {
        headers: { Authorization: "Bearer " + getGMBService_().getAccesstoken() },payload: {
        languageCode: "en",callToAction: {
          actionType: "ORDER",url: "https://www.artivem.com/"
        },media: {
          sourceUrl: "https://untappd.akamaized.net/photos/2021_04_16/ccff4c358e362ce3c4835fcc94549a8f_640x640.jpg",mediaFormat: "PHOTO"
        }
      }
      };
    
      var response = UrlFetchApp.fetch(url,options);
      Logger.log(response);
    }

提前致谢!

解决方法

请求对象需要进入有效载荷。 “payload”是options的一个参数。

function callToActionPost() {
  var url = 'https://mybusiness.googleapis.com/v4/accounts/123/locations/456/localPosts';

  var payload = {
    "languageCode": "en-US","summary": "New Release!","callToAction": {
      "actionType": "ORDER","url": "https://www.artivem.com/"
    },"media": [{
      "sourceUrl": "https://untappd.akamaized.net/photos/2021_04_16/9999999999999999.jpg","mediaFormat": "PHOTO"
    }]
  }
  
  var options = {
    headers: { Authorization: "Bearer " + getGMBService_().getAccessToken() },method: 'POST',muteHttpExceptions: true,'contentType': 'application/json','payload' : JSON.stringify(payload)
  };

  var response = UrlFetchApp.fetch(url,options);
  Logger.log(response);
  
  if (response.getResponseCode() !== 200) {
    console.log("Error Code: " + response.getResponseCode());
    
  }
  
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。