[18:11:07] anyone here able to answer some questions about an API error? [18:11:07] specifically about https://www.mediawiki.org/wiki/API:User_group_membership [18:13:33] !ask [18:14:10] Don't yell at me for code quality, but https://gist.github.com/Protonk/e9b72657e889a72af15f [18:14:10] script works up until the removal of rights [18:14:10] lines 37-39 seem to be where the problem is. If I put the token in the query string, I get an error saying that it needs to be in the body [18:14:11] if I put it in the payload only I get an error that the token wasn't passed [18:14:12] if I put it in the payload and the query string I get the first error again [18:22:26] Protonk: umm [18:22:43] try printing out request to make sure its doing what you think it is [18:22:44] ? [18:27:04] this is the url it generates: https://en.wikipedia.org/w/api.php?reason=Course+instructor+rights+are+deprecated+per+https%3A%2F%2Fen.wikipedia.org%2Fw%2Findex.php%3Ftitle%3DWikipedia%3AEducation_noticeboard%26oldid%3D672369110%23Change_in_instructor_user_rights_with_new_course_page_system&remove=epinstructor&user=Adam+%28Wiki+Ed%29&action=userrights [18:27:17] I can slim that down by removing the reason etc. [18:29:55] w/ https://en.wikipedia.org/w/api.php?action=userrights&remove=epinstructor&reason=something&token=0b352adc9775b00516c1f03a3ad8175355ae8f65%252B%5C&user=Adam+%28Wiki+Ed%29 [18:30:10] I get "The 'token' parameter was found in the query string, but must be in the POST body" [18:30:40] which is fine, except if I add it to the body nothing changes. and if I add it to the body and remove it from the query string, I get an error that no token was passed [18:31:59] Protonk: Not sure, but I was fooling with ApiSandbox [18:32:07] and action=edit (which also requires POST rather than GET) [18:32:14] placed all parameters other than action and format into the POST data [18:32:31] so try that perhaps? [18:32:35] will do [18:32:37] thanks [18:34:12] hmmm. no dice. Says the token parameter must be set. (error code is "notoken") [18:34:54] You are sure you are using the right token? [18:35:17] and it is encoded correctly? [18:35:50] Protonk: there is a backslash '\' at the end of a token, that you are not encoding [18:37:25] https://docs.python.org/2/library/urllib.html#urllib.urlencode [18:37:41] from urllib import urlencode [18:38:03] something like urlencode( getUserToken( targetname ) ) ? [18:42:01] From my testing action=query&meta=tokens returns a token with an extra backslash at the end [18:42:10] for some reason I'm not able to import anything from urllib. hmm [18:43:23] ahh, nevermind [18:43:27] the extra backslash is JSON escaping [18:43:28] silly me [18:43:59] Protonk: python 2 or 3? [18:44:04] 3 [18:44:12] which may explain the problem [18:44:22] urllib.parse.urlencode [18:44:31] (see note at top of python 2 doc i linked previously) [18:48:38] Protonk: any luck? [18:49:20] Now I get a different error message, so that's progress [18:49:30] it's telling me I have an invalid token. one moment [18:50:13] Make sure that python is properly handling JSON escaping [18:51:53] Protonk: someone on stackoverflow suggested json.loads( r.json() )['query']['tokens']['userrightstoken'] [18:52:03] that way it will process the escaped backslash so the token is valid [18:52:40] you need to put all data in the POST body [18:52:55] not just the token [18:53:00] Nevermind, that probably wouldn't work [18:53:04] because requests.get doesn't return a string [18:53:05] derp [18:55:52] Protonk: wherever you use POST, specify data = uriencodedparams, not params = [18:56:39] params == query parameters, whereas the api expects formdata in the body. [18:58:56] headers = {'content-type': 'application/x-www-form-urlencoded'} [18:58:56] payload = {'action': 'edit', 'assert': 'user', 'format': 'json', 'text': content, 'summary': summary, 'title': title, 'token': edit_token} [18:58:59] r4 = requests.post(baseurl+'api.php', headers=headers, data=payload, cookies=edit_cookie) [19:00:14] ok, thanks thedj [19:00:24] I'll make those changes momentarily [19:17:04] https://gist.github.com/Protonk/e9b72657e889a72af15f So now I'm url encoding the whole payload. The token is properly escaped but I'm getting an invalid token error [19:17:23] I'm pretty sure that I'm using the right token (from meta tokens -> userrights) [19:20:46] apologies if thedj responded in the last 90 seconds. :) [19:21:46] nope [19:25:43] ehm, wait, you are not persisting your cookies between the requests ? [19:27:27] not that I know of. But I have to be logged in to get a user token at all [19:27:32] and that seems to persist [19:29:01] oohhhh. hang on. I may be very dumb [19:29:13] well, I AM, but this may be a manifestation [19:29:15] one second [19:34:17] Protonk: print out your token if you are completely stuck [19:34:35] Then you can tell if you are correctly getting token and if you are properly escaping it [19:41:18] Protonk: ehm, you are not logged [19:41:41] from your firs request, you will get a lgtoken. You need to login AGAIn with that lgtoken [19:42:30] thedj: hmm. the logging in I'm pretty sure works [19:42:46] if you aren't logged in the request for a user token will return, but with the token field blank [19:43:04] 'token': '+\\' [19:43:06] and that's it [19:50:21] Protonk: well then you are lucky, perhaps you still have cookies floating somewhere [19:51:23] hey! thedj. Persisting the cookies appropriately across sessions worked! [19:52:32] was just gonna say, you should try with https://en.wikipedia.org/wiki/Special:ApiSandbox#action=query&meta=tokens&format=json&type=csrf [19:53:40] was just gonna say, you should try with https://en.wikipedia.org/wiki/Special:ApiSandbox#action=userrights [19:55:55] thedj: thanks for your help. Was kinda driving me nuts. Now I have to go and fix the documentation errors in https://www.mediawiki.org/wiki/API:User_group_membership :) [19:56:08] please do :) [21:26:03] Krenair: https://phabricator.wikimedia.org/T62373#1468884 run it on nlwiki? :)