Importing users with the Zendesk API Follow

Comments

12 comments

  • Avatar
    Michael Jessen

    This is great!

    I'm trying to make this work, though, and hitting a lot of stumbling blocks. We're using Duo remote authentication, so I need to use an API token. How would I integrate that here?

    I tried changing the session.auth to:

    session.auth = 'emailaddress/token:token'

    But now I'm getting a lot more errors on output that suggest to me this isn't right.

    Traceback (most recent call last):
    File "user_import.py", line 33, in
    response = session.post(url, data=payload)
    File "/Library/Python/2.7/site-packages/requests/sessions.py", line 508, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
    File "/Library/Python/2.7/site-packages/requests/sessions.py", line 451, in request
    prep = self.prepare_request(req)
    File "/Library/Python/2.7/site-packages/requests/sessions.py", line 382, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
    File "/Library/Python/2.7/site-packages/requests/models.py", line 308, in prepare
    self.prepare_auth(auth, url)
    File "/Library/Python/2.7/site-packages/requests/models.py", line 496, in prepare_auth
    r = auth(self)
    TypeError: 'str' object is not callable

  • Avatar
    Charles Nadeau

    Hi Michael,

    session.auth should be a tuple, so try

    `session.auth = 'emailaddress/token', 'token'`

    Make sure the string '/token' remains appended to your email.

  • Avatar
    Michael Jessen

    Hi Charles,

    Yikes! I had actually done that first, but must have made some other error, because I thought that didn't work. But it does, so thank you!

    That's made all the other errors go away, but it does still fail with "status 400".

    I had also received the message "InsecurePlatformWarning: A true SSLContext object is not available" but was able to solve that with "pip install requests[security]", but perhaps that had an unintended consequence?

    Thank you for the help!

  • Avatar
    Charles Nadeau

    A 400 error usually means the request couldn't be understood by the server due to some syntax problem. Can you make a successful test request with curl? Something like:

    ```
    curl https://{subdomain}.zendesk.com/api/v2/users/create_many.json \
    -H "Content-Type: application/json" -X POST \
    -d '{"users": [{"name": "Roger Wilco", "email": "roge@example.org"}, {"name": "Woger Rilco", "email": "woge@example.org"}]}' \
    -v -u {email_address}:{password}
    ```

    As for the SSL problem, I notice you're using Python 2.7. I haven't tested with that version (I'm using 3.3) but from a quick Google search, upgrading to 2.7.9 might help.

  • Avatar
    Michael Jessen

    I can, yes!

    I have to remove the brackets around email_address and password for it to work. I'm very new to Python, so a lot of this is new to me. But THAT script does create a user. Is there something I can learn from that success to get the other one working?

    I was just running through OS X terminal, which is 2.7, but I've also tried on 3.4.3 as well. Same thing!

  • Avatar
    Charles Nadeau

    Hi Michael

    It seems to point to a problem with the data pulled from your spreadsheet and sent in the request. The sample script assumes some text data in column C, D, and H of a spreadsheet. The `print` statement is your best friend here for debugging. You can inspect the data you're trying to send. If you switched to Python 3, add the following print statement in the `for payload in payloads` loop and comment out the rest of the lines:

    ```
    for payload in payloads:
    print(payload)
    # response = session.post(url, data=payload)
    # if response.status_code != 200:
    # print('Import failed with status {}'.format(response.status_code))
    # exit()
    # print('Successfully imported a batch of users')
    ```

    If you're using Python 2.7, the print statement is `print payload`.

    Anything look strange in the JSON that's printed out compared to what's in your spreadsheet? The data will be different but it should be formed like the data in the curl test request:

    ```
    {"users": [{"name": "Roger Wilco", "email": "roge@example.org"}, {"name": "Woger Rilco", "email": "woge@example.org"}]}
    ```

  • Avatar
    Michael Jessen

    Wow, thank you so much. I really appreciate your walking me through this. I had added phone number as a column, and Excel was formatting it as a number, so even though it looked right, the script was trying to feed in xxxx.0!

  • Avatar
    Winterrain Sa

    hi,
    is there any way to use this in php

  • Avatar
    Jordan Weekes

    There is a incorrect section on this page which caused us a few hours headache before we figured it out! Otherwise good job! :)


     


    for user in users_dic['users']:
    
    print(user)

    Should be


    for user in users_dict['users']:
    
    print(user)

  • Avatar
    Charles Nadeau

    Sorry about that. I fixed the typo. Thanks.  

  • Avatar
    John Warner

    The above code which i modified slightly worked fine for importing all my users, but now when I ran it again the first batch of 100 users fails with a status code of 400.

    Could someone from Zendesk support let me know what could be the cause of this error?

    Thanks

  • Avatar
    John Warner

    Nevermind, I had changed the POST to a PUT and that was borking it up.

Please sign in to leave a comment.

Powered by Zendesk