【问题标题】:302 Found when running the Perl Script运行 Perl 脚本时发现 302
【发布时间】:2013-09-05 03:16:43
【问题描述】:
#!/usr/bin/perl -w

use WWW::Facebook::API;
use WWW::Facebook::API::Auth;
use WWW::Facebook::API::Canvas;
use HTTP::Request;
use LWP;

use CGI;                                # load CGI routines
$q = CGI->new;                          # create new CGI object
print $q->header,                       # create the HTTP header
  $q->start_html('Facebook App'),       # start the HTML
  $q->h1('Facebook Authentication'),    # level 1 header
  $q->end_html;                         # end the HTML

my $facebook_api      = '---------------';
my $facebook_secret   = '----------------------------';
my $facebook_clientid = '-----------------------------------';

my $client = WWW::Facebook::API->new(
    desktop     => 0,
    api_version => '1.0',
    api_key     => $facebook_api,
    secret      => $facebook_secret,

);

$client->app_id($facebook_clientid);
print $q->redirect( $client->get_login_url() );

在浏览器中显示为

Facebook Authentication
Status: 302 Found Location: http://www.facebook.com/login.php? api_key= -  ----------------&v=1.0 

如何解决这个 CGI 问题。我在 ubuntu apache 服务器上运行这个 perl 脚本。

【问题讨论】:

    标签: perl cgi


    【解决方案1】:

    您在此处打印您的 HTTP 标头:

    print $q->header
    

    然后在此处打印另一个 HTTP 标头:

    print $q->redirect($client->get_login_url());
    

    一条 HTTP 消息只能有一组标头。如果您要发送 302 重定向,则也无需打印任何 HTML,因此请摆脱所有这些:

    print $q->header,                         # create the HTTP header
    $q->start_html('Facebook App'),       # start the HTML
    $q->h1('Facebook Authentication'),    # level 1 header
    $q->end_html;                         # end the HTML
    

    【讨论】:

    • 但是当我在另一台机器上使用相同的代码时[配置低]它工作正常。这背后的原因是什么。
    • 谁知道?也许您有不同版本的模块或设置。每次有人说“我正在使用相同的代码......”时,我发现他们不是。 :)
    猜你喜欢
    • 2010-09-26
    • 1970-01-01
    • 2014-11-30
    • 2015-12-01
    • 2014-09-29
    • 2015-05-27
    • 2014-12-27
    • 2017-10-26
    • 2012-09-05
    相关资源
    最近更新 更多