简体   繁体   English

通过 python 访问 API 得到一个奇怪的错误 403 禁止

[英]Getting a strange error 403 forbidden for accessing an API through python

I am trying to access some email stats: My code:我正在尝试访问一些 email 统计信息: 我的代码:

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

Follow this tutorial:按照本教程:

https://sendgrid.com/docs/ui/sending-email/senders/ 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 .您需要在此处的 SendGrid 控制台中编辑 API 密钥的权限。 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.单击要编辑的 API 密钥右侧的齿轮图标,选择“编辑 API 密钥”,您可以微调该密钥的权限。

Change the permission of the api key to full and and add a new sender email in sendgrip web and verify the sender email.将 api 密钥的权限更改为 full 并在 sendgrip web 中添加新的发件人电子邮件并验证发件人电子邮件。 then try the sendmail().然后尝试 sendmail()。

this flow worked for me这个流程对我有用

For me I needed to update the From field in the message body to the verified ail account associated with the sendGrid account.对我来说,我需要将消息正文中的 From 字段更新为与 sendGrid 帐户关联的经过验证的 ail 帐户。

Using Single Sender Authentication verify a single email eg hutch@gmail.com Which will then allow you to send out emails from: hutch@gmail.com via your SendGrid API KEY使用单一发件人身份验证验证单个电子邮件,例如 hutch@gmail.com 这将允许您通过您的 SendGrid API KEY 从 hutch@gmail.com 发送电子邮件

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. From mailId 将从 SendGrid 收到一封验证电子邮件,简单的验证足以让它按照电子邮件中的指示反映出来。

Once verified, you will see an Verified icon, else otherwise.验证后,您将看到“已验证”图标,否则。

在此处输入图片说明

I had the same problem and it also worked for me after I created a new (the second) sender and verified it.我遇到了同样的问题,并且在我创建了一个新的(第二个)发件人并对其进行验证后,它也对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访问发布/订阅时出现 403 错误 - Getting 403 error while accessing pubs/sub 访问 firebase 用户的 photoURL 时出现 http 403 错误 - Getting http 403 error while accessing photoURL of a firebase User 尝试从 Firebase 存储加载图像时出现 403 禁止错误 - Getting 403 Forbidden error when trying to load image from Firebase Storage Gitlab v4 api 403 project.variables.list 禁止错误(get_all=True) - Gitlab v4 api 403 forbidden error for project.variables.list(get_all=True) 使用签名 URL 上传到 S3 时获取 403(禁止访问) - Getting 403 (Forbidden) when uploading to S3 with a signed URL 打开我的 Cloud Run 服务的 URL 时出现 403“错误:禁止” - 403 "Error: Forbidden" when opening the URL of my Cloud Run service 尝试创建子用户时 sendgrid 错误“403 访问被禁止” - sendgrid error "403 access forbidden" when trying to create sub user GitLab:将项目转移到组显示403禁止错误 - GitLab: Transferring project to Group shows 403 Forbidden error 无法使用 HTTP 在本地测试 Sendgrid 错误 403:禁止 - Unable to test Sendgrid locally with HTTP Error 403: Forbidden Elasticsearch::Transport::Transport::Errors::Forbidden: [403] 发送超过 1200 个字符的文本字段时出错 - Elasticsearch::Transport::Transport::Errors::Forbidden: [403] error when sending text fields with more than 1200 characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM