Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8836117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:25:03+00:00 2026-06-14T09:25:03+00:00

I have been trying to implement a program that uploads backups of my user’s

  • 0

I have been trying to implement a program that uploads backups of my user’s websites to google drive. All of them have an account on my domain, so I went through the steps of granting domain wde delegation of authority for my app as described here: https://developers.google.com/drive/delegation

Unfortunately their sample code to instantiate a drive service object fails on many levels. Here it is:

<?php

require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_DriveService.php";
require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php";
session_start();

$DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
$SERVICE_ACCOUNT_EMAIL = '<some-id>@developer.gserviceaccount.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'privatekey.p12';

/**
 * Build and returns a Drive service object 
 * authorized with the service accounts
 * that acts on behalf of the given user.
 *
 * @param userEmail The email of the user.
 * @return Google_DriveService service object.
 */
function buildService($userEmail) {
  $key = file_get_contents(KEY_FILE);
  $auth = new Google_AssertionCredentials(
      SERVICE_ACCOUNT_EMAIL,
      array(DRIVE_SCOPE),
      $key);
  $auth->setPrn($userEmail);
  $client = new Google_Client();
  $client->setUseObjects(true);
  $client->setAssertionCredentials($auth);
  return new Google_DriveService($client);
}

?>

The first obvious error is they have you set up variables but then the function uses constants. So I hardcoded in what should be there for the constants (KEY_FILE, SERVICE_ACCOUNT_EMAIL, etc) just to see if it worked and then I get the following error:

Fatal error: Call to undefined method Google_AssertionCredentials::setPrn()

Does anyone have any suggestions or comments on how to fix this? If you google these issues, google just gives page after page of links to their own documentation, which as I show above, does not work at all.

Basically I was hoping to see an example of how to use a “service account” which has been granted domain wide access to instantiate a drive service object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:25:04+00:00Added an answer on June 14, 2026 at 9:25 am

    It seems that there are some typos (If we wrote the doc, it should be called bug 🙂 ) in the documentation.

    <?php
    
    require_once "google-api-php-client/src/Google_Client.php";
    require_once "google-api-php-client/src/contrib/Google_DriveService.php";
    require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php";
    session_start();
    
    function buildService($userEmail) {
    
      $DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
      $SERVICE_ACCOUNT_EMAIL = '<some-id>@developer.gserviceaccount.com';
      $SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'privatekey.p12';
    
      $key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
    
      $auth = new Google_AssertionCredentials($SERVICE_ACCOUNT_EMAIL, array($DRIVE_SCOPE), $key); // Changed!
    
      $auth->prn = $userEmail; // Changed!
    
      $client = new Google_Client();
      $client->setUseObjects(true);
      $client->setAssertionCredentials($auth);
      return new Google_DriveService($client);
    }
    
    $service = buildService('email@yourdomain.com');
    
    
    $file = new Google_DriveFile();
    $file->setTitle('My document');
    $file->setDescription('A test document');
    $file->setMimeType('text/plain');
    
    $data = "contents";
    
    $createdFile = $service->files->insert($file, array('data' => $data,'mimeType' =>'text/plain',));
    
    print_r($createdFile);
    
    1. They defined three varivbales but used three three constants- Removed the contsnts and used the variables instead.

    2. There is no method Google_AssertionCredentials::setPrn(). The property prn‘s visibility is public. So you can set it as $auth->prn = $userEmail;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to implement GLSL into my program, however given that i have
I've been trying to program an android that takes allows the user to pull
I have been trying to implement an upgrade plan for my Android app which
I have been trying to implement a GlassPane for my games' in-game HUD, but
Hello guys, I have been trying to implement the DSUM function but failed to
I have been trying for the past two days now to implement Admob ads
I have been having this annoying problem when trying to implement a picture gallery
Im trying to implement an Observer/Observable pattern on an EC2 instance. I have been
Have been trying to encrypt an xml file to a string so that I
I have been trying to understand the follwing program. On server side the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.