Bad or Invalid Token in Extension

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Bad or Invalid Token in Extension

Justin Maldonado

I'm trying to make a simple extension that moves a page on my mediawiki site, but whether using curl or FauxRequest, I always get bad token response. Tried urlencoding, not encoding, %2B/, without +/, etc, doesn't matter.  The token shown in the output looks identical to the one when requesting it from the api.  I’m a total programming noob, so could be something simple, but I feel like I’ve been through everything at this point.

Code looks like this currently using FauxRequest, with param1/2/3 coming from the parser function I'm creating.

global $wgUser;
$token = $token = $wgUser->editToken();
 
$params = new FauxRequest( 
    array(
        'action'    => 'move',
        'from'      => “$param1”,
        'to'        => “$param2”,
        'format'    => 'php',
        'reason'    => “$param3”,
        'token'     => “$token”)
);
$api = new ApiMain( $params, true);
$api->execute();
$data = & $api->getResultData();
 
 
$output = "moved $param1 to $param2 - $token";

also tried the below code using curl instead, which results in bad token as well

global $wgUser;
$token = $wgUser->editToken();
$url = 'http://www.wikiocity.com/api.php?';
    $myvars = 'action=move&format=xml&from=' . "$param1" . '&to=' . "$param2" . '&reason=' . "$param3" . '&token=' . urlencode($token);
 
    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_POST, 1);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt( $ch, CURLOPT_USERAGENT, 'MyCoolTool/1.1 (http://example.com/MyCoolTool/; [hidden email]) BasedOnSuperLib/1.4');
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
 
    $response = curl_exec( $ch );
 
 
$output = "moved $param1 to $param2 - $myvars - $response";

Am I missing something in the code, or could I have a setting wrong somewhere?

Any help would be hugely appreciated!

 


_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|

Re: Bad or Invalid Token in Extension

Betacommand
you should be using the move token not the edit token

On Sun, Apr 27, 2014 at 12:28 PM, Justin Maldonado <[hidden email]> wrote:

I'm trying to make a simple extension that moves a page on my mediawiki site, but whether using curl or FauxRequest, I always get bad token response. Tried urlencoding, not encoding, %2B/, without +/, etc, doesn't matter.  The token shown in the output looks identical to the one when requesting it from the api.  I’m a total programming noob, so could be something simple, but I feel like I’ve been through everything at this point.

Code looks like this currently using FauxRequest, with param1/2/3 coming from the parser function I'm creating.

global $wgUser;
$token = $token = $wgUser->editToken();
 
$params = new FauxRequest( 
    array(
        'action'    => 'move',
        'from'      => “$param1”,
        'to'        => “$param2”,
        'format'    => 'php',
        'reason'    => “$param3”,
        'token'     => “$token”)
);
$api = new ApiMain( $params, true);
$api->execute();
$data = & $api->getResultData();
 
 
$output = "moved $param1 to $param2 - $token";

also tried the below code using curl instead, which results in bad token as well

global $wgUser;
$token = $wgUser->editToken();
$url = 'http://www.wikiocity.com/api.php?';
    $myvars = 'action=move&format=xml&from=' . "$param1" . '&to=' . "$param2" . '&reason=' . "$param3" . '&token=' . urlencode($token);
 
    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_POST, 1);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt( $ch, CURLOPT_USERAGENT, 'MyCoolTool/1.1 (http://example.com/MyCoolTool/; [hidden email]) BasedOnSuperLib/1.4');
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
 
    $response = curl_exec( $ch );
 
 
$output = "moved $param1 to $param2 - $myvars - $response";

Am I missing something in the code, or could I have a setting wrong somewhere?

Any help would be hugely appreciated!

 


_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api



_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|

Re: Bad or Invalid Token in Extension

Niklas Keller
In reply to this post by Justin Maldonado
I don't have enough time right now to have a deeper look at your code, but:
$token = $token = $wgUser->editToken();
Although this isn't an error, it's still unnecessary.

Next, are you really using these quotes: ” instead of ". I think this happened when you copied the code because it'll result in an syntax error. Additionally, you shouldn't put pure variables into quotes, there's no reason why I would do so.

You should urlencode all parameters, not just the token.

_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|

Re: Bad or Invalid Token in Extension

Justin Maldonado

Yeah, that was just a typo copying the code over.  It is actually just the one $token variable.  Same with the double quotes.   I’ve tried no quotes as well though, along with single quotes, smiley faces, hearts, etc.

 

I was also going to do some more urlencoding, but just wanted to get the basic page move working first.

 

Thanks though, I appreciate you taking a look.  I have no doubt it could be something as simple as that.

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Niklas Keller
Sent: Sunday, April 27, 2014 11:56 AM
To: MediaWiki API announcements & discussion
Subject: Re: [Mediawiki-api] Bad or Invalid Token in Extension

 

I don't have enough time right now to have a deeper look at your code, but:

$token = $token = $wgUser->editToken();

Although this isn't an error, it's still unnecessary.

Next, are you really using these quotes: ” instead of ". I think this happened when you copied the code because it'll result in an syntax error. Additionally, you shouldn't put pure variables into quotes, there's no reason why I would do so.

You should urlencode all parameters, not just the token.


_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|

Re: Bad or Invalid Token in Extension

liangent

Well you can just use Title::moveTo() directly.

On Apr 28, 2014 1:13 AM, "Justin Maldonado" <[hidden email]> wrote:

Yeah, that was just a typo copying the code over.  It is actually just the one $token variable.  Same with the double quotes.   I’ve tried no quotes as well though, along with single quotes, smiley faces, hearts, etc.

 

I was also going to do some more urlencoding, but just wanted to get the basic page move working first.

 

Thanks though, I appreciate you taking a look.  I have no doubt it could be something as simple as that.

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Niklas Keller
Sent: Sunday, April 27, 2014 11:56 AM
To: MediaWiki API announcements & discussion
Subject: Re: [Mediawiki-api] Bad or Invalid Token in Extension

 

I don't have enough time right now to have a deeper look at your code, but:

$token = $token = $wgUser->editToken();

Although this isn't an error, it's still unnecessary.

Next, are you really using these quotes: ” instead of ". I think this happened when you copied the code because it'll result in an syntax error. Additionally, you shouldn't put pure variables into quotes, there's no reason why I would do so.

You should urlencode all parameters, not just the token.


_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api


_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|

Re: Bad or Invalid Token in Extension

Justin Maldonado

I figured there was something like that I could use, but wasn’t sure if I’d be able to figure it out.  (remember, is noob)

 

I’m guessing it would be something along the lines of

 

Title::moveTo

(

$param2,

$auth = true,

$reason = $param3,

$createRedirect = true 

)

;

But I’m assuming that would just move whatever the current page is over to the page listed in param2?  How could I tell it to move a page listed in $param1 over to $param2?

 

Thanks!

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Liangent
Sent: Sunday, April 27, 2014 12:20 PM
To: MediaWiki API announcements & discussion
Subject: Re: [Mediawiki-api] Bad or Invalid Token in Extension

 

Well you can just use Title::moveTo() directly.

On Apr 28, 2014 1:13 AM, "Justin Maldonado" <[hidden email]> wrote:

Yeah, that was just a typo copying the code over.  It is actually just the one $token variable.  Same with the double quotes.   I’ve tried no quotes as well though, along with single quotes, smiley faces, hearts, etc.

 

I was also going to do some more urlencoding, but just wanted to get the basic page move working first.

 

Thanks though, I appreciate you taking a look.  I have no doubt it could be something as simple as that.

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Niklas Keller
Sent: Sunday, April 27, 2014 11:56 AM
To: MediaWiki API announcements & discussion
Subject: Re: [Mediawiki-api] Bad or Invalid Token in Extension

 

I don't have enough time right now to have a deeper look at your code, but:

$token = $token = $wgUser->editToken();

Although this isn't an error, it's still unnecessary.

Next, are you really using these quotes: ” instead of ". I think this happened when you copied the code because it'll result in an syntax error. Additionally, you shouldn't put pure variables into quotes, there's no reason why I would do so.

You should urlencode all parameters, not just the token.


_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api


_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|

Re: Bad or Invalid Token in Extension

Brad Jorsch (Anomie)
On Sun, Apr 27, 2014 at 1:20 PM, Liangent <[hidden email]> wrote:

Well you can just use Title::moveTo() directly.

Yes, this. It's rarely necessary and often a mistake to use FauxRequest to call the local wiki's API. And using curl to hit the local wiki is likely even worse.
 
On Sun, Apr 27, 2014 at 2:49 PM, Justin Maldonado <[hidden email]> wrote:

I figured there was something like that I could use, but wasn’t sure if I’d be able to figure it out.  (remember, is noob)

 

I’m guessing it would be something along the lines of

 

Title::moveTo

(

$param2,



$auth = true,



$reason = $param3,



$createRedirect = true 


)

;


Title::moveTo isn't a static method. You'd do something like this:

  $oldTitle = Title::newFromText( $param1 );
  $newTitle = Title::newFromText( $param2 );
  // Error checking here
  $oldTitle->moveTo( $newTitle, true, $param3, true );
  // More error checking here

If you need additional help, you might want to try the wikitech-l mailing list, or ask on IRC (#mediawiki on freenode).


--
Brad Jorsch (Anomie)
Software Engineer
Wikimedia Foundation

_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|

Re: Bad or Invalid Token in Extension

Justin Maldonado

Looks like it’s working!  Thank you!  That should get me started nicely!  I’ll definitely be sure to check out the wikitech-l mailing list for any of my non-api questions.

 

Thanks again to everyone!

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Brad Jorsch (Anomie)
Sent: Monday, April 28, 2014 8:48 AM
To: MediaWiki API announcements & discussion
Subject: Re: [Mediawiki-api] Bad or Invalid Token in Extension

 

On Sun, Apr 27, 2014 at 1:20 PM, Liangent <[hidden email]> wrote:

Well you can just use Title::moveTo() directly.

Yes, this. It's rarely necessary and often a mistake to use FauxRequest to call the local wiki's API. And using curl to hit the local wiki is likely even worse.

 

On Sun, Apr 27, 2014 at 2:49 PM, Justin Maldonado <[hidden email]> wrote:

I figured there was something like that I could use, but wasn’t sure if I’d be able to figure it out.  (remember, is noob)

 

I’m guessing it would be something along the lines of

 

Title::moveTo

(

$param2,

$auth = true,

$reason = $param3,

$createRedirect = true 

)

;

 

Title::moveTo isn't a static method. You'd do something like this:

  $oldTitle = Title::newFromText( $param1 );

  $newTitle = Title::newFromText( $param2 );

  // Error checking here

  $oldTitle->moveTo( $newTitle, true, $param3, true );

  // More error checking here

 

If you need additional help, you might want to try the wikitech-l mailing list, or ask on IRC (#mediawiki on freenode).



--
Brad Jorsch (Anomie)
Software Engineer
Wikimedia Foundation


_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api