尝试访问Microsoft Graph时出现“无法获取本地发行者证书”

时间:2019-06-28 21:43:23

标签: php symfony azure-active-directory microsoft-graph guzzle

我想配置我的Symfony4应用程序以使用msgraph-sdk-php库阅读和发送电子邮件。

我的第一次经验是这段代码:

    $guzzle = new \GuzzleHttp\Client();
    $url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';
    $token = json_decode($guzzle->post($url, [
        'form_params' => [
            'client_id' => $clientId,
            'client_secret' => $clientSecret,
            'resource' => 'https://graph.microsoft.com/',
            'grant_type' => 'client_credentials',
        ],
        'verify' => false
    ])->getBody()->getContents());
    $accessToken = $token->access_token;

    $graph = new Graph();
    $graph->setAccessToken($accessToken);

    try {
        $user = $graph->createRequest("GET", "/me")
            ->setReturnType(User::class)
            ->execute();
    } catch (GraphException $e) {
        $user=(object) ['getGivenName'=>$e->getMessage()];
    }

    return "Hello, I am $user->getGivenName() ";

但随后Symfony向我显示此消息的异常页面:

  

cURL错误60:SSL证书问题:无法获取本地颁发者证书

我该如何克服呢?

1 个答案:

答案 0 :(得分:1)

在Windows系统上,cURL有时无法访问CA证书。因此,您必须下载CA文件并将其添加到Curl。您可以在此处下载证书:

http://curl.haxx.se/docs/caextract.html

curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/certs/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

因此,要暂时解决此问题,您可以禁用对等验证,但您应该仅在测试时这样做。

$client->setDefaultOption('verify', false);

然后应该可以进行连接。要添加证书,您可以执行以下操作,但随后必须先下载证书。

$client = new \GuzzleHttp\Client();
$client->setDefaultOption('verify', 'curl-ca-bundle.crt');

或最后一个解决方案将ca文件添加到您的php.ini(curl.haxx.se中的文件):

curl.cainfo = "[pathtothisfile]\cacert.pem"