sendgrid未经授权。错误的凭证-PHP

时间:2020-08-25 10:25:35

标签: php sendgrid

我正在尝试将sendgrid实现到我的项目中,并且我有一个运行以下内容的基本功能:

@override
Stream<NumberTriviaState> mapEventToState(NumberTriviaEvent event,
  ) async* {
  if (event is GetTriviaForConcreteNumberEvent) {
    final inputEither =
      inputConverter.stringToUnsignedInteger(event.numberString);

    yield* inputEither.fold(
      (failure) async* {
        yield Error(message: invalidInput);
      },
      (integer) async* {
        yield null;
      },
    ); 
  }
}

我收到错误“权限被拒绝:凭据错误”。我设置了环境变量,我尝试将键的值放在引号和不引号之间。它也位于我的sendgrid.env文件(gitignored)中,如下行

require '../vendor/autoload.php'; // If you're using Composer (recommended)
        // Comment out the above line if not using Composer
        // require("<PATH TO>/sendgrid-php.php");
        // If not using Composer, uncomment the above line and
        // download sendgrid-php.zip from the latest release here,
        // replacing <PATH TO> with the path to the sendgrid-php.php file,
        // which is included in the download:
        // https://github.com/sendgrid/sendgrid-php/releases
        $email = new \SendGrid\Mail\Mail();
        $email->setFrom("test@example.com", "Example User");
        $email->setSubject("Sending with SendGrid is Fun");
        $email->addTo("tiernoyt@gmail.com", "Example User");
        $email->addContent("text/plain", "and easy to do anywhere, even with PHP");
        $email->addContent(
            "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
        );
        $sendgrid = new \SendGrid($_ENV['SENDGRID_API_KEY']);
        try {
            $response = $sendgrid->send($email);
            print $response->statusCode() . "\n";
            print_r($response->headers());
            print $response->body() . "\n";
        } catch (Exception $e) {
            echo 'Caught exception: '. $e->getMessage() ."\n";
        }
      }

如果我将代码更改为export SENDGRID_API_KEY='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' ,那么它将起作用。因此,显然,获取变量是一个错误,但是我不确定为什么

0 个答案:

没有答案