gpt4 book ai didi

perl - 无法使用twitter api上传图片

转载 作者:行者123 更新时间:2023-12-02 03:14:31 31 4
gpt4 key购买 nike

我正在使用 Net::Twitter( cpan link ) 提供的 Twitter API。我可以发布状态但无法上传图片。

Perl 代码:

use strict;
use warnings;
use MIME::Base64;

use Net::Twitter;
use Scalar::Util 'blessed';

# When no authentication is required:
#my $nt = Net::Twitter->new(legacy => 0);
my $consumer_key = "consumerkey";
my $consumer_secret = "consumersecret";
my $token="token";
my $token_secret="tokensecret";

# As of 13-Aug-2010, Twitter requires OAuth for authenticated requests
my $nt = Net::Twitter->new(
traits => [qw/API::RESTv1_1/],
consumer_key => $consumer_key,
consumer_secret => $consumer_secret,
access_token => $token,
access_token_secret => $token_secret,
);
eval {
#my $result = $nt->update({status=>'some message'}); ##this works fine
my $result = $nt->upload({media=>'/some/path/toimage.jpeg' ,media_data => encode_base64('/some/path/toimage.jpeg')}); #this line produces error
};

if ( my $err = $@ ) {
die $@ unless blessed $err && $err->isa('Net::Twitter::Error');

warn "HTTP Response Code: ", $err->code, "\n",
"HTTP Message......: ", $err->message, "\n",
"Twitter error.....: ", $err->error, "\n";
}

错误:

HTTP Response Code: 400

HTTP Message......: Bad Request

Twitter error.....: media type unrecognized.

最佳答案

从这个答案得到启发后here .

我在 perl 中做了同样的事情,比如:

my @filename = ('path/to/image.jepg');
my $result = $nt->update_with_media({status=>'hI',media=>\@filename});

这非常有效。虽然这里只有数组引用有效。

注意:根据文档和来自@simbabque 的回答 link

upload upload(media)

Parameters: media
Required: media
Upload images to twitter without posting them on the timeline

Returns: Image

我猜这只会上传但不会显示在时间轴上。所以我使用了 update_with_media 方法来发布图片。

关于perl - 无法使用twitter api上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37858881/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com