Android Code Snippet GoogleDrive via REST API V3 - Small Testproject

fredo

Well-Known Member
Licensed User
Attached is a small project to get a grip on Google Drive.

Most work was done by mw71, but this brings it all together from scattered informations to a usable entrypoint.

' ~~~-- ~~~-- ~~~-- ~~~-- ~~~-- ~~~-- ~~~-- ~~~-- ~~~--
' Previous Info sources on B4X (sorted by date DEC)
' ~~~-- ~~~-- ~~~-- ~~~-- ~~~-- ~~~-- ~~~-- ~~~-- ~~~--
' 2018-06-01, Erel, OAUTH2, v2.10 --> https://www.b4x.com/android/forum/threads/class-b4x-google-oauth2.79426/#content
' 2017-08-26, mw71, testconnect, --> https://www.b4x.com/android/forum/threads/problems-with-oauth.80174/page-2#post-526872'
' 2017-08-18, mw71, googledrive_playing --> https://www.b4x.com/android/forum/threads/problems-with-oauth.80174/#post-524978
' 2017-06-20, mw71, Testconnect --> https://www.b4x.com/android/forum/threads/googledrive-via-api-v3.80775/#post-512176
' 2017-06-18, mw71, GDrive via APIv3 --> https://www.b4x.com/android/forum/threads/googledrive-via-api-v3.80775/
' 2017-06-04, mw71, upload file --> https://www.b4x.com/android/forum/threads/http-patch-request.80261/#post-508444

Libraries:
03-08-_2018_17-45-57.jpg
Modules:
03-08-_2018_17-46-33.jpg

HttJob: version 2.01
HttUtilsService: version 2.10 (based on OkHttp library)

Manifest:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
AddActivityText(Main,
  <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="$PACKAGE$" />
    </intent-filter>
    )
Notes:
1. The targetsdk is not yet set to 26
2. The module GoogleOauth2 contains "Sub Testconnect" from mw71
3. Read and understand the project specific setup below

Create a new project in the Google AP developer console
04-08-_2018_13-05-22.jpg
Select "Google Drive API" in the dashboard
04-08-_2018_13-09-40.jpg
Enable the API
04-08-_2018_13-11-49.jpg
Create credentials
04-08-_2018_13-14-25.jpg
As alerted by (1) on first entry you may create the credentials via button (2) right now. It is recommended to create credentials via button (3)
04-08-_2018_13-55-29.jpg 04-08-_2018_13-55-36.jpg 04-08-_2018_13-55-49.jpg

You need to fill in "SHA-1" and "Package name". Both are available in your B4A IDE:
04-08-_2018_13-29-14.jpg 04-08-_2018_13-34-38.jpg
Then receive your client ID:
04-08-_2018_13-51-02.jpg

The program sequence is as follows:
1. "GoogleDrive" is initialized in Main with the "ClientId" for Oauth2
1.1 "GoogleDrive" initializes "GoogleOauth2" and informs about the "Scope= ....drive"​
2. Main calls "ConnectToDrive" and waits for message from "GD_Connected".
2.1 "GoogleDrive" requests an AccessToken via "GoogleOauth2"
2.2 "GoogleDrive" executes a "TestConnect" and reports via "GD_Connected" to waiting Main​
3. Main displays the AccessToken in Label1​
With the AccessToken several actions can be can performed with the drive:
https://developers.google.com/drive/api/v3/reference/files#methods
If you need a "ClientSecret" at some point create a new OAuth2 ClientId with the ApplicationType "Web application"

B4X:
' ------------------------------------------------------------------------------
Sub btnStart_Click As ResumableSub
   Log("#-btnStart_Click -----------------------------------")
   Label1.Text = "(Trying to connect to Google Drive...)"
   GD.Initialize(Me, "GD", ClientIdOauth, ClientSecret) ', AppApiKey)
   GD.ConnectToDrive
   wait for GD_Connected(mapRet As Map)
   ' ww~~-- ww~~-- ────────────────────
   '
   'Log("#-  x129, GD_Connected, mapRet=" & mapToPrettyString(mapRet) )
   Label1.Text = "Access_token= " & mapRet.GetDefault("access_token", "?")
   '
   GD.ShowFileList("")
   wait for GD_FileListResult(lstFiles As List)
   ' ww~~-- ww~~-- ────────────────────
   '
   Log("#-GD_FileListResult, lstFiles=" & lstToPrettyString(lstFiles) )
   ListView1.Clear
   Dim lblX As Label
   lblX = ListView1.SingleLineLayout.Label
   lblX.TextSize = 12
   lblX.TextColor = Colors.Green
   For i=0 To lstFiles.Size -1
       ListView1.AddSingleLine(lstFiles.Get(i))
   Next
   '
   btnCreaFolder.Enabled = True
   btnUpload.Enabled = True
   btnDownload.Enabled = True
   btnSearch.Enabled = True
   '
   Return Null
End Sub
' ------------------------------------------------------------------------------
Sub btnCreaFolder_Click As ResumableSub
   Log("#-")
   Log("#- ---*** ---*** ---*** ---*** ---*** ---*** ---*** ---*** ")
   Log("#-Sub btnCreaFolder_Click")
   Dim strFolderParentID As String = ""
   edtFolderToCreate.Text = "Testfolder_01"
   Label3.Text = $"(Trying to create folder ${edtFolderToCreate.Text})"$
   GD.CreateFolder(edtFolderToCreate.Text, strFolderParentID)
   wait for GD_FolderCreated(strFileId As String)
   ' ww~~-- ww~~-- ────────────────────
   '
   Label3.Text = "FileId= " & strFileId
   Log("#-  x178, strFileId=" & strFileId)
   Return Null
End Sub
' ------------------------------------------------------------------------------
Sub btnUpload_Click As ResumableSub
   Log("#-")
   Log("#- ---*** ---*** ---*** ---*** ---*** ---*** ---*** ---*** ")
   Log("#-Sub btnUpload_Click")
   Dim strFolderParentID As String = ""
   Dim strFilenameInGdrive As String = "testdatafile4"
   EditText2.Text = strFilenameInGdrive
   Dim strFileToUpload As String     = "testdata2.json"
   Label2.Text = $"(Trying to upload ${strFileToUpload})"$
   GD.UploadFile("", File.DirAssets, strFileToUpload, strFolderParentID, strFilenameInGdrive)
   wait for GD_FileUploadDone(strFileId As String)
   ' ww~~-- ww~~-- ────────────────────
   '
   Label2.Text = "FileId= " & strFileId
   Log("#-  x130, strFileId=" & strFileId)
 
   edtFileToDownload.Text = strFileId
 
   Return Null
End Sub
' ------------------------------------------------------------------------------
Sub btnSearch_Click As ResumableSub
   Log("#-")
   Log("#- ---*** ---*** ---*** ---*** ---*** ---*** ---*** ---*** ")
   Log("#-Sub btnSearch_Click")
   Label4.Text = $"(Trying to search for ${EditText2.Text.Trim})"$
   GD.SearchForFileID(EditText2.Text.Trim, EditText1.Text.Trim)
   wait for GD_FileFound(strFileId As String)
   ' ww~~-- ww~~-- ────────────────────
   '
   Label4.Text = "FileId= " & strFileId
   Log("#-  x170, strFileId=" & strFileId)
   Return Null
End Sub
' ------------------------------------------------------------------------------
Sub btnDownload_Click As ResumableSub
   Log("#-")
   Log("#- ---*** ---*** ---*** ---*** ---*** ---*** ---*** ---*** ")
   Log("#-Sub btnDownload_Click")
 
   Label5.Text = $"(Trying to download ${edtFileToDownload.Text.Trim})"$
   GD.DownloadFile(edtFileToDownload.Text.Trim, File.DirDefaultExternal, "aaa_file_" & edtFileToDownload.Text.Trim)
   wait for GD_FileDownloaded(strRet As String)
   ' ww~~-- ww~~-- ────────────────────
   '
   Label5.Text = "strRet= " & strRet
   Log("#-  x203, strRet=" & strRet)
 
End Sub
' ------------------------------------------------------------------------------

04-08-_2018_18-41-27.jpg


B4X:
'           Sub ConnectToDrive
'            Sub ShowFileList(ParentFolderID As String)
'            Sub CreateFolder(FolderName As String, ParentFolderID As String)
'            Sub UploadFile(FileId As String, LocalPath As String, LocalFilename As String, ParentFolderID As String, Name As String)
'            Sub DownloadFile(FileID As String, LocalPath As String, LocalFilename As String)
'            Sub SearchForFileID(SearchFile As String, ParentFolderID As String)
'            Sub SearchForFolderID(SearchFolder As String, ParentFolderID As String)

If something is fundamentally wrong, please explain and enclose a corresponding test project.

Questions should be asked in the Android questions forum including errormessages or code in code-tags.

Edit: Testproject updated to 18 due to some typos in connection strings
 

Attachments

Last edited:

leitor79

Active Member
Licensed User
Hi!

Thank you very much for sharing! I'll start to see what can I do with this.

Meanwhile, I have a question: in the comments, there is a statement:

' #### NEVER STORE THIS IN PRODUCTION SOURCECODE #############################################################
(regarding the ClientIDOauth). So, what would be the alternative? storing it where?

Thank you very much!
 

leitor79

Active Member
Licensed User
Well, I've started the project, and when I click the "connect" button, Chrome opens and shows the screen below.

Besides this error I'd like to know: is it the end behavior that the browser will open when the user wants to store/use a google drive file?

Regards!
 

Attachments

fredo

Well-Known Member
Licensed User
...storing it where?
A potential attacker can see all possibilities of local storage with sufficient energy sooner or later with the means of reverse engineering that are common today.

Probably the safest method would be to keep the keys available on your own external server and to retrieve them encrypted with your own access code at short notice. However, you need the knowledge of how to do this exactly and a suitable server.

Since I am not a specialist in the field of access security, I currently only see these easy to handle possibilities for us to make a hacker's search as difficult and time-consuming as possible:
  • Obfuscate the keys as "clever altered strings" and save them encrypted in a keyvaluestore
  • Hide the keys via steganography in harmless looking images
  • Be careful on how the code looks around the use of the security measures. A msgbox("access denied",...) will certainly be quickly detected by a superficially interested 12-year-old between school and the next fortnite session.
Here are some reads around android security:
Some more information on securing android apps:
To check your own app for hidden/detectable strings:
 
Last edited:

leitor79

Active Member
Licensed User
A potential attacker can see all possibilities of local storage with sufficient energy sooner or later with the means of reverse engineering that are common today.

Probably the safest method would be to keep the keys available on your own external server and to retrieve them encrypted with your own access code at short notice. However, you need the knowledge of how to do this exactly and a suitable server.

Since I am not a specialist in the field of access security, I currently only see these easy to handle possibilities for us to make a hacker's search as difficult and time-consuming as possible:
  • Obfuscate the keys as "clever altered strings" and save them encrypted in a keyvaluestore
  • Hide the keys via steganography in harmless looking images
  • Be careful on how the code looks around the use of the security measures. A msgbox("access denied",...) will certainly be quickly detected by a superficially interested 12-year-old between school and the next fortnite session.
Here are some reads around android security:
Thank you very much, fredo! What a great post. I was aware about the reverse engineering vulnerability, but I thought that the "release (obfuscated)" mode was enough for that?
 

leitor79

Active Member
Licensed User
This is probably due to the incorrect creation of the API credentials or the entry in the manifest.

Make sure to have a working project of Erel's Oauth2 class.
Thanks again, fredo! I was running your example. I supposed the manifest was right, but "just checking" it I realized that your example has one package name and my credential was created with my package name, so that was it. I've changed your package name and put it mine and now it works great.

Thank you very much!
 

leitor79

Active Member
Licensed User
Hi fredo!

I've been looking at the service properties, and I have some questions.

First of all, I haven't found any issues regarding the change of scope. I've just changed:

B4X:
    'Private scope As String = "https://www.googleapis.com/auth/drive"
    Private scope As String = "https://www.googleapis.com/auth/drive.appdata"
...and everything seems to be working. I can upload a file and restore it.

The thing now is tat I'd like to show the user the last modified date/time of that file before retrieving it.

So, I've started trying to modify SearchFileID to include that information. Intercepting the H_SFI.GetString I notice that the date information is not coming in the response. THen I've googled and found this. However, I couldn't find the modifiedTIme property into the request. I think it would be in GoogleDrive.SearchForFileID, at H_SFI.Download2... line, something like this:

B4X:
        H_SFI.Download2("https://www.googleapis.com/drive/v3/files", _
        Array As String("access_token", myAccessToken, _
                             "corpora", "user", _
                             "q","mimeType!='application/vnd.google-apps.folder' and" & _
                             "name='" & SearchFile & "' and " & _
                             "trashed=false", _
                            "properties","modifiedTime"))
However, this doesn't provide the requested information.

Other ways ("fields" instead of "properties", por putting the stuff into the "q" parameter) returns an error.

Do you have any insight of this?

Thank you very much!
 

fredo

Well-Known Member
Licensed User
... any insight of this?
In order to get special information about a file BEFORE the download, it must be given as metadata during the upload. Since there is no "tag" field and the other fields are not suitable, only a workaround remains via the "description" field.

But be cautious. This is a weak solution, as the "description" may be changed on the GDrive by someone.​

In the GDrive class add a "description" field to the metadata
26-08-_2018_07-49-07.jpg

The "description" can contain not only human-readable information but also purely technical information in the form of strings of characters.
26-08-_2018_07-54-39.jpg
In this case, a time stamp in the form "#1534785367351" is appended.

However, the "description" can also be viewed and modified by the Gdrive user in a browser (except for the files in "files.appdata").
26-08-_2018_07-56-29.jpg
To evaluate the "description" as metadata, the "sub ShowFileList" must be adapted in the GDrive class module.
26-08-_2018_08-04-35.jpg
B4X:
Sub ShowFileList(ParentFolderID As String) As ResumableSub'ignore
    Log("#-GoogleDrive.Sub ShowFileList, ParentFolderID=" & ParentFolderID)
    Dim h_sfl As HttpJob
    h_sfl.Initialize("", Me)
    If ParentFolderID="" Then
        h_sfl.Download2("https://www.googleapis.com/drive/v3/files", _
             Array As String("access_token", myAccessToken, _
                             "corpora", "user", _
                             "fields", "files(description,appProperties,createdTime,id,kind,lastModifyingUser,mimeType,modifiedByMe,modifiedByMeTime,name,properties,size,starred,version)" , _
                             "q","mimeType!='application/vnd.google-apps.folder' and trashed=false"))
    Else
        h_sfl.Download2("https://www.googleapis.com/drive/v3/files", _
             Array As String("access_token", myAccessToken, _
                             "corpora", "user", _
                             "fields", "files(description,appProperties,createdTime,id,kind,lastModifyingUser,mimeType,modifiedByMe,modifiedByMeTime,name,properties,size,starred,version)" , _
                             "q","mimeType!='application/vnd.google-apps.folder' and '" & ParentFolderID & "' in parents and trashed=false"))
    End If
    '
    ' Find the parameters with API explorer:
    '     --> https://developers.google.com/apis-explorer/?hl=en_GB#p/drive/v3/drive.files.list?corpora=user&q=mimeType!%253D'application%252Fvnd.google-apps.folder'
    '                 files(appProperties,createdTime,fileExtension,id,kind,size)
 
    Wait For (h_sfl) JobDone(h_sfl As HttpJob)
    ' ww~~-- ww~~-- ────────────────────
    Log("#-  x207, h_sfl.Success=" & h_sfl.Success)
    '
    If h_sfl.Success Then
        Log("#-  x209, h_sfl.GetString=" & h_sfl.GetString)
        '    {
        '         "files": [
        '          {
        '           "kind": "drive#file",
        '           "id": "sdfsdf",
        '           "name": "A1 gesicherte Daten vom 21.8., 10:23",
        '           "mimeType": "application/x-zip",
        '           "description": "Datendatei von A1 Wissyxcyxc\nLetzte Änderung: 20.8.2018, 19:16\n\n#1534785367351",
        '           "starred": false,
        '           "version": "2",
        '           "createdTime": "2018-08-21T08:23:42.114Z",
        '           "modifiedByMeTime": "2018-08-21T08:23:42.114Z",
        '           "modifiedByMe": true,
        '           "lastModifyingUser": {
        '            "kind": "drive#user",
        '            "displayName": "sadasd sdasda sd",
        '            "photoLink": "https://lh3.goog.../photo.jpg",
        '            "me": true,
        '            "permissionId": "453453455345",
        '            "emailAddress": "sdfsfsdfs fghfhfgh"
        '           },
        '           "size": "311567"
        '          },
        '          {
        '           "kind": "drive#file",
        '           "id": "fdssdfsdf",
        '           "name": "A1 gesicherte Daten vom 21.8., 10:19",
        '           "mimeType": "application/x-zip",
        '           "description": "Datendatei von A1 Wissyxcyxc\nLetzte Änderung: 20.8.2018\n#1534785367351",
        '           "starred": false,
        '           "version": "2",
        '           "createdTime": "2018-08-21T08:19:30.441Z",
        '           "modifiedByMeTime": "2018-08-21T08:19:30.441Z",
        '           "modifiedByMe": true,
        '           "lastModifyingUser": {
        '            "kind": "drive#user",
        '            "displayName": "sadasd sdasda sd",
        '            "photoLink": "https://lh3.goog.../photo.jpg",
        '            "me": true,
        '            "permissionId": "453453455345",
        '            "emailAddress": "sdfsfsdfs fghfhfgh"
        '           },
        '           "size": "311567"
        '          }
        '         ]
        '        }
        '
        Dim files As List
        Dim Map1 As Map
        Dim J As JSONParser
        J.Initialize(h_sfl.GetString)
        files.Initialize
        Map1 = J.NextObject
        files = Map1.Get("files")
'        Log("#-  x255, files.Size=" & files.Size)
        '
        Dim lstFilesToDownload As List
        lstFilesToDownload.Initialize
        Dim mapFileEntry As Map
        For i = 0 To files.Size - 1
            mapFileEntry = files.Get(i)
            'Log("#-    x261, mapFileEntry=" & fcn.logm(mapFileEntry))
            Dim mapFileEntryExp As Map
            mapFileEntryExp.Initialize
            mapFileEntryExp.Put("id", mapFileEntry.Get("id"))
            mapFileEntryExp.Put("name", mapFileEntry.Get("name"))
            mapFileEntryExp.Put("mimeType", mapFileEntry.Get("mimeType"))
            mapFileEntryExp.Put("createdTime", mapFileEntry.Get("createdTime"))
            mapFileEntryExp.Put("description", mapFileEntry.Get("description"))
            lstFilesToDownload.Add(mapFileEntryExp)
        Next
'        Log("#-  x220, lstFilesToExport=" & fcn.loglst(lstFilesToExport, "    "))
        'CallSub2(evModule, evName & "_FileListResult", FilelisteExport)
    Else
        'hhgf-Log("#-  x231, h_sfl.ErrorMessage=" &  h_sfl.ErrorMessage)
    End If
    h_sfl.Release
    'hhgf-Log("#-        x233, end of ShowFileList >>>")
    'hhgf-Log("#-")
    'hhgf-Log("#-")
    Return lstFilesToDownload
 
End Sub

Now, for example, the received time stamp can be evaluated BEFORE a download and can be compared with a local value.
26-08-_2018_08-13-45.jpg
In this example, the attached time stamp is extracted and evaluated.
 
Last edited:

leitor79

Active Member
Licensed User
Hi Fredo! Thank you very much for your answer, again!

I've understood your explanation.

However, I don't use the ShowFileList method, since I'm writing on appdata. I've tried replacing the link at the SearchForFileID method to include only the properties I want, and it worked!


B4X:
        H_SFI.Download2("https://www.googleapis.com/drive/v3/files", _
        Array As String("access_token", myAccessToken, _
                             "corpora", "user", _
                             "fields", "files(id,name,modifiedTime)" , _
                             "q","mimeType!='application/vnd.google-apps.folder' and" & _
                             "name='" & SearchFile & "' and " & _
                             "trashed=false"))
So now, I got 3 properties back. Then, below, I use them this way:

B4X:
CallSub3(evModule, evName & "_FileFound", fileEntry.Get("id"), fileEntry.Get("modifiedTime"))
Just in case it would be useful for others, if you add the "fileds" property it seems that you should explicitly declare id and name (both things came by default if you don't use the field property, but if you add it and you don't add id and name, then you won't get those properties; only the ones you declare -modifiedTime only, in my case-)

Thank you very much for sharing this!
 

leitor79

Active Member
Licensed User
Ok. Thank you.

View attachment 71597
Could it be that a space is missing here? In the concatenation "... andname=" would be created...
Hi fredo!

That section was a copypaste of your demo project. However, it seems to work that way. I've added the missing space, and it works too... so, maybe google is prepared for missing spaces?

Regards!
 

fredo

Well-Known Member
Licensed User
was a copypaste
Thank you for noticing.

A new Testproject was uploaded to correct this:

29-08-_2018_06-36-06.jpg


Hmm, that's weird.

It would be impressive if the API recognizes such errors as typical and automatically corrects "andsomething" in "and something".

29-08-_2018_08-13-07.jpg
 

leitor79

Active Member
Licensed User
Thank you for noticing.
Actually it was YOU that noticed it! I've just pasted your code without noticing there was something wrong.

Thank you very much, again!

PS: Maybe the "andtrashed" part was completely ignored by the API and google was returning also trashed files? Because your previous code throw no exceptions and get results back.
 

DonManfred

Expert
Licensed User
You should always create a NEW THREAD for ANY question you have. Posting to existing Thread is a mistake.

Looks like you did not successfully implement the client in the google console.
 
Top