+ Reply to Thread
Results 1 to 3 of 3

Thread: Adding new users with the API

  1. #1

    Question Adding new users with the API

    Hello,

    I've been trying to add new users to a Deki Wiki using the API but have only found a dozen ways on how *not* do it and I'm looking for some help on just 1 way to actually do it. I'm trying implement this in a python script although have also been messing with curl, I can successfully run GET commands and even other POST commands for pages, but I can't understand what I'm doing wrong when trying to POST new users.

    Here's are the current curl commands:
    Code:
    #!/bin/bash
    curl -c cookies.txt -u {Username}:{Passwd} http://Deki.Wiki.Url//@api/deki/users/authenticate
    curl -b cookies.txt -X POST -d @testuser.xml "http://Deki.WIki.Url/@api/deki/users"
    testuser.xml is a xml file that I copied from the POST:users API documentation input format section:
    Code:
    <user id="200">
    	<username>JackSprat</username>
    	<email>jsprat@gmail.com</email>
    	<fullname>Jack Sprat</fullname>
    	<status>active</status>
    	<service.authentication id="1" />
    	<permissions.user>
    		<role>contributor</role>
    	</permissions.user>
    </user>
    I've also tried removing the status and service.authentication parts as they are in the implementation section of the API doc.

    When I run this it complains that the username isn't specified:
    Code:
    <?xml-stylesheet type='text/xsl' href='/@api/host/resources/error.xslt'?>
    <error>
    	<status>400</status>
    	<title>Bad Request</title>
    	<message>'username' not provided or invalid</message>
    	<uri>http://Deki.Wiki.Url/@api/deki/users</uri>
    </error>
    I've already tried replacing -d in the curl code with --data-binary and --data-urlencoded as well as just pasting in the XML testuser information (whitespace removed), and using -d username=JackSprat etc... although none of them work.


    The end goal though is to have this running in a python script using a standard library module. Heres an example of what I've been using with urllib2:
    Code:
    wikiURL = 'http://Deki.Wiki.Url'
    user = '{Username}'
    passwd = '{Password}'
    
    # Get the authentication info, save to cookieReponse
    url = wikiURL+"/@api/deki/users/authenticate"
    passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    passmgr.add_password(None, url, user, passwd)
    authhandler = urllib2.HTTPBasicAuthHandler(passmgr)
    opener = urllib2.build_opener(authhandler)
    cookieResponse = opener.open(url)
    
    # `input` is a string of the testuser.xml file, whitespace removed
    url = wikiURL+'/@api/deki/users/'
    request = urllib2.Request(url=url, data=input)
    request.add_header('Set-Cookie', cookieResponse.headers['Set-Cookie'])
    response = urllib2.urlopen(request)
    This yields a HTTPError: HTTP Error 500: Internal Server Error

    I've been hacking at this for a while and am would like to not pull out all of my hair over this =) I'm not a window's user so using c# isn't much of an option for me. I'm also trying to refrain from just going into the database to play it safe although its looking like it might be easier. Any help on this would be *greatly* appreciated!

    Cheers.

  2. #2
    Join Date
    Feb 2007
    Location
    San Diego, CA
    Posts
    733

    Default

    If you're adding a new user, then do not specify an id attribute in your xml:

    Code:
    <user>
    	<username>JackSprat</username>
    	<email>jsprat@gmail.com</email>
    	<fullname>Jack Sprat</fullname>
    	<status>active</status>
    	<service.authentication id="1" />
    	<permissions.user>
    		<role>contributor</role>
    	</permissions.user>
    </user>
    Also, the command line I use varies slightly:

    Code:
    curl -c cookies.txt -u {Username}:{Passwd} http://Deki.Wiki.Url//@api/deki/users/authenticate
    curl -b cookies.txt -H "Content-type: text/xml" -d @testuser.xml "http://Deki.WIki.Url/@api/deki/users"
    Let me know if problems persist.

  3. #3

    Default

    Works great =D Thank you very much brigettek!

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts