Trouble with google drive - android

I want to create an application for browsing all files in google drive that are located in root folder. The problem is I can set up permissions only for creating and browsing files. But I have to browse the files which I upload in google drive via browser.
#Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
mGoogleApiClient.connect();
}

To retrieve files from the root folder use Drive.getRootFolder method.
The getRootFolder will returns a DriveFolder and it can be used to interact with the root folder. This method will return synchronously, and is safe to invoke from the UI thread.
DriveFolder folder = Drive.DriveApi.getFolder(getGoogleApiClient(), result.getDriveId());
folder.listChildren(getGoogleApiClient())
.setResultCallback(metadataResult);
public static final Scope SCOPE_FILE a Scope that gives 'drive.file' access to a user's drive. This scope give per-file access to files that have been created by, or specifically opened with the app.
The Scope implements SafeParcelable which describes an OAuth 2.0 scope to request. This has security implications for the user, and requesting additional scopes will result in authorization dialogs.
Here's a sample demo app, demo app illustrates all possible ways to talk to Drive service with the use of interfaces available in Google Play Services: https://github.com/googledrive/android-demos/

Related

Google Drive V3 Rest API Play Video Thru Drive App

I currently have 4 accounts connected to the Google Drive app on my phone.
I am trying to play a .mp4 file from Google Drive using the following:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(file.getFile().getWebContentLink())));
When I select the Google Drive app to play the file, I am presented with a list of the 4 currently connected accounts.
I already know which account the file is from. Is there any way of passing this account to the the startActivity, maybe as part of the intent?
Try below code it may work and let me know it worked for you or not.:)
#Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.setAccountName("yourmaild#gmail.com")
.addScope(Drive.SCOPE_FILE)
.addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
mGoogleApiClient.connect();
}

Create folder on Google drive (Android)

i need to access the google drive storage of logged in user. For now i can fetch his name and his Email, Cover picture and Avatar. I can even ask for accessing the drive storage. But i can't get it managed to create files/folders on Gdrive.
The developers page is giving me this code to create folder:
https://developers.google.com/drive/v3/web/folder
File fileMetadata = new File();
fileMetadata.setName("Invoices");
fileMetadata.setMimeType("application/vnd.google-apps.folder");
File file = driveService.files().create(fileMetadata)
.setFields("id")
.execute();
System.out.println("Folder ID: " + file.getId());
But i am sure it can't just be that easy.
fileMetadata.setName
can not be resolved for me. I am sure i am missing something.
This is what i do while loading my GoogleApiClient
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(Plus.API, Plus.PlusOptions.builder().build())
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
I don't want to use hidden app folder, instead, i want that user see the app folder, and could easy see what's inside. I have a button, and when user click on it, specific file that is saved locally should be uploaded to drive. Later on i will add maybe auto sync, but for now manual upload would be enough to understand. Thanks!!!
Any help is welcome.
In Drive API for Android, note that working with folders has slight differences when compared to Google Drive API. Folders in the Drive API for Android are specialized resources within metadata and a DriveId and to create a folder, call DriveFolder.createFolder for the root folder. Then, pass the metadata containing the title and other attributes to set the values for the folder.
For a full working example, see the CreateFolderActivity sample in the Google Drive Android API Demos app.

How to remove the “Have offline access” permission on Android using oAuth

I need to remove the “Have offline access” permission on my Android app.
In my application, when I click in sign me with Google it is showing me this:
I don't want that message
I'm using "GoogleApiClient" like this for the initialization:
// Build GoogleApiClient with access to basic profile
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.addScope(Plus.SCOPE_PLUS_PROFILE)
.build();
I search over internet, but I can't found how to do that in Android. I only found information about Web Server Applications and the approval_prompt parameter.
Someone have any idea of how to do that on Android?
Thanks and sorry for my poor English
That is the scope of permissions your application is requesting. Users need to know what permissions you want to access If you want to remove that you will have to remove
.addScope(Plus.SCOPE_PLUS_LOGIN)
.addScope(Plus.SCOPE_PLUS_PROFILE)
But then you will not be able to access the users account either.

Google Drive scopes deprecated?

I'm writing an app that would play media straight off Google Drive.
Google docs mention
https://www.googleapis.com/auth/drive.readonly
scope (see here: https://developers.google.com/drive/web/scopes) that seems exactly to be what I need. Yet, it seems that Android drive API supports only two of scopes mentioned there:
Drive.SCOPE_FILE
Drive.SCOPE_APPFOLDER
so I've tried several non-standard scopes like this:
mGoogleApiClient = new GoogleApiClient.Builder(XenoAmp.getContext())
.addApi(Drive.API)
//.addApi(DriveScopes.)
//.addScope(Drive.SCOPE_APPFOLDER)
.addScope(Drive.SCOPE_FILE)
.addScope(new Scope("https://www.googleapis.com/auth/drive"))
//.addScope(new Scope("https://www.googleapis.com/auth/drive.readonly"))
//.addScope(new Scope("https://www.googleapis.com/auth/drive.metadata.readonly"))
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
Yet my logcat complains:
03-29 12:53:58.935: E/ClientConnectionOperation(9953): com.google.android.gms.drive.auth.c: Authorization failed: Unsupported scope: https://www.googleapis.com/auth/drive
So - is drive scope no more supported? How can I get access to all user media files on Goodle Drive then?
Yes, the DRIVE scope is not supported in GDAA. If you need it, you have to go with the REST API. GDAA is not a 'newer' API, it is a different API. Both will be around for a while. You can compare these two here.

Uploading a CSV file as Google Sheet using Android SDK

I have an Android app that allows users to export a .csv file to Google Drive so that they can edit it and then reimport the file into Android.
This used to work perfectly with the old Google Docs api's. I upgraded from that old API to an early version of Google Drive last year and was able to get it working well enough. Now, when I upgraded to the latest version, this feature of my app is effectively broken. I think it's a combination of the SDK permissions and breaking out Google Sheets from Google Drive.
What happens is I upload a file with this meta data:
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(fileName)
.setMimeType("text/csv")
.setStarred(true)
.build();
The uploaded file then has a blue Google Docs icon. When a user access Google Drive to edit it they can only "preview" the document. While they are previewing it they can "Open" the document using Google Sheets which then creates a new document (this new document has the Green Google Sheets icon) and since Google SDK has this new "Feature" (quoted from: https://developers.google.com/drive/android/queries ):
Note: The Android Drive API only works with the
https://www.googleapis.com/auth/drive.file scope. This means that only
files which a user has opened or created with your application can be
matched by a query.
The new file has can not be seen by my, so the user's edits can't be retrieved.
Is there someway I can upload it directly as a Google Sheets file? Or maybe another solution I've missed entirely?
Edits
Here is an example on how I am uploading my code using the Google Drive SDK:
In the constructor of my AsyncTask I create the google client:
mGoogleApiClient = new GoogleApiClient.Builder(activity)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
Then in the doInBackground method I do something like this:
DriveApi.DriveContentsResult cResult = Drive.DriveApi.newDriveContents(mGoogleApiClient).await();
OutputStream os = cResult.getDriveContents().getOutputStream();
InputStream is = new FileInputStream(f);
//write the input stream to the output stream
....
DriveFileResult exportResult = Drive.DriveApi.getRootFolder(mGoogleApiClient)
.createFile(mGoogleApiClient, changeSet, cResult.getDriveContents())
.await();
It's pretty simple (once you figure it out) there just doesn't seem to be away to tell the SDK that I want this document to be a "Google Sheet" instead of a "Google Doc"
Thanks!
-Aaron
You should convert the original explicitly into a Spreadsheet, otherwise it will end up as a straight upload into your Drive account. Try the following code:
java.io.File fileContent = new java.io.File(csvfilepath);
FileContent mediaContent = new FileContent("text/csv", fileContent);
// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/csv");
Insert request = service.files().insert(body, mediaContent);
request.setConvert(true);
File file = request.execute();

Categories

Resources