Sending authorised GoogleApiClient to a service - google-drive-api

I have an android application where Im uploading a file to Google Drive. First I load an activity and let a user make some choises, then I start an service that uploads the file in the background. As I understand I need to create the GoogleApiClient in an activity to be able to launch the authorization screens. How Im i supposed to get the then authorised GoogleApiClient to my background service?

You can instantiate the GoogleApiClient in the Activity once in order to ensure that the user has selected an account and authorized your app. Once they have done that once, they shouldn't need to do it again.
At that point you can create a new GoogleApiClient in your service and be fairly confident it will succeed. If it fails, you could always throw up a notification or some such indicating the user needs to take action.
That said, you shouldn't need a background service just to write a file. Since the actual upload will happen in the background already, all you need to do is write to the Contents in an AsyncTask.

I had the same problem...
I solve it by making part of my configuration activity the login to google (check if google play is installed, register and then connect). The activity asks for my google account and password.
My service then don't have problems with connect. I also find that I only have to supply my google login and password once. When I restart my app and I start the configuration activity, it connect to the drive SDK without asking my google login and password

Related

Drive API multi login iframe issue

I am using drive api in my app and it works well , but sometimes when multiple users are logged in and I try to open a file in the browser with the link fetched via drive api I see the error message "You need permission. Want in? Ask the owner for access, or switch to an account with permission. Learn more" .
Steps to reproduce :
login to google drive with one account.
login with a second account and use drive api.
when you try to open public files you get that error.
If you add access token in url it seems to resolve the issue but thats a security risk. If you add authuser param to url that also works, but I can't find a way to get current user's authuser param.
Any help is appreciated.

Document List API to Drive SDK and Client Login

We used to have an application connector implementing the Document List Service v3 to upload documents to users account. Now that the service will be discontinued starting as of next Monday and we need to migrate to the Drive API/SDK we have the problem to migrate our current login schema .. we are unable to use the OAuth 2 protocol and we need to authenticate users with their username/password credentials.
DocumentsService myService = new DocumentsService("xxx");
myService.setUserCredentials(username, password);
The reason is that our application scans and processes documents asynchronously from MFD devices (printers) and all processing/storage job is done in a different moment on processing servers, thus the limitation that the processing service cannot ask any consens to the user.
We do the same for other online cloud storage application (e.g. Dropbox) where they allow special 'OAuth 1' schema on request for such 'enterprise' situations.
How can we do this with the new Drive API/SDK? I couldn't find anything about that in the documentation rather than the service account, also looks like not suitable.
What you need to do is request authentication from you user once. The server gives you back a refresh token. Your automated application can then use this refresh token to get a new access token. You only need to ask the user one time for authentication. Then everything can run automated.
A service account wont really work in this instance because its meant for use with an account that you the developer own not a users account

How to prevent suspended Google account to signup

I am integrating ASP.NET application using Google Drive API. For this after authentication we re uploading Files to Google drive. I am using Google client library to Call the APIs.
Everything is working as expected I am able to authenticate user successfully and able to upload the file successfully.
In one scenario when the user Google account is suspended then I am getting refresh token from Google but my upload method is failing and it is not uploading the file to Google drive.
I want to restrict the user on Signup screen itself, when account is suspended.
What parameter do I have to pass to achieve this please suggest?
Unfortunately this info is not easily available. You have two options :
Use the Directory API to see if the user is suspended. This requires additional OAuth permissions to be provided by an admin of the domain.
At login, try and perform a Drive API call to see if you get an error or not. If you get an error (with a couple of retried) and the error message matches the one you had for suspended users, then you can deny access to the user.

Automate the oauth authentication without browser in cron utility

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,
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
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.
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

User not getting prompted to allow app access to Google Drive

To get a token, I am calling:
GoogleAuthUtil.getToken(context,account, "oauth2:" +"https://www.googleapis.com/auth/drive.appdata");
Which will always give me a token, so my code works, BUT I am expecting(at least the first time) to get a UserRecoverableAuthException so I can use that intent on the exception to prompt the user for action. The action in this case is granting my app access to that user's Google drive storage.
From the web site, I disconnect the app from the drive, run my app, files get written to my gdrive, and I see, from the website, that the app is connected to my drive.
Why is the user not getting prompted to allow access?
If user has already given permissions for you app for that scope, no UserRecoverableAuthException will be thrown.
I discovered in my case the Authorization dialog wasn't been showed because I added singleInstance to the activity in the AndroidManifest.xml
Removing from the AndroidManifest
android:launchMode = "singleInstance"
fixed this problem and a couple of other strange transition effects between activities.
The first time you connect your app to drive it asks for permission by itself when you send the account name.
The Exception you are expecting is only thrown if the user Disconnects the app.
Try disconnecting the app and then read/write to drive. It should throw this Exception.