2015年7月3日 星期五

create new users in openfire via user service plugin using python requests

由於工作需要,來吧,看如何加帳號

原文看這 http://stackoverflow.com/questions/29310297/create-new-users-in-openfire-via-user-service-plugin-using-python-requests

You should not set two Authorization headers. You can do Basic or Digest authorization, and the auth argument can handle either. Pick one or the other.
Using basic auth:
headers = {'Content-Type': 'application/xml'}
r = requests.post(
    url='http://192.168.200.115:9090/plugins/userService/users',
    data=xml, headers=headers, 
    auth=('admin', '12345'))
or using digest auth:
headers = {'Content-Type': 'application/xml'}
r = requests.post(
    url='http://192.168.200.115:9090/plugins/userService/users',
    data=xml, headers=headers, 
    auth=HTTPDigestAuth('admin', '12345'))
See the dedicated Authentication chapter of the documentation.
The Openfire user service endpoint should work just fine with the basic auth option.
You can more easily create the XML document using templating, and you should really use the xml.sax.saxutils.escape() function to ensure your data is suitable for inclusion in the document:
from xml.sax.saxutils import escape

xml = """\
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user>
    <username>{}</username>
    <password>{}<password>
</user>""".format(escape(name), escape(password))


This is for my future reference , openfire requires the authorization header. My working code is as follows , It adds test user with password as test
import requests 
from xml.sax.saxutils import escape

def add_controller(name,password):
    xml = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user>
    <username>{}</username>
    <password>{}</password>
</user>""".format(escape(name), escape(password))

    headers = {'Content-Type': 'application/xml','Authorization': 'Basic YWRtaW46MTIzNDU='}
    r = requests.post(
        url='http://192.168.200.105:9090/plugins/userService/users',
        data=xml, headers=headers, 
        auth=('admin', 'admin')
    )
    print r 

add_controller("test","test")

沒有留言:

張貼留言