Twitter Can';t从谷歌应用程序脚本发送推特帖子

Twitter Can';t从谷歌应用程序脚本发送推特帖子,twitter,oauth,google-apps-script,Twitter,Oauth,Google Apps Script,我不熟悉谷歌应用程序脚本,我正试图从我的脚本在twitter上发布,但每次我运行脚本时都会出现这个错误 您无权访问脚本使用的库OAuth1,或者它已被删除 我在库中添加了TwtrService,并选择了最新版本15。这是我的密码 function main() { var TWITTER_CONSUMER_KEY = "MY_KEY"; var TWITTER_CONSUMER_SECRET = "MY_SECRET";

我不熟悉
谷歌应用程序脚本
,我正试图从我的脚本在twitter上发布,但每次我运行脚本时都会出现这个错误

您无权访问脚本使用的库OAuth1,或者它已被删除

我在库中添加了
TwtrService
,并选择了最新版本
15
。这是我的密码

    function main()
    {
        var TWITTER_CONSUMER_KEY     = "MY_KEY";
        var TWITTER_CONSUMER_SECRET  = "MY_SECRET";

        ScriptProperties.setProperty("TWITTER_CONSUMER_KEY",    TWITTER_CONSUMER_KEY);
        ScriptProperties.setProperty("TWITTER_CONSUMER_SECRET", TWITTER_CONSUMER_SECRET);
        login();
        tweet("Hi there from Google Apps Script");
    }


    function login()
    {
        var loginConfig = UrlFetchApp.addOAuthService("twitter");
        loginConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token");
        loginConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token");
        loginConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authorize");
        loginConfig.setConsumerKey(ScriptProperties.getProperty("TWITTER_CONSUMER_KEY"));
        loginConfig.setConsumerSecret(ScriptProperties.getProperty("TWITTER_CONSUMER_SECRET"));
    }

    function tweet(text)
    {
        var params =
        {
            "method": "POST",
            "oAuthServiceName":"twitter",
            "oAuthUseToken":"always"        
        };
        var status =  "https://api.twitter.com/1.1/statuses/update.json";
        status = status + "?status=" + text;
        try
        {
            var success = UrlFetchApp.fetch(status, params);
            Logger.log(success.getContentText());
        }
        catch(ex)
        {
            Logger.log(ex.toString()); 
        }
    }

如何解决这个问题?如何访问图书馆OAuth1?非常需要这个帮助!谢谢。

转到谷歌脚本编辑器,按照以下说明操作:()

  • 单击菜单项“资源>库…”
  • 在“查找库”文本框中,输入项目密钥并单击“选择”按钮。 Mb2Vpd5nfD3Pz-_a-39Q4VfxhMjh3Sh48
  • 在下拉框中选择一个版本(通常最好选择 最新版本)。单击“保存”按钮

  • 应用程序脚本不再支持OAuth的“oAuthServiceName”方法。新方法有一个不同的API,它涉及到创建一个新的OAuth1对象(由中引用的库提供),并使用它来获取,而不是直接调用
    UrlFetchApp.fetch

    function login() {
      return OAuth1.createService("twitter")
        .setPropertyStore(PropertiesService.getScriptProperties())
        .setAccessTokenUrl("https://api.twitter.com/oauth/access_token")
        .setRequestTokenUrl("https://api.twitter.com/oauth/request_token")
        .setAuthorizationUrl("https://api.twitter.com/oauth/authorize")
        .setConsumerKey(ScriptProperties.getProperty("TWITTER_CONSUMER_KEY"))
        .setConsumerSecret(ScriptProperties.getProperty("TWITTER_CONSUMER_SECRET"));
    }
    
    function tweet(text) {
      var oauth = login();
      var status =  "https://api.twitter.com/1.1/statuses/update.json";
      oauth.fetch(status + "?status=" + text, {method : "POST"});
    }
    
    但是,我建议您使用我的Twitter lib for Apps脚本,因为它可以为您设置正确的端点和查询。它构建在OAuth1上,因此,尽管您仍然可以直接访问
    fetch()
    ,但您只需将所有配置转储到ScriptProperties中,调用
    new Twitterlib.OAuth即可(PropertiesService.getScriptProperties()).sendTweet(text);
    ,安装将为您完成

    Twitter库的关键是mkvhyydya4g5jhj7hxicoh8v4ox7x1m_