Getting large data sets with the Zendesk API and Python Follow

Comments

15 comments

  • Avatar
    Viola Rudenko

    Hi Charles,


    Thank you for the great API tutorials! It's super helpful.


    Is there a way to use token for authentication in this setup? I tried to follow the token guidelines here , but it gives me 404 error:


    credentials = 'my_email' + '/token', 'my_api_token'
    session = requests.Session()
    session.auth = credentials


    OR


    user = 'my_email +'/token'
    pwd = 'my_api_key'
    session = requests.Session()
    session.auth = (user, pwd)


    I also tried to set the header like here, but still no luck:


    session = requests.Session()
    head = {'Authorization': 'access_token my_api_token'}
    response = session.get(url, headers=head)


    P.S. I was able to get access to Zendesk data with my email address and token before.

  • Avatar
    Charles Nadeau

    Hi Viola,


    Hmm, I tested the script with a token and it works for me. Example:


    credentials = 'example@outlook.com' + '/token', 'qPbD6eTsXNC4aBIDs4UkHYD3fss6fdfalnZDjmSN'

    Did you check if token access is enabled in your Zendesk? In the agent interface, click Admin (gear icon) in the lower-left, then API in the Channels category. On the Channels/API page, verify that Token Access is enabled (the blue checkbox). If not, make sure to click the Save button after selecting the checkbox.

  • Avatar
    Viola Rudenko

    Hi Charles,


    Thank you for looking into that! The token access was enabled, but looks like I picked incorrect topic_id. As soon as I switched to ticket_metrics, the script executed.

  • Avatar
    Don Wood

    Thanks! this looks very useful. I'm not very technical so excuse my question. Can this work to get all the data on tickets out to a spreadsheet? The reporting in Zendesk is limited and I want to be able to get ticket data our of the system and into a format to build reports. A simple this I would like is to report the ticket and the associated JIRA ticket number. We are using the integration with JIRA but I don't see a way to get this information.

  • Avatar
    Emilio Méndez

    Great topic, not only in content but in form as well; exactly what I was looking for !!  Thank you Charles & Zendesk Team !!

  • Avatar
    Emilio Méndez

    Hi Don:


    Basic reporting might be limited, but you can try "Insights", it's definitely a whole different thing; the only thing is that it depends on the plan you have.


    Hope that works for you !!


    Regards,

  • Avatar
    Charles Nadeau

    Hi Don,


    To learn how to use the API to export lots of data to Excel, see the companion tutorial:


  • Avatar
    Alex Taitague

    Thanks. I had trouble at the pickle part...I run my script with the API call and serializing the results, but when I go to run the script again (API commented out) I get the message that the pickle file cannot be found. I can't find any file created after the script run. (within any script run, I can write and read back the serialized data -- it just doesn't persist outside of the script run).


    Any ideas?


     


     

  • Avatar
    Charles Nadeau

    Hi Alex,


    I ran this script again to test if any Python libraries had been updated since I wrote it. Running Python 3.6, I had no issues serializing the data into a separate file named 'my_serialized_data_file'. The file appears in the same folder as my Python script.


    Maybe you're in Windows and the OS is confused by the lack of a file extension? Try adding ".p" to the Pickle file, as follows:


    topic_data = {'posts': topic_posts, 'users': user_list}
    with open('my_serialized_data_file.p', mode='wb') as f:
    pickle.dump(topic_data, f)

     

  • Avatar
    Ann Jensen

    Hi,

    Just wondering if there is a similar article that describes how to bulk update Zendesk (Help Center) e.g. by bulk uploading articles from one script?

    Thanks,

    Ann

  • Avatar
    Charles Nadeau

    Hi Ann,

    The batch-publishing tool the Zendesk Docs team uses is open source on Github for anybody to use. See https://github.com/chucknado/zpu. For instructions on how to use it, see the readme at https://github.com/chucknado/zpu/blob/master/README.md.

    Feel free to download and modify it for your use case.

    Thanks.

  • Avatar
    Jose Juan Muñoz JImenez (Edited )

    Could you explain what this is about? "The Zendesk support URL should look like 'https://obscura.zendesk.com'". I do not understand how the URL should look

  • Avatar
    Charles Nadeau (Edited )

    Hi Jose,

    Sorry if it's not clear. Your Zendesk URL has two parts: a subdomain name you chose when you set up your account, followed by zendesk.com (for example: mycompany.zendesk.com). 

    The URLs for 3 different accounts might look something like this:

    - https://red.zendesk.com

    - https://blue.zendesk.com

    - https://green.zendesk.com

    Hope that helps.

     

  • Avatar
    Panaguit Chrysler

    Hello my code says keyerror 'posts'

  • Avatar
    Panaguit Chrysler

    import pickle
    import time
    import requests

    ##credentials = 'your_zendesk_email', 'your_zendesk_password'
    credentials = 'user@something.com' + '/token', 'thisisatokencode'
    session = requests.Session()
    session.auth = credentials
    zendesk = 'zendeskurl'

    topic_id = 360002871987
    topic_posts = []
    user_list = []
    url = zendesk + '/api/v2/community/posts/' + str(topic_id) + '/posts.json?include=users'
    while url:
    response = session.get(url)
    if response.status_code == 429:
    print('Rate limited! Please wait.')
    time.sleep(int(response.headers['retry-after']))
    continue
    if response.status_code != 200:
    print('Error with status code {}'.format(response.status_code))
    exit()
    data = response.json()
    topic_posts.extend(data['posts'])
    user_list.extend(data['users'])
    url = data['next_page']

    topic_data = {'posts': topic_posts, 'users': user_list}
    with open('my_serialized_data_file.p', mode='wb') as f:
    pickle.dump(topic_data, f)

    with open('my_serialized_data_file.p', mode='rb') as f:
    topic = pickle.load(f)

    for post in topic['posts']:
    author = 'anonymous'
    for user in topic['users']:
    if user['id'] == post['author_id']:
    author = user['name']
    break
    print('"{}" by {}'.format(post['title'], author))

Please sign in to leave a comment.

Powered by Zendesk