4

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

5 Answers5

9

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.

José Renato
  • 136
  • 2
  • 5
4

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.

That1Guy
  • 7,075
  • 4
  • 47
  • 59
  • Which permission is missing though? – gdvalderrama Nov 30 '20 at 12:44
  • @guival It depends on what you're trying to do. What call is failing for you? – That1Guy Nov 30 '20 at 14:29
  • the api v3 send call, I almost definitely know it's because I'm using an unverified from email (sending works perfectly when I use my verified domain or verified single senders), but getting a 403 with no further message doesn't really help. Also I see a lot of examples of people doing this and just having a "sent via sendgrid" added to their from. – gdvalderrama Dec 01 '20 at 10:41
2

Change the permission of the api key to full and and add a new sender email in sendgrip web and verify the sender email. then try the sendmail().

this flow worked for me

santhoshc
  • 29
  • 5
1

For me I needed to update the From field in the message body to the verified ail account associated with the sendGrid account.

Using Single Sender Authentication verify a single email e.g. hutch@gmail.com Which will then allow you to send out emails from: hutch@gmail.com via your SendGrid API KEY

Hutch
  • 411
  • 10
  • 32
0

I got this rectified by Verifying the sender. Once triggered the verification from Settings > Verify a Single Sender. The From mailId will get a verification email from SendGrid, simple verification is sufficient to get it reflected as directed in email.

Once verified, you will see an Verified icon, else otherwise.

enter image description here

pjoshi
  • 249
  • 3
  • 7