6

I am trying to upload some documents on Google drive, i want to run a cron script which is executed at mid night every day and the files generated as a result of it should be uploaded on the uses Google drive.

I tried the standalone script, which uploads document on Google drive, but for that i have to every time do allow access via browser.

However my purpose is to run a cron and upload the files, at the time the cron executes there will be no browser access.

Is there any way i can do the authentication process without manual intervention.

any help in this case would be really appreciated.

THanks,

opensource-developer
  • 2,826
  • 4
  • 38
  • 88

3 Answers3

2

you can authorize your App(script) with Google Drive.

Here, you mentioned you are writing a script which upload docs to your Google Drive. I suggest you register a app in Google Cloud Console to get client ID and client Secret firstly, and turn on Drive API for you registered App.

Then use this client ID and Secret to run oauth flow in your script to get an access token and refresh token, the access token's lifespan is about 3600s, and if it's expired, you can also get a new one with the refresh token.

User's interaction(consent) is required only in the first time you request access token.

In this way, your script can work in "a real script way".

Here are some reference: https://developers.google.com/drive/about-auth https://developers.google.com/accounts/docs/OAuth2InstalledApp?hl=zh-CN

Owen Cao
  • 7,955
  • 2
  • 27
  • 35
  • Hi Thanks for your prompt reply, i have registered the app under the cloud console. I have the client id and the Secret key as well. I am unsure how to use the refresh token. below is the code i have $service = new Google_DriveService($client); if($client->isAccessTokenExpired()) { $client->authenticate(); $NewAccessToken = json_decode($client->getAccessToken()); $client->refreshToken($NewAccessToken->refresh_token); } // INSERT FILE – opensource-developer Nov 27 '13 at 13:30
  • I also tried the use of set accesstoken method like this, $client->setAccessToken('{ "access_token" : "ya29.1.AADtN_UffmvD-9MPhjIUKtXyhNOP-IwgvLh48nOSFRIKvC_xdXCXO-Eflb2MY0A-I3RNtQ", "token_type" : "Bearer", "expires_in" : 3600, "refresh_token" : "1/d8x8099NpHVepjut--pYcrGJ8Ebnz13zw_KpIaXy8OI" }'); But its giving me error as invalid client and bad request – opensource-developer Nov 27 '13 at 13:52
  • about the details, hope [Adding Files to Google Drive using PHP](http://hublog.hubmed.org/archives/001954.html) and [Google Client API with PHP](http://enarion.net/programming/php/google-client-api/google-client-api-php/)can help you – Owen Cao Nov 27 '13 at 14:02
1

I am assuming its only one user drive account you are uploading to. Have you considered using a service account fo this? https://developers.google.com/drive/service-accounts

If its not a single user account you are uploading you can just save the refresh token some place and use that to get a new authtoken every night.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Hi I have the refresh token, when i try to generate auth token from it, i get the error as Error refreshing oauth2 token error : unauthorized client. $auth_code = $client->refreshToken("1/3EZsybCh1jolk4YMHyQ6LgpiE_zlb4u9R6f0KqjGXMA"); Below is the code that iam using, $accessToken = $client->authenticate($auth_code); Please let me know if this is wrong or how can i best use refresh token. Thanks in advance – opensource-developer Nov 27 '13 at 14:27
  • You only autenticate once and thats when your user says yes. You want to excange the refresh token for an access token. try this. $client->refreshToken("1/3EZsybCh1jolk4YMHyQ6LgpiE_zlb4u9R6f0KqjGXMA"); $token = $client->getAccessToken(); – Linda Lawton - DaImTo Nov 27 '13 at 14:33
  • Its giving me same error below is my code $client = new Google_Client(); // Get your credentials from the console $client->setClientId('440456931204-q2omnu8fh75n4s4111h503nav60h5gpf.apps.googleusercontent.com'); $client->setClientSecret('eBIztfMNhaBtyMZ8qDMVN7vk'); $client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob'); $client->setScopes(array('https://www.googleapis.com/auth/drive')); $service = new Google_DriveService($client); $auth_code = $client->refreshToken("1/3EZsybCh1jolk4YMHyQ6LgpiE_zlb4u9R6f0KqjGXMA"); $token = $client->getAccessToken(); //Insert a file – opensource-developer Nov 27 '13 at 14:44
  • have you checked what token is returning? – Linda Lawton - DaImTo Nov 27 '13 at 14:47
  • Hi i figured it i was generating refersh token from oauthplayground and was trying to integrate it in the php script....which was invalid...... I then pritned the refresh token returned for the first time and now iam using it and it works like a charm.. Thanks – opensource-developer Nov 27 '13 at 15:10
1

Hi i figured the problem,

i was generating refresh token from oauthplayground and was trying to integrate it in the php script....which was invalid......

I then printed the refresh token returned for the first time when a user allows access and used that for future generation of access token and it works like charm. thanks All for help

opensource-developer
  • 2,826
  • 4
  • 38
  • 88
  • 1
    You *can* use the Oauth Playground, but you need to enter your own client ID. See http://stackoverflow.com/questions/19766912/how-do-i-authorise-a-background-web-app-without-user-intervention-canonical – pinoyyid Nov 27 '13 at 15:16