首页 > 解决方案 > 提供的密钥参数不能强制转换为私钥

问题描述

我正在尝试使用 JWT Bearer 按照此处的步骤操作 OAuth2,我已包含安装说明中所述的相应文件。

但是,当我运行以下 PHP 代码时:

$private_key = file_get_contents("/www/var/includes/keyfile.pem");
$client_id   = 'example_client_id';
$user_id     = 'example_user_id';
$grant_type  = 'urn:ietf:params:oauth:grant-type:jwt-bearer';
$jwt = generateJWT($private_key, $client_id, $user_id, 'https://api.example.com');

...我收到以下错误:

openssl_sign(): supplied key param cannot be coerced into a private key

Uncaught exception 'Exception' with message 'Unable to sign data.'

我读过使用'file_get_contents'可能不是最好的方法,尝试过其他方法没有运气。第一个链接中的文档特别说要使用它,这似乎很奇怪?

标签: phpoauth-2.0jwt

解决方案


  1. 准备好“私钥id”:</li>
   $fp = fopen("/path/to/file/privatekey.pem", "r");
   
   $privKey = fread($fp, 8192);
   
   fclose($fp);

   $pKeyId = openssl_get_privatekey($privKey, 'optional_passphrase');

  1. 在 openssl_sign 中使用:


<code>openssl_sign($dataToSign, $signatureVar, $pKeyId);

信用:https ://rupom.wordpress.com/2015/09/19/openssl_sign-supplied-key-param-cannot-be-coerced-into-a-private-key/


推荐阅读