gpt4 book ai didi

python - 通过python访问API时出现403禁止的奇怪错误

转载 作者:行者123 更新时间:2023-12-04 00:22:14 24 4
gpt4 key购买 nike

我正在尝试访问一些电子邮件统计信息:我的代码:

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 上添加一个新的发件人后才有效,并在“ from ”参数上使用这个发件人。

按照本教程:

https://sendgrid.com/docs/ui/sending-email/senders/



然后,转到注册的电子邮件并确认发件人身份。之后,您应该能够发送电子邮件。

关于python - 通过python访问API时出现403禁止的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59739152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com