加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 提供的密钥参数不能通过Google API强制转换为私钥

发布时间:2020-12-13 13:11:44 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试测试这个示例,我找到了 here,这样我就可以在客户端进行直接上传,而无需用户使用Google云端存储登录. 表达的所有常量都有正确的值,路径正确且没有空内容. 我得到的错误: openssl_sign(): supplied key param cannot be coerced into a private key
我正在尝试测试这个示例,我找到了 here,这样我就可以在客户端进行直接上传,而无需用户使用Google云端存储登录.

表达的所有常量都有正确的值,路径正确且没有空内容.

我得到的错误:

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

我实现的功能是:

public static function storageURL( $id,$method = 'GET',$duration = 10 ) {
    $key = file_get_contents(self::KEY_FILE);
    $pkey = openssl_get_privatekey($key,'notasecret');
    $expires = time( ) + $duration;
    $content_type = ($method == 'PUT') ? 'application/x-www-form-urlencoded' : ''; 
    $to_sign = ($method . "n" . 
    /* Content-MD5 */ "n" . 
    $content_type . "n" . 
    $expires . "n" . 
    '/'.self::BUCKET_NAME.'/' . $id); 
    $signature = '*Signature will go here*';
    if (!openssl_sign( $to_sign,$signature,$pkey,'sha256' ))
    { 
        error_log( 'openssl_sign failed!' );
        $signature = '<failed>'; 
    } else { 
        $signature = urlencode( base64_encode( $signature ) ); 
    } 
    return ('https://'.self::BUCKET_NAME.'.commondatastorage.googleapis.com/' .
        $id .
        '?GoogleAccessId=' . self::SERVICE_ACCOUNT_NAME .
        '&Expires=' . $expires . '&Signature=' . $signature);
    }
首先,您需要使用openssl_pkcs12_read来读取密钥文件,而不是file_get_contents.其次,我相信你想把第二个参数留给openssl_get_privatekey.

我强烈建议您使用google-api-php-client,这是Google_P12Signer.php

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读