使用 Google Apps 脚本中的 goo.gl API 时出现问题

发布于 2024-11-18 02:07:02 字数 1392 浏览 2 评论 0原文

我正在尝试从 Google Apps 脚本内部查询 goo.gl API。我看到的问题是以下错误消息:

Request failed for https://www.googleapis.com/urlshortener/v1/url?key=AIXXXXXXXXXXXXXXXXXXXXXLmGJQw returned code 400. Server response: { "error": { "errors": [ { "domain": "global", "reason": "parseError", "message": "This API does not support parsing form-encoded input." } ], "code": 400, "message": "This API does not support parsing form-encoded input." } } (line 28)

当我尝试在 UrlFetchApp.fetch(post_url, options); 执行实际请求时,会出现该消息。

这是我在 Google Apps 脚本中使用的实际编码。

function minifyGoogl(longUrl) {
  var post_url = 'https://www.googleapis.com/urlshortener/v1/url';

  var apiKey = UserProperties.getProperty('googl_api_key');

  if(!apiKey){
    var apiKey = ScriptProperties.getProperty('googl_api_key');
  }

  if(apiKey){
    post_url += '?key=' + apiKey;
  }

  var payload = Utilities.jsonStringify({'longUrl': longUrl });

  var options = {
    'method' : 'post',
    'headers' : {
      'Content-Type' : 'application/json'
    },
    'payload' : payload
  };

  try{
    var response = UrlFetchApp.fetch(post_url, options);
  }catch(e){
    if(e.message){
      throw e.message;
    }
  }

  var responseJson = response.getAs('json');
}
function testMinifyGoogl(){
  minifyGoogl('http://eduardo.cereto.net');
}

I'm trying to query the goo.gl API from inside a Google Apps Script. The problem I'm seeing is the following error message:

Request failed for https://www.googleapis.com/urlshortener/v1/url?key=AIXXXXXXXXXXXXXXXXXXXXXLmGJQw returned code 400. Server response: { "error": { "errors": [ { "domain": "global", "reason": "parseError", "message": "This API does not support parsing form-encoded input." } ], "code": 400, "message": "This API does not support parsing form-encoded input." } } (line 28)

the message comes up when I try to do the actual request at UrlFetchApp.fetch(post_url, options);.

Here's the actual coding I'm using in Google Apps Script.

function minifyGoogl(longUrl) {
  var post_url = 'https://www.googleapis.com/urlshortener/v1/url';

  var apiKey = UserProperties.getProperty('googl_api_key');

  if(!apiKey){
    var apiKey = ScriptProperties.getProperty('googl_api_key');
  }

  if(apiKey){
    post_url += '?key=' + apiKey;
  }

  var payload = Utilities.jsonStringify({'longUrl': longUrl });

  var options = {
    'method' : 'post',
    'headers' : {
      'Content-Type' : 'application/json'
    },
    'payload' : payload
  };

  try{
    var response = UrlFetchApp.fetch(post_url, options);
  }catch(e){
    if(e.message){
      throw e.message;
    }
  }

  var responseJson = response.getAs('json');
}
function testMinifyGoogl(){
  minifyGoogl('http://eduardo.cereto.net');
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝影如岚 2024-11-25 02:07:02

文档表示 contentType 默认为'application/x-www-form-urlencoded'。

也许尝试使用 contentType 参数设置 Content-Type 而不是手动插入 Content-Type 标头?

The documentation says the contentType defaults to 'application/x-www-form-urlencoded'.

Perhaps try setting the Content-Type with the contentType argument rather than inserting a Content-Type header manually?

〃安静 2024-11-25 02:07:02

下面的代码可以完美运行。

function ShortenUrl(){
var url = 'https://www.googleapis.com/urlshortener/v1/url';
var apiKey = 'AIzBlNS-3HZdxKgwj-x30';
url += '?key=' + apiKey;
var payload = {"longUrl":"www.google.com"};

var parameters = { method : 'post',
                payload:JSON.stringify(payload),
                contentType:'application/json',                    
                muteHttpExceptions:true};

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

The following code works perfectly.

function ShortenUrl(){
var url = 'https://www.googleapis.com/urlshortener/v1/url';
var apiKey = 'AIzBlNS-3HZdxKgwj-x30';
url += '?key=' + apiKey;
var payload = {"longUrl":"www.google.com"};

var parameters = { method : 'post',
                payload:JSON.stringify(payload),
                contentType:'application/json',                    
                muteHttpExceptions:true};

var response = UrlFetchApp.fetch(url, parameters);
Logger.log(response);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文