Hi,
I installed the MediaWiki::API (version 0.30) module from CPAN onto my linux workstation. The wiki site that I wish to connect and modify is running WikiMedia over https and I cannot get this simple login script to work:
#!/usr/bin/perl use strict; use warnings; use MediaWiki::API;
my $mw = MediaWiki::API->new();
sub on_error() { print "Error code: " . $mw->{error}->{code} . "\n"; print $mw->{error}->{stacktrace}."\n"; die; }
$mw->{config}->{api_url} = 'https://wiki.eng.somecompany.com/index.php'; $mw->{config}->{on_error} = &on_error;
# log in to the wiki $mw->login( { lgname => 'myuser', lgpassword => 'mypass'} ); use Data::Dumper; print Dumper($mw);
# perl wiki.pl Error code: 2 401 Authorization Required : error occurred when accessing https://wiki.eng.somecompany.com/index.php after 1 attempt(s) at /usr/local/lib/perl5/site_perl/5.10.0/MediaWiki/API.pm line 354 MediaWiki::API::api('MediaWiki::API=HASH(0xce6e88)', 'HASH(0xf64ad0)') called at /usr/local/lib/perl5/site_perl/5.10.0/MediaWiki/API.pm line 218 MediaWiki::API::login('MediaWiki::API=HASH(0xce6e88)', 'HASH(0xf64ad0)') called at wiki.pl line 19
Died at wiki.pl line 12.
From the documentation, error code 2 stands for ERR_HTTP and I believe
this is due to attempting a connection over https.
Has anyone on the list had success with using the API (with perl/python/java etc..) to login to wikis running https? Any pointers to examples will be greatly helpful.
Thanks, ravi
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Sat, Jun 5, 2010 at 11:53 PM, Ravi Parimi rparimi@gmail.com wrote:
$mw->{config}->{api_url} = 'https://wiki.eng.somecompany.com/index.php';
You should probably configure this to an "api.php", not an "index.php".
401 Authorization Required : error occurred when accessing https://wiki.eng.somecompany.com/index.php after 1 attempt(s) at
Try to open that URL in a web browser. Does it ask for a user name and password?
- - -- Jimmy Xu
On Sat, Jun 5, 2010 at 9:00 AM, Jimmy Xu xu.jimmy.wrk@gmail.com wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Sat, Jun 5, 2010 at 11:53 PM, Ravi Parimi rparimi@gmail.com wrote:
$mw->{config}->{api_url} = 'https://wiki.eng.somecompany.com/index.php';
You should probably configure this to an "api.php", not an "index.php".
401 Authorization Required : error occurred when accessing https://wiki.eng.somecompany.com/index.php after 1 attempt(s) at
Try to open that URL in a web browser. Does it ask for a user name and password?
Yes, it indeed asks me for a login/password. I replaced the actual company name with the "companyname" string, but thought that was too obvious to state.
Thanks
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Sun, Jun 6, 2010 at 12:04 AM, Ravi Parimi rparimi@gmail.com wrote:
Yes, it indeed asks me for a login/password. I replaced the actual company name with the "companyname" string, but thought that was too obvious to state.
Indeed MediaWiki::API doesn't seems to support basic auth. Just try to use "https://user:pass@example.com/w/api.php" as your api_url. If this doesn't work, you may have to hack MediaWiki::API to honor the $ua->credentials setting in LWP::UserAgent.
- -- Jimmy Xu
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10-06-05 01:14 PM, Jimmy Xu wrote:
Indeed MediaWiki::API doesn't seems to support basic auth. Just try to use "https://user:pass@example.com/w/api.php" as your api_url. If this doesn't work, you may have to hack MediaWiki::API to honor the $ua->credentials setting in LWP::UserAgent.
Missed this before... emails arriving out of order O_o
MediaWiki::Bot doesn't support basic auth either. We can probably do that in the future though. You can follow issue 68: http://code.google.com/p/perlwikipedia/issues/detail?id=68
There might be non-easy ways to accomplish it, I'm not sure.
- -Mike
Indeed MediaWiki::API doesn't seems to support basic auth. Just try to use "https://user:pass@example.com/w/api.php" as your api_url. If this doesn't work, you may have to hack MediaWiki::API to honor the $ua->credentials setting in LWP::UserAgent.
it should support http auth - you do as you have just said here. Should work fine. If not, please open a bug about it. You can also modify the $ua object as needed after creating an instance of MediaWiki::API.
Best Regards
Jools
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10-06-07 02:05 PM, Jools Wills wrote:
Indeed MediaWiki::API doesn't seems to support basic auth. Just try to use "https://user:pass@example.com/w/api.php" as your api_url. If this doesn't work, you may have to hack MediaWiki::API to honor the $ua->credentials setting in LWP::UserAgent.
it should support http auth - you do as you have just said here. Should work fine. If not, please open a bug about it. You can also modify the $ua object as needed after creating an instance of MediaWiki::API.
I also added easy-to-use support to MediaWiki::Bot in r303: http://code.google.com/p/perlwikipedia/source/detail?r=303
- -Mike
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10-06-05 12:53 PM, Ravi Parimi wrote:
I installed the MediaWiki::API (version 0.30) module from CPAN
MediaWiki::Bot provides another layer of convenience, you might want to use that. It'll handle logging in for you automatically, for example.
You can use 2.3.1, but note that the 3.0 release of MediaWiki::Bot is about to come out in just a few days, and it introduces breaking changes.
I'll be making an actual announcement on a few mailing lists, including this one, when it becomes available in CPAN.
- -Mike
On Sat, Jun 5, 2010 at 9:07 AM, Mike.lifeguard mike.lifeguard@gmail.com wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10-06-05 12:53 PM, Ravi Parimi wrote:
I installed the MediaWiki::API (version 0.30) module from CPAN
MediaWiki::Bot provides another layer of convenience, you might want to use that. It'll handle logging in for you automatically, for example.
You can use 2.3.1, but note that the 3.0 release of MediaWiki::Bot is about to come out in just a few days, and it introduces breaking changes.
I'll be making an actual announcement on a few mailing lists, including this one, when it becomes available in CPAN.
Mike,
Thanks much for the pointer to MediaWiki::Bot. I came up with the following script but it seems to hang right during $bot->login():
# cat -n bot.pl
1 #!/usr/bin/perl 2 use strict; 3 use warnings; 4 use MediaWiki::Bot; 5 my $bot = MediaWiki::Bot->new( 6 useragent => 'MediaWiki::Bot 3.0.0 (User:Mike.lifeguard)', 7 assert => 'bot', 8 protocol => 'https', 9 host => 'wiki.eng.xxx.com', 10 path => 'RRDevTools', 11 ); 12 print "bot created\n"; 13 14 $bot->login( 15 { 16 # Username/password masked out 17 username => 'user', 18 password => 'passwd', 19 }, 20 ) or die "Login failed"; 21 22 print "Logged in!\n"; 23 use Data::Dumper; 24 print Dumper($bot);
# perl bot.pl bot created Error code: 2 at bot.pl line 14 500 Can't connect to https:80 (Bad hostname 'https') : error occurred when accessing http://https/host/api.php after 6 attempt(s) at bot.pl line 14
I tried setting usagent to 'MediaWiki::Bot 2.3.1 (User:Mike.lifeguard)' as well, but it didn't help. Can you spot something wrong with the above snippet? If you have any scripts that login over https, can you please point them to me?
thanks, ravi
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10-06-05 02:05 PM, Ravi Parimi wrote:
8 protocol => 'https', 9 host => 'wiki.eng.xxx.com',
This is where you're going wrong. You can see from the carp
Error code: 2 at bot.pl line 14 500 Can't connect to https:80 (Bad hostname 'https') : error occurred when accessing http://https/host/api.php after 6 attempt(s) at bot.pl line 14
that it is trying to contact "http://https/host/api.php" which is... not valid. At all. You have to set protocol, host and path properly.
I can't tell what mistake you made without the actual code and output.
I tried setting usagent to 'MediaWiki::Bot 2.3.1 (User:Mike.lifeguard)' as well, but it didn't help.
The useragent is totally unrelated, and you don't need to set it at all. As evidenced by the above, the default is probably better than what most people will set it to.
And I have to ask... you have MediaWiki::Bot 3.0? You checked it out from SVN yourself? Because the code you've copied is written for 3.0... if you don't have 3.0 then you're likely going to have issues.
- -Mike
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Sun, Jun 6, 2010 at 1:13 AM, Mike.lifeguard mike.lifeguard@gmail.com wrote:
And I have to ask... you have MediaWiki::Bot 3.0? You checked it out from SVN yourself? Because the code you've copied is written for 3.0... if you don't have 3.0 then you're likely going to have issues.
IMO MediaWiki::Bot 3.0 is avaliable on CPAN: http://search.cpan.org/~dcollins/MediaWiki-Bot-3.0.0/lib/MediaWiki/Bot.pm
- -- Jimmy Xu
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10-06-05 02:23 PM, Jimmy Xu wrote:
IMO MediaWiki::Bot 3.0 is avaliable on CPAN: http://search.cpan.org/~dcollins/MediaWiki-Bot-3.0.0/lib/MediaWiki/Bot.pm
Well, it wasn't finished until this morning, so O___o
It's been usable for a while, so feel free to use that. I suppose that means it is the 3.1 release that's coming out in a few days :P
- -Mike
8 protocol => 'https', 9 host => 'wiki.eng.xxx.com',
This is where you're going wrong. You can see from the carp
Error code: 2 at bot.pl line 14 500 Can't connect to https:80 (Bad hostname 'https') : error occurred when accessing http://https/host/api.php after 6 attempt(s) at bot.pl line 14
that it is trying to contact "http://https/host/api.php" which is... not valid. At all. You have to set protocol, host and path properly.
I was puzzled when the script ended up connecting to http://https/host/api.php. My host, protocol and path seem to be correct. So, if I intend to connect to https://wiki.eng.xx.com/foo/bar is this how I should assign my variables?
my $bot = MediaWiki::Bot->new( useragent => 'MediaWiki::Bot 2.3.1 (User:Mike.lifeguard)', assert => 'bot', protocol => 'https', host => 'wiki.eng.xxx.com', path => 'foo/bar', );
$bot->set_wiki( protocol => 'https', host => 'wiki.eng.xxx.com', path => 'foo/bar', );
$bot->login( { username => 'user', password => 'passwd', }, ) or die "Login failed";
Since the host, path and protocol were all initialized when when initializing $bot, is it necessary to call $bot->set_wiki() subsequently? That step sounds redundant to me..
I can't tell what mistake you made without the actual code and output.
The code I pasted earlier is exactly what I used (with the exception of replacing the username/password and xx with valid strings).
I tried setting usagent to 'MediaWiki::Bot 2.3.1 (User:Mike.lifeguard)' as well, but it didn't help.
The useragent is totally unrelated, and you don't need to set it at all. As evidenced by the above, the default is probably better than what most people will set it to.
And I have to ask... you have MediaWiki::Bot 3.0? You checked it out from SVN yourself? Because the code you've copied is written for 3.0... if you don't have 3.0 then you're likely going to have issues.
As Jimmy pointed out, I download whatever was available on CPAN and it turned out to be version 3.0.
thanks, ravi
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10-06-05 02:35 PM, Ravi Parimi wrote:
my $bot = MediaWiki::Bot->new( useragent => 'MediaWiki::Bot 2.3.1 (User:Mike.lifeguard)', assert => 'bot', protocol => 'https', host => 'wiki.eng.xxx.com', path => 'foo/bar', );
I missed it before - you haven't passed a hashref. Should be:
my $bot = MediaWiki::Bot->new({ protocol => 'https', host => 'wiki.eng.xxx.com', path => 'foo/bar', });
This is documented incorrectly in some places right now, another reason this shouldn't have been sent to CPAN yet. ARGH!
Since the host, path and protocol were all initialized when when initializing $bot, is it necessary to call $bot->set_wiki() subsequently? That step sounds redundant to me..
The constructor in 3.0 automatically logs you in and does some automatic configuration unless you tell it not to. You needn't call either set_wiki() or login().
- -Mike
my $bot = MediaWiki::Bot->new( useragent => 'MediaWiki::Bot 2.3.1 (User:Mike.lifeguard)', assert => 'bot', protocol => 'https', host => 'wiki.eng.xxx.com', path => 'foo/bar', );
I missed it before - you haven't passed a hashref. Should be:
my $bot = MediaWiki::Bot->new({ protocol => 'https', host => 'wiki.eng.xxx.com', path => 'foo/bar', });
This is documented incorrectly in some places right now, another reason this shouldn't have been sent to CPAN yet. ARGH!
Hi Mike,
I have modified the constructor to accept a hashref and it returns instantly (like it did earlier). Is there a way to determine if this operation succeeded? Using Data::Dumper, I dumped out the contents of $bot but nothing in the output indicated that the login succeeded or failed. The doc for this module seems pretty sparse and the examples therein are not easy to use/adapt :(
#!/usr/bin/perl use strict; use warnings; use MediaWiki::Bot; my $pagename = "foo/bar"; my $bot = MediaWiki::Bot->new({ protocol => 'https', host => 'wiki.eng.xxx.com', # path => 'foo/bar', username => 'user', password => 'pass', } ); print "bot created\n"; my $text = $bot->get_text($pagename);
Running the above hangs right after the print is executed:
# perl bot.pl bot created
I don't have access to the server that runs the wiki to debug if this is a server side issue.
Do you have any working examples of connecting to a wiki page and performing simple tasks such as examining history of pages, editing them etc..?
Thanks, ravi
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10-06-05 03:25 PM, Ravi Parimi wrote:
The doc for this module seems pretty sparse and the examples therein are not easy to use/adapt :(
Well, they were totally *wrong* until a few moments ago. I'm working on it! See http://code.google.com/p/perlwikipedia/source/browse/trunk/lib/MediaWiki/Bot... (Note your login data is passed as another hashref)
But as I said earlier: I missed that you're dealing with basic auth. We don't have any way to deal with that currently. I've opened a bug report, and I hope we can support that soon.
Do you have any working examples of connecting to a wiki page and performing simple tasks such as examining history of pages, editing them etc..?
Attached.
BTW, I am setting Reply-To to the perlwikibot Google Group. This is no longer related to the MediaWiki API.
- -Mike
A working example with http auth.
#!/usr/bin/perl use strict; use warnings; use MediaWiki::API;
my $mw = MediaWiki::API->new();
sub on_error() { print "Error code: " . $mw->{error}->{code} . "\n"; print $mw->{error}->{stacktrace}."\n"; die; }
$mw->{config}->{api_url} = 'http://hello:world@testwiki.exotica.org.uk/mediawiki/api.php'; $mw->{config}->{on_error} = &on_error;
use Data::Dumper; print Dumper $mw->get_page( { title => 'Main Page' } );
Jools,
Thanks for taking a look at my post. Is there a way to determine the location of api.php on a given wiki server? I tried out your code against my internal wiki page but it fails as below:
$perl wiki.pl Error code: 2 301 Moved Permanently : error occurred when accessing http://foo:bar@wiki.eng.vmware.com/api.php after 1 attempt(s) at /usr/local/lib/perl5/site_perl/5.10.0/MediaWiki/API.pm line 354 MediaWiki::API::api('MediaWiki::API=HASH(0x22bde88)', 'HASH(0x22e6b08)') called at /usr/local/lib/perl5/site_perl/5.10.0/MediaWiki/API.pm line 513 MediaWiki::API::get_page('MediaWiki::API=HASH(0x22bde88)', 'HASH(0x2837568)') called at wiki.pl line 21
Died at wiki.pl line 12.
On Mon, Jun 7, 2010 at 10:29 AM, Jools Wills buzz@exotica.org.uk wrote:
A working example with http auth.
#!/usr/bin/perl use strict; use warnings; use MediaWiki::API;
my $mw = MediaWiki::API->new();
sub on_error() { print "Error code: " . $mw->{error}->{code} . "\n"; print $mw->{error}->{stacktrace}."\n"; die; }
$mw->{config}->{api_url} = 'http://hello:world@testwiki.exotica.org.uk/mediawiki/api.php'; $mw->{config}->{on_error} = &on_error;
use Data::Dumper; print Dumper $mw->get_page( { title => 'Main Page' } );
Mediawiki-api mailing list Mediawiki-api@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10-06-07 02:48 PM, Ravi Parimi wrote:
Jools,
Thanks for taking a look at my post. Is there a way to determine the location of api.php on a given wiki server?
I wrote a little guide at http://code.google.com/p/perlwikipedia/wiki/WhereIsAPI which might help.
- -Mike
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10-06-07 02:48 PM, Ravi Parimi wrote:
Jools,
Thanks for taking a look at my post. Is there a way to determine the location of api.php on a given wiki server?
I wrote a little guide at http://code.google.com/p/perlwikipedia/wiki/WhereIsAPI which might help.
Thanks a lot Mike/Jools. I am now able to login to my wiki using MediaWiki::API !
On Mon, 2010-06-07 at 10:48 -0700, Ravi Parimi wrote:
Jools,
Thanks for taking a look at my post. Is there a way to determine the location of api.php on a given wiki server? I tried out your code against my internal wiki page but it fails as below:
click edit or view source on a page, and you should see a path like
/w/index.php or similar. use that path so /w/api.php.
Best Regards
Jools
mediawiki-api@lists.wikimedia.org