使用 Google Apps 脚本中的 goo.gl API 时出现问题
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文档表示
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 thecontentType
argument rather than inserting aContent-Type
header manually?下面的代码可以完美运行。
The following code works perfectly.