2013-07-26 58 views
1

我正在php中实现云存储API,用于上传mp3文件并加载这些文件。 我想上传没有授权的文件对话框是可能的吗?有没有没有oauth2的样本?未经授权的Google云端存储API可能?

+2

你能成为一个更具体一点你想做什么?您是否想为网站用户提供一个工具来将MP3文件上传到您的存储区,为用户提供一种工具,让用户可以将MP3文件上传到他们自己的存储区,或者为您的程序编写一个系统,以便在没有用户参与的情况下直接上传MP3 ? –

+0

是一种上传和想要使用服务认证的工具,但我无法使用。 – techenthusiast

+0

如果不显示授权对话框至少一次,则无法以用户名上传文件。您可以使用服务帐户通过自己的帐户进行此操作。或者,您可以请求用户授予脱机访问权限,以便您的应用在用户不在场时可以使用用户的文件。 –

回答

2

云存储的一切都需要授权密钥。你可以设置一个服务账号获取一个承载密钥,而不需要用户登录。我修改了一些来自google-api-php-client的代码。提供承载密钥。使用以下内容。

功能getMediaKey(){

/************************************************ 
    Make an API request authenticated with a service 
    account. 
************************************************/ 
set_include_path("../google-api-php-client/src/" . PATH_SEPARATOR . get_include_path()); 
require_once 'Google/Client.php'; 

$client_id = '<your clientid>'; 
$service_account_name = '<serviceaccountemail'; 
$key_file_location = '< location of you privatekey.p12>'; 

//echo pageHeader("Service Account Access"); 
if ($client_id == '<YOUR_CLIENT_ID>' 
    || !strlen($service_account_name) 
    || !strlen($key_file_location)) { 
    echo missingServiceAccountDetailsWarning(); 
} 

$client = new Google_Client(); 

$key = file_get_contents($key_file_location); 
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name, 
    array('https://www.googleapis.com/auth/devstorage.full_control'), 
    $key 
); 
$client->setAssertionCredentials($cred); 
if($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion($cred); 
} 
$bearerKey = $client->getAccessToken(); 

return $bearerKey; 

}

然后你可以使用承载关键发表您的媒体文件。
结账https://developers.google.com/storage/docs/json_api/ 尝试它例子

+0

我会测试这个 – Cristiana214