Twitter oAuth callbackUrl - localhost development

2020-01-24 19:16发布

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://code.google.com/p/twitter-api/issues/detail?id=534#c1

Does anyone have a workaround. I don't really want to stop my development

标签: twitter oauth
17条回答
我命由我不由天
2楼-- · 2020-01-24 19:32

Just put http://127.0.0.1:xxxx/ as the callback url, where xxxx is the port for your framework

查看更多
戒情不戒烟
3楼-- · 2020-01-24 19:33

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.

查看更多
Juvenile、少年°
4楼-- · 2020-01-24 19:33

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.

查看更多
戒情不戒烟
5楼-- · 2020-01-24 19:34

These are the steps that worked for me to get Facebook working with a local application on my laptop:

  • goto apps.twitter.com
  • enter the name, app description and your site URL Note: for localhost:8000, use 127.0.0.1:8000 since the former will not work
  • enter the callback URL matching your callback URL defined in TWITTER_REDIRECT_URI your application Note: eg: http://127.0.0.1/login/twitter/callback (localhost will not work).
  • Important enter both the "privacy policy" and "terms of use" URLs if you wish to request the user's email address
  • check the agree to terms checkbox
  • click [Create Your Twitter Application]
  • switch to the [Keys and Access Tokens] tab at the top
  • copy the "Consumer Key (API Key)" and "Consumer Secret (API Secret)" to TWITTER_KEY and TWITTER_SECRET in your application
  • click the "Permissions" tab and set appropriately to "read only", "read and write" or "read, write and direct message" (use the least intrusive option needed for your application, for just and OAuth login "read only" is sufficient
  • Under "Additional Permissions" check the "request email addresses from users" checkbox if you wish for the user's email address to be returned to the OAuth login data (in most cases check yes)
查看更多
Animai°情兽
6楼-- · 2020-01-24 19:35

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; }
}
查看更多
女痞
7楼-- · 2020-01-24 19:40

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).

查看更多
登录 后发表回答