Is there a way to enable advanced google services in the Google Developers Console by script?

喜欢而已 提交于 2019-12-24 14:43:13

问题


again!
I created an embedded in spreadsheet script that use UrlShortener.Url.insert feature. My client wants to be able to make a new instances of this spreadsheet to share it with colleagues. I've implemented this feature, but when I begin to test new instance it's turned out that I have to enable URL Shortener API in my Google Developers Console.
I wonder if I can bypass this manual work using my script or only I can do is to provide client with instructions how to do it?

Update: Sandy Good recomends to use UrlFetch.fetch() to get the short link, but this code:

function test_short_link() {
  var options =
     {
       'longUrl': 'http://www.google.com/',
       'muteHttpExceptions': true
     };

   var result = UrlFetchApp.fetch("https://www.googleapis.com/urlshortener/v1/url",
       options);
   Logger.log(result);
}

returns this:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Required parameter: shortUrl",
    "locationType": "parameter",
    "location": "shortUrl"
   }
  ],
  "code": 400,
  "message": "Required parameter: shortUrl"
 }
}

Looks like this topic

And this code

function test_short_link() {
  var options =
     {
       'longUrl': 'http://www.google.com/',
       'muteHttpExceptions': true,
       'method':'post'

     };
   var result = UrlFetchApp.fetch("https://www.googleapis.com/urlshortener/v1/url",
       options);
   Logger.log(result);
}

bring us:

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "userRateLimitExceededUnreg",
    "message": "User Rate Limit Exceeded. Please sign up",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "User Rate Limit Exceeded. Please sign up"
 }
}

回答1:


Edit: No there is no way to access the Google Dev console through any method beside the website itself.

Here is the method to do this by UrlFetchApp. You need to pass the options in payload parameter, not in the UrlFetchApp options object. You also need to pass the current users OAuth token in the header. Of course you will need to modify this code as it hard codes the longUrl and does no error checking.

function ShortenUrl(){
var url = "https://www.googleapis.com/urlshortener/v1/url"

var payload = {"longUrl":"www.google.com"};

var parameters = { method : 'post',
                    headers : {'Authorization': 'Bearer '+ScriptApp.getOAuthToken()},
                    payload:JSON.stringify(payload),
                    contentType:'application/json',                    
                    muteHttpExceptions:true};

  var response = UrlFetchApp.fetch(url, parameters);
  Logger.log(response);

}



回答2:


This can be done perfectly. Try this code:

function ShortenUrl(){
var url = 'https://www.googleapis.com/urlshortener/v1/url';
var apiKey = 'AIzBlNS-3HZdxKgwj-x30';
url += '?key=' + apiKey;
var payload = {"longUrl":"www.google.com"};

var parameters = { method : 'post',
                payload:JSON.stringify(payload),
                contentType:'application/json',                    
                muteHttpExceptions:true};

var response = UrlFetchApp.fetch(url, parameters);
Logger.log(response);
}


来源:https://stackoverflow.com/questions/28854403/is-there-a-way-to-enable-advanced-google-services-in-the-google-developers-conso

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!