2009-08-20 57 views
1

我試圖使用Net::OAuth模塊來授權Yammer API,並且我有以下代碼片段,幾乎取自CPAN上的Synopsis。Perl Net :: OAuth問題

Expected a hash! at /Library/Perl/5.8.8/Net/OAuth/Message.pm line 241. 

。我在一個明顯的語法錯誤還是我將不得不看的OAuth模塊本身:

$Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A; 
my $q = new CGI; 

my $request = Net::OAuth->request("request token")->from_hash($q->Vars, 
       request_url => $self->_request_token_url, 
       request_method => $q->request_method, 
       consumer_secret => $self->consumer_private, 
      ); 

但是,如果我嘗試運行我的測試如下拋出一個錯誤?

回答

4

$q->Vars返回標量上下文散列引用,並在列表中的扁平哈希上下文。子例程參數創建列表上下文。因此,你應該這樣做:

my $request = Net::OAuth->request("request token")->from_hash(
     scalar $q->Vars, 
     request_url => $self->_request_token_url, 
     request_method => $q->request_method, 
     consumer_secret => $self->consumer_private, 
); 

感謝亞當貝萊爾的評論,讓我檢查這一點。

+0

我一開始也是這麼想的,但它不對。查看由ccheneson鏈接的源代碼,只有第一個參數有望成爲散列引用,其餘的從@_被拖入單獨的散列。 – 2009-08-20 12:06:18

+0

此外,這是散列構造函數中奇數個元素;) – 2009-08-20 12:07:08

+0

它不是奇數個元素,因爲$ q-> Vars在列表上下文中返回一個展開的散列。但這是解決方案的關鍵。看到我編輯的答案。 – 2009-08-20 12:19:03

2

Net::OAuth::Message

sub from_hash { 
    my $proto = shift; 
    my $class = ref $proto || $proto; 
    my $hash = shift; 
    if (ref $hash ne 'HASH') { 
     die 'Expected a hash!'; 
    } 
    my %api_params = @_; 

也許你可以確保$ Q->瓦爾返回一個哈希裁判

my $vars = $q->Vars; 
print ref($vars);