我想使用一个API (url缩短器)与一个公共谷歌插件。目前我的代码返回:
超过未经验证使用的每日限制。继续使用需要注册。
- 这可能吗?
- 如果是,我需要一个认证令牌吗?
- 如果是,我应该选择什么类型的密钥?
- 我如何实施这类使用的授权?
- 我需要付费吗?
- 如果没有,其他插件如何使用外部api
谢谢你的回答,
编辑:OP在评论中指出这是一个自定义函数。自定义函数以有限的授权运行。完整的可用列表见:
https://developers.google.com/apps-script/guides/sheets/functions using_apps_script_services
使用REST API来获取缩短的url。这将适用于自定义函数。您只需要启用URL缩短器API并生成服务器API密钥。使用以下链接中的ip作为您的服务器api密钥:
https://developers.google.com/apps-script/guides/jdbc setup_for_google_cloud_sql
/**
* Returns a shortened URL of the input.
*
* @param {string} longUrl The long URL to shorten.
* @return The shortened url.
* @customfunction
*/
function getShortUrl(longUrl) {
var payLoad = {"longUrl": longUrl};
var apiKey = PropertiesService.getScriptProperties().getProperty("ServerApiKey");
var url = "https://www.googleapis.com/urlshortener/v1/url?key="+ apiKey;
var options = { method:"POST",
contentType:"application/json",
payload:JSON.stringify(payLoad),
muteHttpExceptions:true};
var response = UrlFetchApp.fetch(url, options);
if(response.getResponseCode() != 200){throw new Error("Unable to shorten url");}
return JSON.parse(response).id;
}
原创文章
这里是使用UrlShortener高级服务的快速入门。您需要在开发人员控制台中打开该服务并激活Url Shortener api。这将为您的附加组件提供每天1,000,000个请求的配额。
function myFunction() {
var url = UrlShortener.newUrl();
url.longUrl = "http://www.example.org";
var short = UrlShortener.Url.insert(url);
Logger.log(short);
//list all users shortened urls
Logger.log(UrlShortener.Url.list());
}