5

I am brand new to Python coding, and getting stuck with a particular task.

I have the following URL: https://freeman7.zendesk.com/api/v2/views/313117127/count.json

I would like to use Python to display something like the following (this is the content that that URL displays when you open it in a web browser):

{
"view_count": {
"url":     "https://freeman7.zendesk.com/api/v2/views/313117127/count.json",
"view_id": 313117127,
"value":   2,
"pretty":  "2",
"fresh":   true
}
}

I've tried all sorts of things after searching the Internet for a couple of hours, but can't seem to find anything that works. Here is an example of something I've tried:

import urllib.request, json
with urllib.request.urlopen("url") as url:
    data = json.loads(url.read().decode())
    print(data)

And here is the error I get for that:

URLError: urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Anyone have any idea what's going on here, and/or have a recommendation on how I could successfully complete this task? I would appreciate any insight!

Thanks, Stephen

5
  • What is the authentication method for this API? Commented May 13, 2017 at 20:28
  • 1
    You must authenticate: freeman7.zendesk.com/access/unauthenticated Commented May 13, 2017 at 20:31
  • 2
    I would generally recommend requests to do that. It's interface is more intuitive and it is easier to keep an authenticated session than with urllib. Commented May 13, 2017 at 20:37
  • Thanks for the responses. Could someone help me understand what syntax I would use to add authentication info? Commented May 13, 2017 at 20:40
  • 1
    Approach: Use a requests Session object to post proper credentials on the login page, then use the same Session object to make the API call. You wil have to read some doumentation ;) Commented May 13, 2017 at 20:48

1 Answer 1

1

This works:

import json

import requests

url = 'https://freeman7.zendesk.com/api/v2/views/313117127/count.json'
response = requests.get(url)
response_json = response.json()
print(json.dumps(response_json, sort_keys=True, indent=4))

Results:

{
    "error": {
        "message": "There is no help desk configured at this address. This means that the address is available and that you can claim it at http://www.zendesk.com/signup",
        "title": "No help desk at freeman7.zendesk.com"
    }
}

If I had access to the actual content it would be better, but this will work for all JSON responses from a URL.

Also, for viewing JSON content in a browser, this Chrome extension is clutch: https://chrome.google.com/webstore/detail/json-formatter/bcjindcccaagfpapjjmafapmmgkkhgoa

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.