2014-01-15 21 views
8

我試圖測試此示例,我發現here以便我可以在客戶端直接上傳,而無需用戶使用Google雲端存儲進行登錄。提供的關鍵參數不能強制轉換爲使用Google API的私鑰

所有表達的常量都有它們的正確值,路徑是正確的,沒有空的內容。

我得到的錯誤:

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); 
    } 

回答

2

首先,你需要使用openssl_pkcs12_read讀取密鑰文件,而不是file_get_contents。其次,我相信你想把第二個參數設置爲openssl_get_privatekey

我強烈建議你使用google-api-php-client這一點,其中有Google_P12Signer.php

+0

這樣我就可以只使用這生成URL的? – RyanDawkins

+0

什麼是「這個」?如果你的意思是google-api-php-client,那麼是的 – jterrace

+0

哦......你在哪裏找到api-library的文檔? – RyanDawkins

0

更安全的方法是使用Google_Signer_P12類隨Google API PHP client做到這一點。

代碼示例:

set_include_path(get_include_path() . PATH_SEPARATOR . 'PATH/TO/API/src'); 
require_once 'Google/Signer/P12.php'; 

$p12contents = file_get_contents('FILE.p12'); 
$googleSigner = new Google_Signer_P12($p12contents, 'notasecret'); 
$signature = $googleSigner->sign($data); 
+0

google_signer_p12在新版本中被刪除,代之以使用什麼? –

相關問題