question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

a bytes-like object is required, not 'str'

See original GitHub issue

There is a issue in analytics.py. Traceback (most recent call last): File ".\api.py", line 60, in <module> async_data.append(LineItem.async_stats_job_data(account, url=result.url)) File "D:\Office\twitter ads\env\lib\site-packages\twitter_ads\analytics.py", line 115, in async_stats_job_data response = Request(account.client, 'get', resource.path, domain=domain, File "D:\Office\twitter ads\env\lib\site-packages\twitter_ads\http.py", line 70, in perform raise Error.from_response(response) File "D:\Office\twitter ads\env\lib\site-packages\twitter_ads\error.py", line 45, in from_response return ERRORS[response.code](response) File "D:\Office\twitter ads\env\lib\site-packages\twitter_ads\error.py", line 13, in __init__ if response.body and 'errors' in response.body: TypeError: a bytes-like object is required, not 'str'

This error is caused when running the analytics.py file. Please fix this issue.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:7

github_iconTop GitHub Comments

1reaction
pep4eto1211commented, Aug 10, 2022

Just posting an update here as it seems none of the developers are actually looking at these issues. The error we are seeing is not indicating the actual fault. Rather it is the library failing to provide additional information about the real error. Basically their code that is supposed to show what the error is, is failing and this is its error. As such, this means that any failure might have happened to cause this. As a workaround I found that you can print the response’s HTTP status code here: https://github.com/twitterdev/twitter-python-ads-sdk/blob/a3dd5819341e77aa469d0b4b3399f0bcd028c80c/twitter_ads/http.py#L69 by reading the response.code property. This might at least point you in the right direction. Unfortunately I was unable to find a way to also print the response body, as it is а bytes object and I don’t know what the encoding is. In addition: my error was 403- which is extremely weird, considering I was able to paste the result’s URL and download the generated report just fine. Download even works in incognito mode- making me thing that the file downlead requires no authentication whatsoever (also seen in @rafabandoni’s comment). Overall the code quality for this library is extremely low and the only reason I keep using it is because it handles the cumbersome auth. @rafabandoni I actually found a way to do checks if the report generation is complete. Using the job ID, you can retrieve the job using the async_stats_job_result function. This object has a status property:

{
    "id": "*****",
    "status": "SUCCESS",
    "url": "*****",
    "created_at": "2022-08-10T10:39:25Z",
    "expires_at": "2022-08-12T10:39:38Z",
    "updated_at": "2022-08-10T10:39:38Z",
    "start_time": "2022-06-21T04:00:00Z",
    "end_time": "2022-06-22T04:00:00Z",
    "entity": "CAMPAIGN",
    "entity_ids": [
        "*****"
    ],
    "placement": "ALL_ON_TWITTER",
    "granularity": "DAY",
    "metric_groups": [
        "ENGAGEMENT",
        "BILLING"
    ]
}

A simple while with some waiting time and periodic checking should be a better way to do this than waiting an arbitrary amount of time.

These were the changes I made to get the proper data from the url after the sleep:

Thanks for this- I’ll make use of it.

1reaction
rafabandonicommented, Aug 2, 2022

Is there a more reliable way to find out if a report is ready or not? The sleep method looks way too “hacky” for me.

Idk 😦 These were the changes I made to get the proper data from the url after the sleep:

async_data = []
for result in async_stats_job_results:
  print(result)
  url = result.url
  #async_data.append(LineItem.async_stats_job_data(account, url=result.url))
  
response = requests.get(url)
data = gzip.decompress(response.content)

data = json.loads(data.decode('utf-8'))
Read more comments on GitHub >

github_iconTop Results From Across the Web

a bytes-like object is required, not 'str' when writing to a file in ...
You cannot write a string to a file, you need to encode the string to a byte-like object to do so. By running...
Read more >
typeerror: a bytes-like object is required, not 'str' - STechies
This is a very common type of error faced by programmers while coding in Python. The typeerror occurs when there is a mismatch...
Read more >
Python typeerror: a bytes-like object is required, not 'str' Solution
The error “a bytes-like object is required, not 'str'” tells us that we've tried to access an object as if it were a...
Read more >
How to Fix Typeerror a bytes-like object is required not 'str'
This error is mainly caused by passing the wrong type to a function. Therefore if a function is expecting the bytes object, we...
Read more >
Typeerror a bytes like object is required not str : How to Fix?
Typeerror a bytes like object is required not str error occurs when we compare any 'str' object with the 'byte' type object. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found