cloudant-labs / cloudant-python

Asynchronous Cloudant / CouchDB interface for Python
http://cloudant-labs.github.io/cloudant-python/
37 stars 17 forks source link

Using API Keys #43

Open dennismonsewicz opened 10 years ago

dennismonsewicz commented 10 years ago

Can someone give an example on using Cloudant API Keys to connect?

garbados commented 10 years ago
import cloudant

account = cloudant.Account(USERNAME, auth=(API_KEY, API_SECRET))

Let me know if that doesn't work :)

dennismonsewicz commented 10 years ago

The code seems to be working, but when I try to iterate over all the documents in the DB, I am getting this error:

{u'reason': u'_reader access is required for this request', u'error': u'unauthorized'}

The API key I am using is setup to be an admin on the account

dennismonsewicz commented 10 years ago

Another question, I am using the library to incorporate with a CRUD controller in a Flask app...

I am attempting to DRY up some of my code and here is what I have so far:

class WorkflowsCloudant(cloudant.Account):
    def __init__(self):
        super(WorkflowsCloudant, self).__init__(settings.COUCH_DB_ACCOUNT_NAME,
                                                auth=(settings.COUCH_PUBLIC_KEY, settings.COUCH_PRIVATE_KEY))
        self._set_database()

    def _set_database(self):
        return self.database(settings.COUCH_DB_NAME)

@blueprint.route('/<workflow_id>')
def get_single_workflow(account_id, workflow_id):
    account = WorkflowsCloudant()
    # db = account.database(settings.COUCH_DB_NAME)
    doc = account.document(workflow_id)
    resp = doc.get().json()

    if resp['account_id'] != account_id:
        return error_helpers.forbidden('Invalid Account')

    return jsonify(resp)

For some reason when I try to access account.document() the error I am getting is: AttributeError: 'WorkflowsCloudant' object has no attribute 'document'

Sorry for my ignorance, I am a Python newb

janakmayer commented 9 years ago

I have the same issue as dennismonsewicz - I can authenticate using

account = cloudant.Account(USERNAME, auth=(API_KEY, API_SECRET))
db= account.database('my-database')

and I can then read and write individual docs to the db without a problem. However when I try to iterate over all docs with db.all_docs() I get the same error: {u'reason': u'_reader access is required for this request', u'error': u'unauthorized'}

janakmayer commented 9 years ago

I just created a pull request to fix this: https://github.com/cloudant-labs/cloudant-python/pull/53

In the mean time, a solution that works is to pass the authorization tuple directly into the all_docs() method:

auth = (api_key, api_secret)
DB.all_docs(auth=auth)
ayushidalmia commented 8 years ago

On line: account = cloudant.Account(USERNAME, auth=(API_KEY, API_SECRET))

I get the error: AttributeError: 'module' object has no attribute 'Account'

TheBatAss commented 7 years ago

I get the same error as @ayushidalmia, anyone resolved this?

rajlath commented 7 years ago

try this from cloudant.client import Cloudant account = Cloudant(USERNAME, auth=(API_KEY, API_SECRET)