deepgram / deepgram-python-sdk

Official Python SDK for Deepgram's automated speech recognition APIs.
https://developers.deepgram.com
MIT License
245 stars 63 forks source link

Python API | Option to add username, secret and key #18

Closed chintanckg closed 2 years ago

chintanckg commented 2 years ago

What is the current behavior?

We consume DG in our production. I have been provided with username, key and secret for testing some features for prod build.

What's happening that seems wrong?

I read the docs, but I don't see examples where I can given key, secret and username. The instantiation example contains api_key and api_url in the params dictionary. By just giving key and url, it throws unautorized exception.

geekchick commented 2 years ago

Hi @chintanckg, I believe you can only provide the api_key and api_url, as we are using Bearer Token Authentication.

Once an application has received an access token, it will include that token as a credential when making API requests. To do so, it should transmit the access token to the API as a Bearer credential in an HTTP Authorization header.

geekchick commented 2 years ago

@chintanckg Also, please note that with Basic Authentication you can use a username and password (key and secret) but we use Bearer Authentication as stated earlier. In our API docs, it states You may embed basic authentication credentials in the callback URL. Here's that reference: https://developers.deepgram.com/api-reference/#authentication

geekchick commented 2 years ago

Another thing to check is to make sure your API key has not expired.

michaeljolley commented 2 years ago

@chintanckg, an important note, you may not be using the latest Deepgram API. If the url you're sending requests to is not https://api.deepgram.com/ then you're likely using the Brain API which allows username/password combinations for authentication. This SDK only supports our newest API that makes requests to https://api.deepgram.com/

chintanckg commented 2 years ago

Basic Authentication worked. Requesting support for the same in the sdk as well, as user will have to write req_body code for brain.deepgram, basic authentication and other ways of authentication. Posting here, the code that worked for me, maybe it will be helpful to other users:

def get_transcription(filename, key_boost_list):
    headers = {}
    headers['Authorization'] = 'Basic {}'.format(
        base64.b64encode('{}:{}'.format(username, password).encode('utf-8')).decode('utf-8'))
    headers['Content-Type'] = 'audio/mpeg'
    data = open(filename, 'rb')
    args = [
        ('punctuate', 'true'),
        ('multichannel', 'true'),
        ('search', ''),
        ('diarize', 'false'),
        ('language', 'en'),
        ('model', 'name_of_subscribed model'),
        ('uttseg', 'true'),
        ('sentiment','true')
    ]
    keywords = []
    if key_boost_list:
        for key in key_boost_list:
            keywords.append(('keywords',key))
    args.extend(keywords)
    resp = requests.request(method='POST', data=data, headers=headers, url=url, params=args)
    print(resp.request.url)
    if resp.ok:
        return (True, resp.json())
    return (False, resp)

Here, username will be the key and password will be the secret.

geekchick commented 2 years ago

Hi @chintanckg thank you for posting your solution! That always helps others as well. We appreciate it! :)