有没有方法通过脚本在Google Developers Console中启用高级Google服务? [英] Is there a way to enable advanced google services in the Google Developers Console by script?

查看:107
本文介绍了有没有方法通过脚本在Google Developers Console中启用高级Google服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次!

我创建了一个使用UrlShortener.Url.insert功能的嵌入电子表格脚本。我的客户希望能够将此电子表格的新实例与同事分享。我已经实现了此功能,但是当我开始测试新实例时,事实证明我必须在Google Developers Console中启用URL Shortener API。


我不知道是否可以用我的脚本绕过这个手动工作,或者我可以做的就是向客户提供如何操作的指导?

更新:
Sandy Good推荐使用UrlFetch.fetch()获取短链接,但是这段代码:

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

$ / code>

返回:

< pre $ {
error:{
errors:[
{
domain:global,
reason:required,
message:必需参数:shortUrl,
locationType:参数,
location:shortUrl
}
],
code:400,
message:必需参数:shortUrl
}
}

看起来像这样 topic



此代码

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

带给我们:

< pre $ {
error:{
errors:[
{
domain:usageLimits,
原因:userRateLimitExceededUnreg,
消息:用户比率超出限制。请注册,
extendedHelp:https://code.google.com/apis/console


code:403,
message:超出用户费率限制,请注册
}
}


解决方案

编辑:没有办法访问Google通过网站旁边的任何方法开发控制台。



以下是UrlFetchApp执行此操作的方法。您需要将选项传递到有效内容参数中,而不是UrlFetchApp选项对象中。您还需要在标头中传递当前用户的OAuth令牌。当然你需要修改这段代码,因为它对longUrl进行了硬编码,并且没有进行错误检查。

 函数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有效载荷),
contentType:'application / json',
muteHttpExceptions:true};

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

}


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"
 }
}

解决方案

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

}

这篇关于有没有方法通过脚本在Google Developers Console中启用高级Google服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆