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

问题描述

我正在尝试将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' ,那么它将起作用。因此,显然,获取变量是一个错误,但是我不确定为什么

解决方法

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

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

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