【问题标题】:Is there any way to overwrite CONSUMER_KEY and CONSUMER_SECRET有没有办法覆盖 CONSUMER_KEY 和 CONSUMER_SECRET
【发布时间】:2015-12-08 10:35:52
【问题描述】:

我正在使用 laravel 和 thujohn/twitter 包。 但我希望每当注册任何使用时,他们都会向我们提供 CONSUMER_KEY 和 CONSUMER_SECRET,我们将使用这些详细信息发布推文、最喜欢的推文等。

但在 thujohn/twitter 包中,CONSUMER_KEY 和 CONSUMER_SECRET 被设置一次,这将用于所有用户,我想使用每个注册用户将使用他们自己的消费者详细信息。

任何人都知道相同的任何解决方案

【问题讨论】:

    标签: php api laravel twitter


    【解决方案1】:

    看源码你有reconfigure方法:

    /**
     * Set new config values for the OAuth class like different tokens.
     *
     * @param Array $config An array containing the values that should be overwritten.
     *
     * @return void
     */
    public function reconfig($config)
    {
        // The consumer key and secret must always be included when reconfiguring
        $config = array_merge($this->parent_config, $config);
        parent::reconfigure($config);
        return $this;
    }
    

    所以你可以传递一个带有你想要的配置的数组:

    Twitter::reconfigure([
        'consumer_key'               => '',
        'consumer_secret'            => '',
        'token'                      => '',
        'secret'                     => '',
    ]);
    

    然后,此配置将传递给父级,这是另一个名为 tmhOAuth 的库,代码如下:

    public function reconfigure($config=array()) {
        // default configuration options
        $this->config = array_merge(
            array(
                // leave 'user_agent' blank for default, otherwise set this to
                // something that clearly identifies your app
                'user_agent'                 => '',
                'host'                       => 'api.twitter.com',
                'method'                     => 'GET',
                'consumer_key'               => '',
                'consumer_secret'            => '',
                'token'                      => '',
                'secret'                     => '',
                // OAuth2 bearer token. This should already be URL encoded
                'bearer'                     => '',
                // oauth signing variables that are not dynamic
                'oauth_version'              => '1.0',
                'oauth_signature_method'     => 'HMAC-SHA1',
                // you probably don't want to change any of these curl values
                'curl_http_version'          => CURL_HTTP_VERSION_1_1,
                'curl_connecttimeout'        => 30,
                'curl_timeout'               => 10,
                // for security this should always be set to 2.
                'curl_ssl_verifyhost'        => 2,
                // for security this should always be set to true.
                'curl_ssl_verifypeer'        => true,
                // for security this should always be set to true.
                'use_ssl'                    => true,
                // you can get the latest cacert.pem from here http://curl.haxx.se/ca/cacert.pem
                // if you're getting HTTP 0 responses, check cacert.pem exists and is readable
                // without it curl won't be able to create an SSL connection
                'curl_cainfo'                => __DIR__ . DIRECTORY_SEPARATOR . 'cacert.pem',
                'curl_capath'                => __DIR__,
                // in some cases (very very odd ones) the SSL version must be set manually.
                // unless you know why your are changing this, you should leave it as false
                // to allow PHP to determine the value for this setting itself.
                'curl_sslversion'            => false,
                'curl_followlocation'        => false, // whether to follow redirects or not
                // support for proxy servers
                'curl_proxy'                 => false, // really you don't want to use this if you are using streaming
                'curl_proxyuserpwd'          => false, // format username:password for proxy, if required
                'curl_encoding'              => '',    // leave blank for all supported formats, else use gzip, deflate, identity etc
                // streaming API configuration
                'is_streaming'               => false,
                'streaming_eol'              => "\r\n",
                'streaming_metrics_interval' => 10,
                // header or querystring. You should always use header!
                // this is just to help me debug other developers implementations
                'as_header'                  => true,
                'force_nonce'                => false, // used for checking signatures. leave as false for auto
                'force_timestamp'            => false, // used for checking signatures. leave as false for auto
            ),
            $config
        );
    }
    

    【讨论】:

    • 抱歉,我正在尝试您的代码并传递 consumer_key 和 consumer_secret 但我收到错误“[220] 您的凭据不允许访问此资源。”同时我检查应用程序的权限,但设置正确
    • 看来是在设置consumer_key 和consumer_secret,你确定你为consumer_key、c​​onsumer、token 和secret 设置了正确的值吗?尝试激活 twitter 调试以检查日志github.com/thujohn/twitter#debug
    • 你的意思是说我们必须提供提供他的consumer_key,consumer_secret的同一用户的令牌和秘密
    • 是的,除非您的应用中有某些内容,否则您会请求用户授予您应用的权限
    • Fabio 你能告诉我有没有可能的方法来获取注册用户令牌和秘密(谁提供我 consumer_key 和 ,consumer_secret )基于我自己的 consumer_key 和 ,consumer_secret, token,consumer_key 和消费者秘密,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多