Twitter oAuth callbackUrl - localhost development

后端 未结 17 1328
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 10:21

Is anyone else having a difficult time getting Twitters oAuth\'s callback URL to hit their localhost development environment. Apparently it has been disabled recently. http:

相关标签:
17条回答
  • 2020-11-27 11:00

    It can be done very conveniently with Fiddler:

    • Open menu Tools > HOSTS...
    • Insert a line like 127.0.0.1 your-production-domain.com, make sure that "Enable remapping of requests..." is checked. Don't forget to press Save.
    • If access to your real production server is needed, simply exit Fiddler or disable remapping.
    • Starting Fiddler again will turn on remapping (if it is checked).

    A pleasant bonus is that you can specify a custom port, like this: 127.0.0.1:3000 your-production-domain.com (it would be impossible to achieve this via the hosts file). Also, instead of IP you can use any domain name (e.g., localhost).

    This way, it is possible (but not necessary) to register your Twitter app only once (provided that you don't mind using the same keys for local development and production).

    0 讨论(0)
  • 2020-11-27 11:00

    edit this function on TwitterAPIExchange.php at line #180

    public function performRequest($return = true)
    {
        if (!is_bool($return)) 
        { 
            throw new Exception('performRequest parameter must be true or false'); 
        }
    
        $header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');
    
        $getfield = $this->getGetfield();
        $postfields = $this->getPostfields();
    
        $options = array( 
            CURLOPT_HTTPHEADER => $header,
            CURLOPT_HEADER => false,
            CURLOPT_URL => $this->url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false
        );
    
        if (!is_null($postfields))
        {
            $options[CURLOPT_POSTFIELDS] = $postfields;
        }
        else
        {
            if ($getfield !== '')
            {
                $options[CURLOPT_URL] .= $getfield;
            }
        }
    
        $feed = curl_init();
        curl_setopt_array($feed, $options);
        $json = curl_exec($feed);
        curl_close($feed);
    
        if ($return) { return $json; }
    }
    
    0 讨论(0)
  • 2020-11-27 11:01

    Looks like Twitter now allows localhost alongside whatever you have in the Callback URL settings, so long as there is a value there.

    0 讨论(0)
  • 2020-11-27 11:03

    I struggled with this and followed a dozen solutions, in the end all I had to do to work with any ssl apis on local host was:

    Go download: cacert.pem file

    In php.ini * un-comment and change: curl.cainfo = "c:/wamp/bin/php/php5.5.12/cacert.pem"

    • You can find where your php.ini file is on your machine by running php --ini in your CLI
    • I placed my cacert.pem in the same directory as php.ini for ease.
    0 讨论(0)
  • 2020-11-27 11:05

    I had the same challenge and I was not able to give localhost as a valid callback URL. So I created a simple domain to help us developers out: https://tolocalhost.com

    It will redirect any path to your localhost domain and port you need. Hope it can be of use to other developers.

    0 讨论(0)
  • 2020-11-27 11:06

    Alternative 1.

    Set up your .hosts (Windows) or etc/hosts file to point a live domain to your localhost IP. such as:

    127.0.0.1 xyz.com

    where xyz.com is your real domain.

    Alternative 2.

    Also, the article gives the tip to alternatively use a URL shortener service. Shorten your local URL and provide the result as callback.

    Alternative 3.

    Furthermore, it seems that it works to provide for example http://127.0.0.1:8080 as callback to Twitter, instead of http://localhost:8080.

    0 讨论(0)
提交回复
热议问题