我有一个Google表格可以通过Twitter发送消息,我已经在Twitter中创建了我的应用程序,并使用了使用者和密钥,如果我没有回调URL进行设置,就会收到一条错误消息,告诉我与桌面应用程序不兼容。
如果我输入一个占位符回调URL,则会出现以下错误:
错误:启动OAuth流时出错:此客户端应用程序未批准回调URL。可以在您的应用程序设置中调整批准的回调URL
我应该使用什么作为回调URL?
这是代码:
function help_() {
var html = HtmlService.createHtmlOutputFromFile('help')
.setTitle("Google Scripts Support")
.setWidth(400)
.setHeight(200);
var ss = SpreadsheetApp.getActive();
ss.show(html);
}
function promo_() {
var html = HtmlService.createHtmlOutputFromFile('promo')
.setTitle("Twitter Merge Complete")
.setWidth(450)
.setHeight(260);
var ss = SpreadsheetApp.getActive();
ss.show(html);
}
function getTwitterService_() {
var consumer_key = "xxxxxxxxxxxxxxxxxxxx";
var consumer_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
return OAuth1.createService('twitter')
.setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
.setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
.setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
.setConsumerKey(consumer_key)
.setConsumerSecret(consumer_secret)
.setCallbackFunction('twitter')
.setPropertyStore(PropertiesService.getUserProperties());
}
function twitter(request) {
var twitterService = getTwitterService_();
var isAuthorized = twitterService.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput('<br><p style="text-align:center">Success! Your Twitter account is now authorized. Please close this window.</p>');
} else {
return HtmlService.createHtmlOutput('<br><p style="text-align:center">Ah! there was an error. Please contact Amit Agarwal amit@labnol.org for support (or tweet @labnol)</p>');
}
}
function clearService(){
OAuth1.createService('twitter')
.setPropertyStore(PropertiesService.getUserProperties())
.reset();
}
function encodeString_(q) {
var str = encodeURIComponent(q
.replace(/&(gt|lt|amp);/g, function(str, code) {
var lookup = {
gt: ">",
lt: "<",
amp: "&"
}
return lookup[code];
})).replace(/[()\[\]!*']/g, function(badchar) {
return "%" + badchar.charCodeAt(0).toString(16);
});
return str;
// return encodeURIComponent(q).replace(/\!/g, "%21").replace(/\*/g, "%2A").replace(/\'/g, "%27").replace(/\(/g, "%28").replace(/\)/g, "%29");
}
function doProperty_(key, value) {
var properties = PropertiesService.getUserProperties();
if (value) {
properties.setProperty(key, value);
} else {
return properties.getProperty(key) || "";
}
}
function onOpen() {
var menu, ss = SpreadsheetApp.getActiveSpreadsheet();
menu = [
{ name: "1. Authorize Twitter", functionName: "authTwitter_"},
{ name: "2. Configure Tweets", functionName: "configTwitter_"},
{ name: "3. Send Tweets", functionName: "sendTweets_"},
null,
{ name: "Help & Support", functionName: "help_"},
];
ss.addMenu(" Twitter Merge", menu);
}