我正在尝试创建一个Google电子表格功能来缩短网址。
我有这段代码:
function shortenUrl(_longurl) {
var url = UrlShortener.Url.insert({longUrl: _longurl});
Logger.log('Shortened URL is "%s".', url.id);
}
function testMinifyGoogl() {
longurl = 'https://maps.google.com/maps';
shortenUrl(longurl);
}
我有几个Google登录ID用于测试目的。我遇到了同样的问题,重复上述两个不同的问题。
在所有情况下,我都会回来:"访问未配置。请使用Google Developers Console激活项目的API。 (第7行,文件"代码")"
作为参考,我正在阅读:https://developers.google.com/apps-script/advanced/url-shortener。在阅读了访问要求后,我访问了[资源]>>我的脚本编辑器中的[高级Google服务]和激活的URL Shortener API。我按照那里的链接访问了#34; Google Developers Console"并在那里启用了相同的API。
我做错了什么还是服务坏了?
我认为我不需要访问密钥。我已登录Google,以便使用电子表格!此外,当我运行脚本时,我执行获取访问我的短网址的权限请求!
答案 0 :(得分:1)
网址缩短工具仍然运行良好,请参阅应用here(http://goo.gl/RqfaY6)(要求授权)
下面的代码说明是否有人感兴趣:
var User = new Object(),
Url = new Object();
User.email = Session.getActiveUser().getEmail();
function doGet() {
var app = UiApp.createApplication().setTitle('url_shortener');
var panel = app.createVerticalPanel().setStyleAttributes({'padding':'40px','backgroundColor':'#fafacc'});
var longUrlLabel = app.createLabel( 'Enter the long url starting with http:// you will receive an email with the short url immediately.' );
var longUrlBox = app.createTextBox().setName( 'longUrl' ).addClickHandler(app.createClientHandler().forEventSource().setText(''))
.setText( 'http://' ).setWidth('500');
var shortUrlLabel = app.createHTML().setId( 'shortUrlLabel' ).setVisible( false );
var handler = app.createServerHandler( 'buttonOnClickListener' ).addCallbackElement( panel );
var button = app.createButton( 'SUBMIT',handler ).setStyleAttributes({'border-radius':'5px'});
var grid = app.createGrid(8,1).setId('grid')
.setWidget(0,0,longUrlLabel )
.setWidget(2,0,longUrlBox )
.setWidget(4,0,button )
.setWidget(6,0,shortUrlLabel);
return app.add( panel.add(grid));
}
function buttonOnClickListener( eventInfo ) {
var app =UiApp.getActiveApplication();
var toShorten = UrlShortener.newUrl().setLongUrl(eventInfo.parameter.longUrl);
var shortened = UrlShortener.Url.insert(toShorten);
Url.short = UrlShortener.Url.insert(toShorten);
Url.long = eventInfo.parameter.longUrl;
sendMail();
app = UiApp.getActiveApplication();
app.getElementById( 'shortUrlLabel' ).setVisible(true).setHTML('<li>Short url = <b>'+Url.short.id+'</b></li><li>Mail sent ...</li>');
app.getElementById('grid').setWidget(7,0,app.createAnchor('test (with redirect warning)', Url.short.id));
return app;
}
function sendMail() {
GmailApp.sendEmail( User.email, "UrlShortener", 'Long url (original) = '+Url.long+"\n\n\nShort url = "+Url.short.id);
}
function GetShortUrlClicks() {
var analytics = UrlShortener.Url.get('http://goo.gl/UxlNQs',{projection:'FULL'}).getAnalytics();
var clicks = analytics.getWeek();
Logger.log(clicks);
}