获取一个奇怪的错误403禁止通过python访问API
我正在尝试访问一些电子邮件统计信息:我的代码:获取一个奇怪的错误403禁止通过python访问API,python,sendgrid,http-status-code-403,sendgrid-api-v3,Python,Sendgrid,Http Status Code 403,Sendgrid Api V3,我正在尝试访问一些电子邮件统计信息:我的代码: 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: #
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
可以注意到,我犯了这个禁止的错误。原因是什么?我该如何解决?在我这方面,这不是一个被阻止的站点,因此不确定为什么会出现此错误您需要在SendGrid控制台中编辑API密钥的权限。单击要编辑的API密钥右侧的齿轮图标,选择编辑API密钥,您可以微调该密钥的权限。对于我来说,只有在SendGrid上添加新发送者并在from参数上使用此发送者后,它才会起作用 按照本教程操作:
然后,转到已注册的电子邮件并确认发件人身份。之后,您应该能够发送电子邮件。将api密钥的权限更改为full,并在sendgrip web中添加新的发件人电子邮件,并验证发件人电子邮件。然后试试sendmail
此流程对我有效对我来说,我需要将消息正文中的“发件人”字段更新为与sendGrid帐户关联的已验证邮件帐户 使用单发送方身份验证 验证一封电子邮件,例如。hutch@gmail.com
这将允许您从以下地址发送电子邮件:hutch@gmail.com通过SendGrid API密钥错误403表示没有权限。您是否使用Curl尝试了相同的请求?客户端是否需要身份验证或登录?为什么需要更改api密钥权限?但缺少哪个权限?@guival这取决于您尝试执行的操作。什么调用失败了?api v3 send调用,我几乎肯定知道这是因为我使用的是未经验证的电子邮件发送,当我使用我的验证域或已验证的单个发件人时,它可以完美地工作,但是在没有进一步消息的情况下获得403并没有真正的帮助。我还看到很多这样做的例子,人们只是将SendVia sendgrid添加到他们的from中。