【问题标题】:Twitter4J with mod_proxy带有 mod_proxy 的 Twitter4J
【发布时间】:2018-05-07 22:44:23
【问题描述】:

我正在使用 twitter4j 发布来自只能与 Internet 代理联系的应用程序的推文。所以我用这个配置在代理上设置了一个 apache 虚拟主机:

ProxyPass           /rest/1.1/  https://api.twitter.com/1.1/
ProxyPassReverse    /rest/1.1/  https://api.twitter.com/1.1/

所以我的应用程序请求将是一个 POST 到 https://proxy.com/rest/1.1/update....,它将被代理到 Twitter Rest API (https://api.twitter.com/1.1/update...),到目前为止一切顺利。
为了遵守 auth-header,每个带有原始 twitter api url 的请求都会计算 header 中的 oauth 签名(否则身份验证将失败,因为主机 url 将是代理的)。 POST 请求本身成功,但抛出一个额外的错误:

{"errors":[{"code":220,"message":"Your credentials do not allow access to this resource."}]}

我不知道,还有什么可做的。顺便说一句:相同的应用程序代码通过直接连接到 api.twitter.com 成功

【问题讨论】:

    标签: twitter4j mod-proxy


    【解决方案1】:

    好吧,叫我白痴。正确的重写方法应该包括访问令牌(而不是 null):

    @Override
    public String getAuthorizationHeader(HttpRequest req) {
    
        if (logger.isDebugEnabled()) {
            logger.debug(req.getURL());
            logger.debug(req.getRequestHeaders().toString());
        }
    
        //replace proxy url with twitterapi only for signature!!
        String restBaseUrl = this.conf.getRestBaseURL();
        String twitterRestBaseUrl = this.twitterRestBaseUrl;
        String modUrl = req.getURL().replace(restBaseUrl, twitterRestBaseUrl);
    
        //generate Token from stored values in provided config
        AccessToken token = new AccessToken(conf.getOAuthAccessToken(), conf.getOAuthAccessTokenSecret());
    
        return super.generateAuthorizationHeader(req.getMethod().name(), modUrl, req.getParameters(), token);
    }
    

    还不确定是否需要更换代理网址,但现在可以了.....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 2012-08-29
      • 2012-10-12
      • 2017-03-27
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多