Learner
Learner

Reputation: 711

Google Cloud Storage API Without Authorization is Possible?

I am implementing cloud storage api in php for uploading mp3 files and loading those in site. I want to upload the files without authorization dialog is that possible? is there any sample without oauth2?

Upvotes: 1

Views: 478

Answers (1)

Jeremy McKay
Jeremy McKay

Reputation: 41

Everything going to cloud storage needs auth key. You can set up a service account get a bearer key without the user logging in. I modified some code from google-api-php-client. To provide the bearer key. Using the following.

function 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;

}

Then you can post your media files using the bearer key.
checkout https://developers.google.com/storage/docs/json_api/ Try it for examples

Upvotes: 2

Related Questions