Cannot create page

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

Cannot create page

Matthew H. Cahn
Hi, I’m trying to create a page programmatically.  I can get a token back from a GET, but doing an action=edit in a PUT does not create the page.  The response I get back is the MediaWiki API help page.

My Python code is below.  Can anyone see what I’m doing wrong?

Thanks,
Matthew



#!/bin/env python

import requests
import urllib
import urllib.parse


baseUrl = 'http://chlamyannotations-test2.princeton.edu/api.php'

params = {'action': 'query',
          'meta': 'tokens'}

responseFilename = '/molbio2/mcahn/temp/createPagesResponse.html'

r = requests.get(baseUrl, params=params)

print(r)
print(r.text)

params = {'action': 'edit',
          'title': 'TestPage',
          'summary': 'Test summary',
          'text': 'article content',
          'token': r'+\\'}

f = open(responseFilename, 'w')
   
r = requests.put(baseUrl, data=urllib.parse.urlencode(params))
print(r)
f.write(r.text)
f.close()



||  Matthew Cahn  |  Linux Administrator  |  Dept. of Molecular Biology / Research Computing  |  Princeton University  |  (609) 258-5404  |  [hidden email]  ||



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

Re: Cannot create page

Bartosz Dziewoński
You'll need to use POST instead of PUT.

--
Bartosz Dziewoński

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

Re: Cannot create page

Matthew H. Cahn
In reply to this post by Matthew H. Cahn
Oh, of course, POST, thanks.  Now it works, after also removing the “r” (raw string) from the token since it already has an escaped backslash, and removing urllib.parse.urlencode from the parameters.  Here’s the working version in case anyone would like to see it:

#!/bin/env python

import requests

baseUrl = 'http://chlamyannotations-test2.princeton.edu/api.php'

params = {'action': 'query',
          'meta': 'tokens'}

responseFilename = '/molbio2/mcahn/temp/createPagesResponse.html'

r = requests.get(baseUrl, params=params)

print(r)
print(r.text)

params = {'action': 'edit',
          'title': 'TestPage3',
          'summary': 'Test summary',
          'text': 'article content',
          'token': '+\\'}

f = open(responseFilename, 'w')
   
r = requests.post(baseUrl, data=params)

print(r)
f.write(r.text)
f.close()


||  Matthew Cahn  |  Linux Administrator  |  Dept. of Molecular Biology / Research Computing  |  Princeton University  |  (609) 258-5404  |  [hidden email]  ||



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

Re: Cannot create page

Brad Jorsch (Anomie)
On Fri, Aug 17, 2018 at 11:45 AM, Matthew H. Cahn <[hidden email]> wrote:

> r = requests.get(baseUrl, params=params)
>
> print(r)
> print(r.text)
>
> params = {'action': 'edit',
>           'title': 'TestPage3',
>           'summary': 'Test summary',
>           'text': 'article content',
>           'token': '+\\'}

You should be using the token in the response (r) here, not
hard-coding the current anonymous-user "+\" token.


--
Brad Jorsch (Anomie)
Senior 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: Cannot create page

Matthew H. Cahn
In reply to this post by Matthew H. Cahn
Thanks, Brad.  Yes, I was being lazy since the token is always the same.

Matthew

||  Matthew Cahn  |  Linux Administrator  |  Dept. of Molecular Biology / Research Computing  |  Princeton University  |  (609) 258-5404  |  [hidden email]  ||



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