禁止使用奇怪的错误403通过python访问API

问题描述 投票:1回答:1

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

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

可以注意到,我收到了这个禁止的错误。这是什么原因?我该如何解决?这不是我阻止的网站,所以不确定为什么会收到此错误

python sendgrid http-status-code-403 sendgrid-api-v3
1个回答
0
投票

您需要在SendGrid控制台here中为您的API密钥编辑特权。单击要编辑的API密钥右侧的齿轮图标,选择“编辑API密钥”,然后可以微调该密钥的权限。

© www.soinside.com 2019 - 2024. All rights reserved.