如何使用 STRAPI 向新 API 添加权限不是从仪表板

问题描述

我需要做一个新的 api 才能使用 sendgrid 发送电子邮件。我遵循了官方文档和其他示例,所以我这样做了:

配置/插件

module.exports = ({ env }) => ({
  email: {
    provider: 'sendgrid',providerOptions: {
      apiKey: env('SENDGRID_API_KEY'),},settings: {
      defaultFrom: 'juliasedefdjian@strapi.io',defaultReplyTo: 'juliasedefdjian@strapi.io',});

然后我在 api 文件夹中创建了一个名为 email 的新文件

api/email/config/routes.json

{
  "routes": [
    {
      "method": "POST","path": "/email","handler": "email.index","config": {
        "policies": []
      }
    }
  ]
}

终于在api/email/controllers/email.js

const { default: createStrapi } = require('strapi');

module.exports = {
  index: async (ctx) => {
    //build email with data from ctx.request.body

    await createStrapi.plugins['email'].services.email.send({
      to: 'email@email.com',from: 'email@email.com',replyTo: 'email@email.com',subject: 'test',text: 'test',});
ctx.send('Email sent!');
  },};

真正的问题是 /email api 会返回 403,即使我从仪表板执行此操作:

enter image description here

我用 Strapi 做了很多 API,但我从来没有用它发送过电子邮件

有没有办法从代码添加权限?我不得不说,如果我使用 GET 方法它可以工作,但我需要使用 POST 方法来完成它,但它没有。我错过了什么吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)