gpt4 book ai didi

twitter - 使用 Google Apps 脚本将推文发布到我的供稿

转载 作者:行者123 更新时间:2023-12-01 09:56:31 24 4
gpt4 key购买 nike

如标题所示,我的目标是能够从 script.gs 发送推文。推文将发布到我的订阅源中,理想情况下我无需访问 Twitter 网站。

我写了两个主要函数来尝试这个:

脚本.gs

//post tweet
function oAuth() {
var CONSUMER_KEY = "**********************";
var CONSUMER_SECRET = "*************************************************";
ScriptProperties.setProperty("TWITTER_CONSUMER_KEY", CONSUMER_KEY);
ScriptProperties.setProperty("TWITTER_CONSUMER_SECRET", CONSUMER_SECRET);
var oauthConfig = UrlFetchApp.addOAuthService("twitter");
oauthConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token");
oauthConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token");
oauthConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authorize");
oauthConfig.setConsumerKey(ScriptProperties.getProperty("TWITTER_CONSUMER_KEY"));
oauthConfig.setConsumerSecret(ScriptProperties.getProperty("TWITTER_CONSUMER_SECRET"));
var options = {muteHttpExceptions: true,oAuthServiceName:'twitter',oAuthUseToken:'always'}
var url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
var response = UrlFetchApp.fetch(url, options).getContentText();
Logger.log(response);
}
function postTweet() {
oAuth();
Logger.log('oAuth complete');
var status = "Tweet";
var Roptions = {
method: "post",
oAuthServiceName: "twitter",
oAuthUseToken: "always",
status: status
};
var url = "https://api.twitter.com/1.1/statuses/update.json";
Logger.log('begin post');
var request = UrlFetchApp.fetch(url, Roptions); //the trouble line. Execution stops.
Logger.log('post complete');
}

经过大约一天的不懈努力,我终于可以使用第一个函数 oAuth() 了。那记录了我的用户数据。然而,对于我来说,我无法弄清楚是什么阻碍了 request。我收到此错误:Request failed for returned code 403. Truncated server response: {"errors":[{"message":"SSL is required","code":92}]}。谷歌搜索并没有发现太多。我猜问题出在 Roptions 中的某个地方。如有任何帮助,我们将不胜感激,如果需要,我可以尝试提供进一步的说明。

最佳答案

Eureka !这是解决方案。具有讽刺意味的是,我以前也有过这样的事情,但已经不屑一顾了。原来 https 是我最大的问题。今晚我要吃不起眼的馅饼。

发送推文的脚本

//post tweet
function oAuth() {
var CONSUMER_KEY = "*************************";
var CONSUMER_SECRET = "**************************************************";
ScriptProperties.setProperty("TWITTER_CONSUMER_KEY", CONSUMER_KEY);
ScriptProperties.setProperty("TWITTER_CONSUMER_SECRET", CONSUMER_SECRET);
var oauthConfig = UrlFetchApp.addOAuthService("twitter");
oauthConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token");
oauthConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token");
oauthConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authenticate");
oauthConfig.setConsumerKey(ScriptProperties.getProperty("TWITTER_CONSUMER_KEY"));
oauthConfig.setConsumerSecret(ScriptProperties.getProperty("TWITTER_CONSUMER_SECRET"));
var options = {muteHttpExceptions: true,oAuthServiceName:'twitter',oAuthUseToken:'always'}
var url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
var response = UrlFetchApp.fetch(url, options).getContentText();
Logger.log(response);
}
function postTweet() {
oAuth();
Logger.log('oAuth complete');
var status='Test tweet';
var options = {
"method": "post",
"oAuthServiceName": "twitter",
"oAuthUseToken": "always",
"payload":{"status":status}
};
var url = "https://api.twitter.com/1.1/statuses/update.json";
Logger.log('begin post');
var request = UrlFetchApp.fetch(url, options);
Logger.log('post complete');
}

当您注册您的 Twitter 应用程序时,您必须选中选项Allow this application to be used to Sign in with Twitter。这可以防止持续的授权弹出窗口。此外,推文文本不能包含单引号 (')。

关于twitter - 使用 Google Apps 脚本将推文发布到我的供稿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25577921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com