使用谷歌应用程序脚本发布到Bitly api返回403



嗨,伙计们!

我一直在尝试使用bit.ly的api为我的谷歌表单编写一个自动化程序,以缩短我的大量链接。现在,我正处于基础阶段,试图记录api返回给我的内容。你们能帮我看看代码出了什么问题吗?我期待着200返回给我,但它一直返回403禁止我。

var form = 
{"long_url": "https://dev.bitly.com", "domain": "bit.ly", "group_guid": "MY GROUP ID" };  
var option =  {'header':'Authorization: Bearer{MY TOKEN}',
'method' : 'post',
'contentType': 'application/json', 
'payload' : JSON.stringify(form)
};
var response = UrlFetchApp.fetch('https://api-ssl.bitly.com/v4/shorten', option);
Logger.log (response);
}

p.S.我试图通过添加标题(成功(和自定义链接(bit.ly/后的短半部分//(来进一步扩展代码。第二部分继续返回我404。还是应该使用Post/custom_bitlinks?

这是我当前的代码:

function bitlyori (i, title){
var form = {
"group_guid": "MINE",  
"domain": "bit.ly",  
"long_url": i,
"title" : title
}; 
const MY_TOKEN = "MINE";
const option = {
headers: { Authorization: `Bearer ${MY_TOKEN}` },
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(form),
};
var result = UrlFetchApp.fetch('https://api-ssl.bitly.com/v4/bitlinks', option);
return (JSON.parse(result.getContentText()));
}
function bitly(url,title,custom) {
var temp = bitlyori(url, title);
var form_2 = { 
"custom_bitlinks": [temp] ,
};
const MY_TOKEN = "MINE";
const option_2 = {
headers: { Authorization: `Bearer ${MY_TOKEN}` },
method: 'patch',
payload: form_2}; 
var temp_link = 'https://api-ssl.bitly.com/v4/bitlinks/'+ JSON.stringify(temp)["id"];
var result_2 = UrlFetchApp.fetch(temp_link, option_2);
return (JSON.parse(result_2.getContentText()));
}

Headers应该是一个在options:内具有关键字"headers"的对象

const MY_TOKEN = "dfjkgsa";
const option = {
headers: { Authorization: `Bearer ${MY_TOKEN}` },
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(form),
};

请参阅:

  • Urlfetchapp§高级参数

相关内容

  • 没有找到相关文章

最新更新