0

Some API calls return empty string

I used the API reference and was able to extract information about Posts and Users, but nothing else. Wherever I try to extract information about searches for example I get an empty json {}. That is:

/api/2/metrics/searches?fields=query,top_clicked_item,searches,click_through_rate,avg_results&period=30d&sort=searches

Please help.

5replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Hi Ahmed Esmat , thank you for your question! The metrics endpoint requires a token with the Admin role. Without the admin role it will not return a result.

    Like
  • I am now using a user/token with Admin role, but still getting an empty resultset. My Python code looks like this:

    session = requests.Session()
    session.auth = ('Authorization: user_name', 'ZK****************0l')
    headers = {'Content-Type': 'application/json'}
    Query='https://community.******.com/api/2/metrics/searches?fields=query,top_clicked_item,searches,click_through_rate,avg_results&period=30d&sort=searches'
    url_content = session.get(Query).content
    result_data = json.loads(url_content)
    print(result_data)

    Am I missing something? I also tried:

    session.auth = ('user_name', 'ZK****************0l')

    But with the same results

    Like
    • Ahmed Esmat 

      The Authorization needs to use Bearer token instead of username. It should be passed in the headers object. For example:

      headers = {"Authorization": "Bearer ZK****************0l"}
      
      Like
  • Thanks so much for the tip. The following had worked:

    session = requests.Session()
    headers = {'Authorization': 'Bearer 'ZK****************0l'}
    session.headers.update(headers)
    Query='https://community.******.com/api/2/metrics/searches?fields=query,top_clicked_item,searches,click_through_rate,avg_results&period=30d&sort=searches'
    url_content = session.get(Query).content
    result_data = json.loads(url_content)
    print(result_data)
    

    I suggest adding sample python code to your documentation.

    Like
    • Ahmed Esmat I'm glad that helped! Thanks for sharing your solution!

      Like
Like Follow
  • Status Answered
  • 1 mth agoLast active
  • 5Replies
  • 22Views
  • 2 Following