Are you sure you're using the correct credentials? I can't reproduce
the problem:
>>> c = pymongo.Connection('mongodb://user:pass@localhost:27017/foo')
>>> c.foo.bar.insert({'foo': ' bar'}, safe=True)
ObjectId('50492fdb430ee6c7d3c1435f')
>>> c.foo.bar.find_one()
{u'_id': ObjectId('50492fdb430ee6c7d3c1435f'), u'foo': u' bar'}
# Calling logout then find_one here to show that we were actually authenticated.
>>> c.foo.logout()
>>> c.foo.bar.find_one()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pymongo/collection.py", line 516, in find_one
for result in self.find(spec_or_id, *args, **kwargs).limit(-1):
File "pymongo/cursor.py", line 778, in next
if len(self.__data) or self._refresh():
File "pymongo/cursor.py", line 729, in _refresh
self.__uuid_subtype))
File "pymongo/cursor.py", line 686, in __send_message
self.__uuid_subtype)
File "pymongo/helpers.py", line 104, in _unpack_response
error_object["$err"])
pymongo.errors.OperationFailure: database error: unauthorized db:foo
ns:foo.bar lock type:0 client:127.0.0.1
On Thu, Sep 6, 2012 at 3:23 PM, Jorge Puente Sarrín
<
puente...@gmail.com> wrote:
> No, that's not working.
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line
> 306, in insert
> continue_on_error, self.__uuid_subtype), safe)
> File "/usr/local/lib/python2.7/dist-packages/pymongo/connection.py", line
> 745, in _send_message
> rv = self.__check_response_to_last_error(response)
> File "/usr/local/lib/python2.7/dist-packages/pymongo/connection.py", line
> 681, in __check_response_to_last_error
> helpers._check_command_response(error, self.disconnect)
> File "/usr/local/lib/python2.7/dist-packages/pymongo/helpers.py", line
> 129, in _check_command_response
> raise OperationFailure(msg % response["errmsg"])
> pymongo.errors.OperationFailure: need to login
>
> Then, what's the best way for work with a MongoDB Uri and authenticate?
>
> def connect(user, pass):
> yield motor.Op(db.authenticate, user, pass)
>
>
>
> 2012/9/6 A. Jesse Jiryu Davis <
je...@10gen.com>
>
>> I see. Does that work with a regular PyMongo Connection?:
>>
>> db =
>> pymongo.Connection('mongodb://user:pa...@xxxxx.mongolab.com:xxxxx/dbname?safe=true').dbname
>> db.my_collection.insert({}, safe=True)
>>
>>
>> On Thursday, September 6, 2012 6:02:36 PM UTC-4, Jorge Puente Sarrín
>> wrote:
>>>
>>> Hi Jesse,
>>>
>>> I have tried to run your snippet and I think that the problem is
>>> Connection, because for create it, I'm using a Uri such as:
>>>
>>> db =
>>> motor.MotorConnection('mongodb://user:pa...@xxxxx.mongolab.com:xxxxx/dbname?safe=true').open_sync().dbname