How to provide UA attributes for Image::Grab?

51 Views Asked by At

I try to add some UA attributes while using Image::Grab, like:

my $fp = 'sha256$26328...';
my $pic = Image::Grab->new( ua => { ssl_opts => { SSL_fingerprint => $fp } } );

I tried also as:

$pic->ua->ssl_opts( { SSL_fingerprint => $fp } ); 

Neither way UA (LWP) seems not get set properly. How to achieve the SSL_fingerprint setting? I need it because site's certificate has some chain flaw in and without specifying the fingerprint I can't connect it securely.

2

There are 2 best solutions below

0
Dada On BEST ANSWER

To set SSL options, you need to pass 2 arguments to the ssl_opts method of LWP::UserAgent: the name of the option and its value. Thus, you should do:

$pic->ua->ssl_opts( SSL_fingerprint => $fp ); 

In your code, you are passing a hash reference to ssl_opts ({ SSL_fingerprint => $fp }). When a single argument is passed to ssl_opts, this argument is expected to be the name of an option, and the function returns the value associated to this name.

0
Dave Cross On

Your code $pic->ua will return the UA object that is being used inside the Image::Grab object. So calling methods on that should do the right thing.

Having said that, the documentation for ssl_opts() contains these examples:

my @keys = $ua->ssl_opts;
my $val = $ua->ssl_opts( $key );
$ua->ssl_opts( $key => $value );

So it looks like you need to remove the anonymous hash constructor from your code:

$pic->ua->ssl_opts( SSL_fingerprint => $fp ); 

Alternatively, you could try creating your own UA object:

my $ua = Image::Grab::RequestAgent->new(...);

And then inserting that into the Image::Grab object:

$pic->ua($ua);

Image::Grab::RequestAgent is a subclass of LWP::UserAgent and therefore its constructor will accept all of the same arguments.