0
votes

I am trying to create a Google Spreadsheet function to shorten URLs.

I have this code :

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

I have several Google login IDs for testing purposes. I experience the same problem repeating the above with two different ones.

In all cases I get back : "Access Not Configured. Please use Google Developers Console to activate the API for your project. (line 7, file "Code")"

As reference I am reading : https://developers.google.com/apps-script/advanced/url-shortener. After reading up on access requirements, I went to [Resources] >> [Advanced Google services] in my script editor and activated URL Shortener API. I followed the link from there to "Google Developers Console" and enabled the same API there.

Am I doing something wrong or is the service broken?

I do not think I require an access key. I am logged in to Google already, in order to use the spreadsheet! Also, when I run the script I do get a request for permission to access my short urls!

1
Are you setting this up on localhost? you still need to get authentication key in the Console and first gain access to your API in order to use it. - damien hawks
It is Google Apps Script running in a Google Spreadsheet. As stated, I have turned access on in both the script itself, and in the console. - Martin Bramwell
From what i remember you need to create a new project and then take the Secret Authentication key and userid and give the url for localhost as the place you'll be accessing the api from. Just turning the access on does not help. - damien hawks
I am accessing from a Google Spreadsheet. What URL do I provide? - Martin Bramwell
Did you find where you're supposed to add it? - damien hawks

1 Answers

1
votes

Url shortener is still working nicely, see app here(http://goo.gl/RqfaY6) (asks for authorization )

code below to illustrate if ever someone is interested :

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