从谷歌应用程序脚本使用goo.gl API的问题 [英] Problems using the goo.gl API from google apps script

查看:163
本文介绍了从谷歌应用程序脚本使用goo.gl API的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Google Apps脚本中查询goo.gl API。我看到的问题是以下错误消息:

  https://www.googleapis.com/urlshortener申请失败/ v1 / url?key = AIXXXXXXXXXXXXXXXXXXXXXLmGJQw返回代码400.服务器响应:{error:{errors:[{domain:global,reason:parseError,message:This API不支持解析窗体编码输入。 }],code:400,message:此API不支持解析窗体编码输入。 }}(第28行)

当我尝试在 UrlFetchApp.fetch(post_url,options);



以下是我在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 = {$ b $'method':'post',
'headers':{
'Content-Type':'application / json'
},
'payload':有效载荷
};

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

var responseJson = response.getAs('json');

函数testMinifyGoogl(){
minifyGoogl('http://eduardo.cereto.net');
}


解决方案

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

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


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');
}

解决方案

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?

这篇关于从谷歌应用程序脚本使用goo.gl API的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆