python: TypeError: 'coroutine' object is not subscriptable

Question:

i keep getting an exception in this function when it trys to subscript the json object from the anticaptcha function, but all the other functions seem to be working fine

'email':email, 'username': username, 'password': passwd, 'invite': None, 'captcha_key': await anticaptcha.solve(session, 'register')['solution']['gRecaptchaResponse']
TypeError: 'coroutine' object is not subscriptable

async def register(session, username, email, passwd):
    """
    sends a request to create an account
    """
    async with session.post('http://randomsite.com',
                            headers={
                                'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0'
                            },
                            json={
                                'email':email, 'username': username, 'password': passwd, 'invite': None, 'captcha_key': await anticaptcha.solve(session, 'register')['solution']['gRecaptchaResponse']
                            }) as response:
            return await response.json()

the functions from the anticaptcha file

async def task_result(session, task_id):
    """
    sends a request to return a specified captcha task result
    """
    while True:
        async with session.post('https://api.anti-captcha.com/getTaskResult',
                                json={
                                    "clientKey": ANTICAPTCHA_KEY,
                                    "taskId": task_id
                                }) as response:
            result = await response.json()
            if result['errorId'] > 0:
                print(colored('Anti-captcha error: ' + result['statusCode']), 'red')
            else:
                if result['status'] == 'ready':
                    return await result

async def solve(session, url):
   await get_balance(session)
   task_id = await create_task(session, url)['taskId']
   return await task_result(session, task_id)
Asked By: user9938946

||

Answers:

await anticaptcha.solve(session, 'register')['solution']['gRecaptchaResponse']

means

await (anticaptcha.solve(session, 'register')['solution']['gRecaptchaResponse'])

but you want

(await anticaptcha.solve(session, 'register'))['solution']['gRecaptchaResponse']

If the other similar thing, task_id = await create_task(session, url)['taskId'], is working, it probably doesn’t return a future and you can just set

task = create_task(session, url)['taskId']

without await.

Answered By: Ry-

An alternative to the accepted method is one line longer, but adds a bit of clarity.

x = await anticaptcha.solve(session, 'register')    # first do the await
x = x['solution']['gRecaptchaResponse']             # then get the dict values

This above clearly shows what happens. When i first came across the same, i found the above helpful for clarity (in place of commenting the code).

Answered By: D.L
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.