Python and WordsApi: 401 unauthorized when using getRandomWords()

232 views
Skip to first unread message

William Shaw

unread,
Jan 19, 2015, 12:15:38 PM1/19/15
to wordn...@googlegroups.com
This is my first time using Wordnik with Python. I also don't have a lot of Python experience. When I execute getRandomWords() against the WordsApi the following error results. I've tried a number of methods in Wordnik and receive the same error for each. What am I doing wrong? I have not provided userId and password info anywhere as it seems getRandomWords() doesn't need it as apposed to some of the wordlist methods that seem to.

urllib2.HTTPError: HTTP Error 401: Unauthorized

My simple script is:

from docx import Document
document = Document()
document.add_heading('Document Title',0)
from wordnik import *
apiUrl = 'http://api.wordnik.com/v4'
apiKey ='xxxxxxxxxxxxxxxxxxxx'
client = swagger.ApiClient(apiKey, apiUrl)
wordsApi = WordsApi.WordsApi(client)
randomWordsList = wordsApi.getRandomWords()
len(randomWordsList)
#code to add words to docx doc - trying to create very large docx file for upload tests
document.save('demo.docx')

William Shaw

unread,
Jan 19, 2015, 10:38:44 PM1/19/15
to wordn...@googlegroups.com
Also, I am working in a Windows 8 environment

Russell Horton

unread,
Jan 19, 2015, 11:08:32 PM1/19/15
to wordn...@googlegroups.com
Hi William,

You're right that getting random words doesn't require login credentials, and your code looks great to me. I just tried the same thing with your code and had no problems, so here is how I would suggest going about pinning down the issue.

a) Try again (could have been a transient server error).

b) Re-install the python client if it might be more than a few months old, making sure to get the latest version from PyPI or from here: https://github.com/wordnik/wordnik-python

c) Double-check to make sure your API key is set correctly (I get the same 401 error if I use an invalid API key). Eliminate the non-Wordnik parts of your code (the DocX bits) to streamline the example and make sure the line number of the error is that of the API call (I'm sure it is, but just to double-check...).

d) If your API key is correct per your records, you could sign up for a new one and see if the issue persists, or leave your contact info for Erin, and she'll check on the server to make your permissions are correct.

e) If you have verified that you have a known working API key, correctly set, and you're still having issues, we'll have to keep sleuthing on this one. I would like to see if someone else on a Windows 8 environment can duplicate the issue or not.

Most likely, in my opinion, is that there was a transient server error, your API key is mistyped in your code, or your API key is misconfigured on the server.

Good luck, let us know how it goes!

Russ

William Shaw

unread,
Jan 20, 2015, 12:14:53 AM1/20/15
to wordn...@googlegroups.com
Russ - Thank you. Erin has validated my key and successfully used it in a simple WordsApi request. I just installed Python (2.7) a few days ago. I trimmed out the extra stuff, same results (output below). I am seeing a lot of info on web about windows8.1 and 401 errors. Unfortunately, possible solutions I've seen so far are out of my comfort zone.  Seems to depend  on if hosting site is  on IIS 6.0.
                   

Inline image 1

--
You received this message because you are subscribed to a topic in the Google Groups "Wordnik API" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wordnik-api/66J8VMNYCtw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to wordnik-api...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Russell Horton

unread,
Jan 20, 2015, 12:27:00 AM1/20/15
to wordn...@googlegroups.com
Thanks for the stack trace and the info about other things you tried. How odd and unfortunate! Do you by any chance have access to another non-Windows computer that you could try running the exact same script on? That would let us know for sure whether it is indeed some kind of urllib2 / Windows error. 

For what it's worth, Wordnik is not being run on an IIS server, so that particular error should not be the cause here.

Anyone out there with Windows 8 able to give this a try on your machine and see what happens?
ii_14b05bd27774ef18

William Shaw

unread,
Jan 20, 2015, 12:35:22 AM1/20/15
to wordn...@googlegroups.com
I do have a Windows 7 machine that I guess I'll have to try. It is a desktop. The beauty of using my Windows 8.1 laptop is he convenience of working on this anywhere and especially not in the solitude of the office space. It will be a while, while I rebuild up a working python environment there. FYI - my goal is to write a script that generates really large .docx files (up to 2 gig)  for use with large file upload testing. Words have to be random or docx recognizes it and makes links, keeping the size small, same for inserted graphics.
ii_14b05bd27774ef18

Russell Horton

unread,
Jan 20, 2015, 12:44:48 AM1/20/15
to wordn...@googlegroups.com
I see, that's a really cool use for the API. How random can your words be? Perhaps until the 401 is figured out, you could just use random sequences of characters? Something like:

import string
import random
def random_word(max_letters=12):
    return ''.join( [ random.choice(string.letters) for _ in range(0, random.randint(1, max_letters)) ] )

In [28]: random_word(9)
Out[28]: 'puAf'

In [29]: random_word(9)
Out[29]: 'wIf'

In [30]: random_word(9)
Out[30]: 'KVuxWPIh'

Those would be even harder for Docx to compress than actual random words of English.

William Shaw

unread,
Jan 20, 2015, 8:41:50 AM1/20/15
to wordn...@googlegroups.com
Nice solution. But not as fun!

William Shaw

unread,
Jan 20, 2015, 10:13:51 AM1/20/15
to wordn...@googlegroups.com
Incidently, I took the simple WordApi HTTP request Erin built with my key and ran successfully at your end and ran it from Chrome form a Windows 8 machine at it fails. I took the same request to a Windows 7 machine, chrome, same home network, it works fine. Seems clearly a Windows 8 problem with how I have it configured/not configured. I think at this point, unless I discover an easy Windows 8 solution, I will either use your suggestion or use a .txt file with 100,000 words as the basis of my randomizing. BTW: I don't understand this simple portion of your code:

In [28]: random_word(9)
Out[28]: 'puAf'

On Monday, January 19, 2015 at 11:15:38 AM UTC-6, William Shaw wrote:

William Shaw

unread,
Jan 20, 2015, 10:14:32 AM1/20/15
to wordn...@googlegroups.com
Incidentally, I took the simple WordApi HTTP request Erin built with my key and ran successfully at your end and ran it from Chrome form a Windows 8 machine at it fails. I took the same request to a Windows 7 machine, chrome, same home network, it works fine. Seems clearly a Windows 8 problem with how I have it configured/not configured. I think at this point, unless I discover an easy Windows 8 solution, I will either use your suggestion or use a .txt file with 100,000 words as the basis of my randomizing. BTW: I don't understand this simple portion of your code:

In [28]: random_word(9)
Out[28]: 'puAf'

Russell Horton

unread,
Jan 20, 2015, 11:47:23 PM1/20/15
to wordn...@googlegroups.com
So strange, hopefully that will be fixed with a Windows or Python update or something. Sorry for the frustration.

In that snippet of code, I'm just running the random_word() function I previously defined, and it's showing the output. It's from the iPython repl, which looks a little different from the standard repl (instead of the familiar >>> prompt, it shows In / Out with sequential numbers).

Xiaohua Yan

unread,
Jan 26, 2015, 3:56:41 PM1/26/15
to wordn...@googlegroups.com
I am experiencing the same problem with the getDefinitions call, using Mac OSX.  I am calling the API in a for loop and the call failed after tens of successful calls. Below is the stack trace. Please help:

Traceback (most recent call last):
  File "abbr.py", line 27, in <module>
    definitions = wordApi.getDefinitions(key, sourceDictionaries='all')
  File "/Library/Python/2.7/site-packages/wordnik/WordApi.py", line 182, in getDefinitions
    postData, headerParams)
  File "/Library/Python/2.7/site-packages/wordnik/swagger.py", line 71, in callAPI
    response = urllib2.urlopen(request)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 410, in open
    response = meth(req, response)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized

Erin McKean

unread,
Jan 26, 2015, 4:02:38 PM1/26/15
to wordn...@googlegroups.com
Sorry to hear about this error!

If you are getting a 401 after successful calls, can you add a counter
to make sure you are not going over the call limits?

Erin
> *urllib2.HTTPError: HTTP Error 401: Unauthorized*
>
> My simple script is:
>
> from docx import Document
> document = Document()
> document.add_heading('Document Title',0)
> from wordnik import *
> apiUrl = 'http://api.wordnik.com/v4'
> apiKey ='xxxxxxxxxxxxxxxxxxxx'
> client = swagger.ApiClient(apiKey, apiUrl)
> wordsApi = WordsApi.WordsApi(client)
> randomWordsList = wordsApi.getRandomWords()
> len(randomWordsList)
> #code to add words to docx doc - trying to create very large
> docx file for upload tests
> document.save('demo.docx')
>
> --
> You received this message because you are subscribed to the Google
> Groups "Wordnik API" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to wordnik-api...@googlegroups.com
> <mailto:wordnik-api...@googlegroups.com>.

Xiaohua Yan

unread,
Jan 26, 2015, 4:18:48 PM1/26/15
to wordn...@googlegroups.com
Thanks for the response. The limit I am hitting seems to be 50 (the call fails when the counter reaches 50). Should I pause the execution of the code or what? Isn’t the limit is 15k calls per hour? 
You received this message because you are subscribed to a topic in the Google Groups "Wordnik API" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wordnik-api/66J8VMNYCtw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to wordnik-api...@googlegroups.com.

Erin McKean

unread,
Jan 26, 2015, 4:26:40 PM1/26/15
to wordn...@googlegroups.com
The limit is 15k/hour, yes. So that seems odd ...

We'll poke further!

Thanks!

Erin

On 1/26/15 1:18 PM, Xiaohua Yan wrote:
> Thanks for the response. The limit I am hitting seems to be 50 (the call
> fails when the counter reaches 50). Should I pause the execution of the
> code or what? Isn’t the limit is 15k calls per hour?
>> On Jan 26, 2015, at 1:02 PM, Erin McKean <er...@logocracy.com
>>> an email towordnik-ap...@googlegroups.com
>>> <mailto:wordnik-api...@googlegroups.com>
>>> <mailto:wordnik-api...@googlegroups.com>.
>>> For more options, visithttps://groups.google.com/d/optout.

Xiaohua Yan

unread,
Jan 26, 2015, 4:54:45 PM1/26/15
to wordn...@googlegroups.com
Thanks Erin. I found out the problem. It was because the word I am passing to the API contains "/" character.
>>> For more options, visithttps://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Wordnik API" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/wordnik-api/66J8VMNYCtw/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> wordnik-api...@googlegroups.com
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Wordnik API" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to wordnik-api...@googlegroups.com

Erin McKean

unread,
Jan 26, 2015, 9:27:01 PM1/26/15
to wordn...@googlegroups.com
Great! Thank you so much for letting us know. :-)

On 1/26/15 1:54 PM, Xiaohua Yan wrote:
> Thanks Erin. I found out the problem. It was because the word I am
> passing to the API contains "/" character.
>
> On Monday, January 26, 2015 at 1:26:40 PM UTC-8, Erin McKean wrote:
>
> The limit is 15k/hour, yes. So that seems odd ...
>
> We'll poke further!
>
> Thanks!
>
> Erin
>
> On 1/26/15 1:18 PM, Xiaohua Yan wrote:
> > Thanks for the response. The limit I am hitting seems to be 50
> (the call
> > fails when the counter reaches 50). Should I pause the execution
> of the
> > code or what? Isn’t the limit is 15k calls per hour?
> >> On Jan 26, 2015, at 1:02 PM, Erin McKean <er...@logocracy.com
> <javascript:>
> >>> an email towordnik-ap...@googlegroups.com <javascript:>
> >>> <mailto:wordnik-api...@googlegroups.com <javascript:>>
> >>> <mailto:wordnik-api...@googlegroups.com <javascript:>>.
> >>> For more options, visithttps://groups.google.com/d/optout
> <http://groups.google.com/d/optout>.
> >>
> >> --
> >> You received this message because you are subscribed to a topic
> in the
> >> Google Groups "Wordnik API" group.
> >> To unsubscribe from this topic, visit
> >>
> https://groups.google.com/d/topic/wordnik-api/66J8VMNYCtw/unsubscribe <https://groups.google.com/d/topic/wordnik-api/66J8VMNYCtw/unsubscribe>.
>
> >> To unsubscribe from this group and all its topics, send an email to
> >> wordnik-api...@googlegroups.com <javascript:>
> >> <mailto:wordnik-api...@googlegroups.com <javascript:>>.
> >> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Wordnik API" group.
> > To unsubscribe from this group and stop receiving emails from it,
> send
> > an email to wordnik-api...@googlegroups.com <javascript:>
> > <mailto:wordnik-api...@googlegroups.com <javascript:>>.
> > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Wordnik API" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to wordnik-api...@googlegroups.com
> <mailto:wordnik-api...@googlegroups.com>.
Reply all
Reply to author
Forward
0 new messages