Android Code Snippet GoogleDrive via API V3

Discussion in 'Code Snippets' started by mw71, Jun 18, 2017.

  1. mw71

    mw71 Active Member Licensed User

    Attached Files:

  2. Star-Dust

    Star-Dust Expert Licensed User

    Thanks, maybe I ask too much, but it would be helpful to have an example or instructions on how to embed it.:D:D:D
    Peter Simpson likes this.
  3. mw71

    mw71 Active Member Licensed User


    For a small project for demonstration, I have unfortunately no time at the moment. I hope I can do it later.

    A good start is the oAuth2 tutorial from Erel.

    In addition, you need the source of the OkHTTPUtils and must insert these two modules into the project (instead of the Libary).

    To the HttpJob Class add the follow Code:
    'Sends a PATCH request with the given string as the post data
    Public Sub PatchBytes(Link As String, Data() As Byte)
        req.InitializePatch2(Link, Data)
    "SubmitJob", Me)
    End Sub
    Then the class appended in Post 1 must be imported.

    (Dim GDrive As GoogleDrive)
    1. Initialize: GDrive.Initialize(Me, "Drive", clientId, clientSecret)
    2. Connect: GDrive.ConnectToDrive (Connect, Test the Connection, Refresh the Token, if necessary)
    -> Call Connected or ConnectFaild (Faild in the Main, can be made better)
    3. You can Search for Folders, Files, Upload and Download Files.....
    - Files are identified by ID, not by name!
    - to search for files in subfolders, the ID of the Parrent folder must be specified
    - Folder are special Files
    4. The event which is raise when the job is completed can be viewed in the code, e.g. CallSub2 (evModule, evName & "_FileFound", fileEntry.Get ("id"))

    fredo, PCastagnetti, Erel and 2 others like this.
  4. @13

    @13 New Member Licensed User

    Thanks so much!
    May I ask you how you've implemented oauth2.TestConnect?

    Sub ConnectToDrive
    Log("GoogleDrive ConnectToDrive")
    'Write log files
        If DoWriteLog Then CreateLogFile
    Log("GoogleDrive, Access Token request")
    Wait For OAuth2_AccessTokenAvailable (Success As Boolean, Token As TokenInformation)
    If Success = True Then
    Log("GoolgeDrive, Run Test Connect")
    '  <---------------------------------------    <---------------------------------------
            Wait For oauth2_TestFinish(Erfolg As Boolean, Token As TokenInformation)
    If Erfolg=True Then
    Log("GoogleDrive, Run Test Connect")
    Log("Google Drive, Test Connect Error")
    End If
    "Google Drive, No Access Token")
    End If

  5. DonManfred

    DonManfred Expert Licensed User

  6. mw71

    mw71 Active Member Licensed User

    sri, TestConnect i added

    Sub TestConnect
    Log("oAuth TestConnect")
    Dim h_tc As HttpJob
    "", Me)
    "", _
    Array As String("access_token", ti.AccessToken, _
    "corpora""user", _
    "q","mimeType!='application/' and trashed=false"))
    Wait For (h_tc) JobDone(h_tc As HttpJob)
    If h_tc.Success Then
    Log("OAuth, Getting access token from refresh token in TestConnect")
    Dim j_rt As HttpJob
    "", Me)
    "", _
    $"refresh_token=${ti.RefreshToken}&client_id=${mClientId}&grant_type=refresh_token&redirect_uri=${packageName & ":/oath"}"$)

    Wait For (j_rt) JobDone(j_rt As HttpJob)

    If j_rt.Success Then
    Log("OAuth TestConnect, Refresh access token Success")
    Dim jp As JSONParser
    Dim m As Map = jp.NextObject
                ti.AccessExpiry = 
    DateTime.Now + m.Get("expires_in")
                ti.AccessToken = m.Get(
    If m.ContainsKey("refresh_token"Then ti.RefreshToken = m.Get("refresh_token")
                ti.Valid = 
    Log("OAuth TestConnect, Refresh Token Error")
    End If
    End If
        CallSubDelayed3(mTarget, mEventName & 
    "_TestFinish", ti.Valid, ti)
    End Sub
    JohnC, fredo, toby and 3 others like this.
  7. Star-Dust

    Star-Dust Expert Licensed User

  8. trueboss323

    trueboss323 Active Member Licensed User

    Is it possible to change sharing settings for a folder on Google drive?
  9. mw71

    mw71 Active Member Licensed User

  10. fredo

    fredo Active Member Licensed User

    soyVB6 likes this.
  11. soyVB6

    soyVB6 Active Member Licensed User

    Great job guys, it was very easy to implement a backup system by following these steps.

    How can I SEE mail with this same token?
  12. fredo

    fredo Active Member Licensed User

    soyVB6 likes this.
  13. soyVB6

    soyVB6 Active Member Licensed User

    I got it that way, I even got the profile picture
    'Retorna el correo
    Sub Mail As ResumableSub
    Dim mp As Map

    Dim j As HttpJob
    "", Me)
    ""&myAccessToken,Array As String())                     
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
            mp = mpData(j.GetString)
    CallSub2(evModule, evName & "_getMail",mp)
    ToastMessageShow("Online data not available."True)
    End If
    Return mp
    End Sub

    Sub mpData(data As StringAs Map

    Dim l1 As String = data
        l1= l1.SubString(
    Dim q As String ="""user"": {"
        l1= l1.Replace(
    Dim l2 As String
        l2= l2.Replace(
        l2= l2.Replace(
    " ","")

    Dim parce As JSONParser
    Dim list1 As List = parce.NextArray
    For Each map1 As Map In list1
    '    Log("1 = "&map1.Get("displayName"))
    '    Log("2 = "&map1.Get("photoLink"))
    '    Log("3 = "&map1.Get("me"))
    '    Log("4 = "&map1.Get("permissionId"))
    '    Log("5 = "&map1.Get("emailAddress"))

    Return map1
    End Sub
    fredo likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice