I am trying to access some email stats: My code:
import json
import os
from sendgrid.helpers.stats import *
from sendgrid import *
# NOTE: you will need move this file to the root directory of this project to execute properly.
# Assumes you set your environment variable:
# https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key
sg = SendGridAPIClient(os.environ.get('SG****'))
def pprint_json(json_raw):
print(json.dumps(json.loads(json_raw), indent=2, sort_keys=True))
def build_global_stats():
global_stats = Stats()
global_stats.start_date = '2020-01-13'
global_stats.end_date = '2020-01-14'
global_stats.aggregated_by = 'day'
return global_stats.get()
def get_global_stats():
stats_params = build_global_stats()
response = sg.client.stats.get(query_params=stats_params)
print(response.status_code)
print(response.headers)
pprint_json(response.body)
get_global_stats()
Traceback (most recent call last):
File "<ipython-input-10-cee8ef5434a2>", line 35, in <module>
get_global_stats()
File "<ipython-input-10-cee8ef5434a2>", line 29, in get_global_stats
response = sg.client.stats.get(query_params=stats_params)
File "C:\Users\blah\AppData\Local\Continuum\anaconda3\lib\site-packages\python_http_client\client.py", line 262, in http_request
self._make_request(opener, request, timeout=timeout)
File "C:\Users\blah\AppData\Local\Continuum\anaconda3\lib\site-packages\python_http_client\client.py", line 178, in _make_request
raise exc
ForbiddenError: HTTP Error 403: FORBIDDEN
As can be noticed, I get this forbidden error. What is the reason for this?How do i solve this? this is not a blocked site on my end, so not sure why I get this error
For me, it only works after add a new sender on SendGrid and uses this sender on the "from" parameter.
Follow this tutorial:
https://sendgrid.com/docs/ui/sending-email/senders/
then, go to the email registered and confirm the Sender Identity. After that, you should be able to send emails.
You need to edit the privileges for your API key in the SendGrid console here. Click the gear icon to the right of the API key you want to edit, select "Edit API Key" and you can fine-tune the permissions for that key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With