Store and Retrieve sqlite databse in Google Drive Xamarin forms

I am developing a Cross Platform Xamarin Application. I am developing an application in which users store their daily transactions in sqlite table . I want to make backup for them in ntheir own google drive. To be specific they should be able to take the backup of the database table in the google drive. Dont give me link to Apis and all I am nmew to this. Refer me to some working samples or Tutorials which explains from the scratch

Answers

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    @mazinncr9 said:
    Dont give me link to Apis and all I am nmew to this. Refer me to some working samples or Tutorials which explains from the scratch

    This should do the trick: https://xamarindev.blog/2017/03/22/google-drive-api-with-xamarin-forms/

  • YahyaAlnaqeebYahyaAlnaqeeb Member ✭✭
    edited March 20

    @mazinncr9 said:
    I am developing a Cross Platform Xamarin Application. I am developing an application in which users store their daily transactions in sqlite table . I want to make backup for them in ntheir own google drive. To be specific they should be able to take the backup of the database table in the google drive. Dont give me link to Apis and all I am nmew to this. Refer me to some working samples or Tutorials which explains from the scratch

    Did You Solve this Issues ??

  • YahyaAlnaqeebYahyaAlnaqeeb Member ✭✭

    Did > @mazinncr9 said:

    I am developing a Cross Platform Xamarin Application. I am developing an application in which users store their daily transactions in sqlite table . I want to make backup for them in ntheir own google drive. To be specific they should be able to take the backup of the database table in the google drive. Dont give me link to Apis and all I am nmew to this. Refer me to some working samples or Tutorials which explains from the scratch

    Did You Solve this Issues

  • mazinncr9mazinncr9 Member ✭✭
    edited March 21

    @YahyaAlnaqeeb I've stored and retrieved SQLite databases using DropBox.
    I couldn't do it to Google Drive

  • tayyipemretayyipemre Member ✭✭
    @mazinncr9 how can you do solution Dropbox? Can you share please.
  • mazinncr9mazinncr9 Member ✭✭

    Sure I'll share the code before that I've to mention you few things.
    1-> I've done it with Dropbox not google drive
    2-> There is a part in dropbox registering where the user has to copy-paste an authentication code from the browser to our application and register which is not required by other apps. I contacted the official Dropbox API helpers. They told me officially they won't support Xamarin and I searched several places to get Redirect Uri for Xamarin I didn't get the code for it.

    I hope you've read the limitations. I am posting the code below

    Used Nuget Packages
    Xamarin.Dropbox.API 4.5.3.1
    PCL Storage
    Xamarin Essentials

    Firstly get an App ID and App Secret from Dropbox API website.

    To get the access token which I mentioned user should copy paste to our App. Developer should store it(I stored it as text using PCL Storage)

    private async Task GetAccessToken()
    {
    var authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(Your App IDI from DropBox API Website);
    await Browser.OpenAsync(authorizeUri.ToString(), BrowserLaunchType.External);
    }

    Where you get a string in browser the user should copy it to an entry or something so that the app can store it. This is the ugly part of these steps. If you better way for this let me know it too

    While they submit that code in Application

    private async void OnCodeSumbmit()
    {

        var temp = await DropboxOAuth2Helper.ProcessCodeFlowAsync(OAUTH, "*APP KEY FROM DROPBOX API WEBSITE*", "*APP SECRET FROM DROPBOX API WEBSITE*");
        try
        {
            using (var dbx = new DropboxClient(temp.AccessToken))
            {
                var updated = await dbx.Users.GetCurrentAccountAsync();
              //  UserDialogs.Instance.Toast("Account Linked Successfully",TimeSpan.FromSeconds(4));
               await PCLHelper.WriteTextAllAsync("AccessToken", temp.AccessToken);
    
            }
        }
        catch (Exception ex)
        {
            UserDialogs.Instance.Toast("You have copied the wrong Code\n press Link to do it again",TimeSpan.FromSeconds(6));
            UserDialogs.Instance.HideLoading();
        }
    }
    

    To fetch files from Dropbox

    private async void GetList()
    {
    var token = await PCLHelper.ReadAllTextAsync("AccessToken");
    // token verification and updating list
    try
    {
    ListBackup.Clear();
    using (var dbx = new DropboxClient(token))
    {

                var updated = await dbx.Users.GetCurrentAccountAsync();
                var list = await dbx.Files.ListFolderAsync("/"+*Drop box folder name where databases are situated*);
                foreach (var item in list.Entries.Where(i => i.IsFolder))
                {
                    ListBackup.Add(new BackUpListModel(item.Name));
                }
             }
        }
        catch(Dropbox.Api.ApiException<Dropbox.Api.Files.ListFolderError> ex)
        {
           // UserDialogs.Instance.Toast("No Backups Found",TimeSpan.FromSeconds(3));
        }
        catch (Exception ex)
        {
          //  UserDialogs.Instance.Toast("Link your account Again");
            await PCLHelper.DeleteFile("AccessToken");
            await _navigationService.GoBackAsync();
        }
    }
    

    To save data in Dropbox

    private async void BackUp()
    {
    //initial set ups
    //---token
    var token = await PCLHelper.ReadAllTextAsync("AccessToken");
    //---foldername
    var folderdbx = "/" + CheckFirst.CompanyName + System.DateTime.Now.ToString("/dd-MMM-yyyy hh:mm:ss tt");
    try
    {
    using (var dbx = new DropboxClient(token))
    {
    bool check = await PCLHelper.IsFileExistAsync(".Journal.db3");
    var sqliteFilename = ".Journal.db3";
    IFolder folder = PCLStorage.FileSystem.Current.LocalStorage;
    IFile file = await folder.GetFileAsync(sqliteFilename);
    stream = await file.OpenAsync(PCLStorage.FileAccess.ReadAndWrite);
    var updated = await dbx.Files.UploadAsync(
    folderdbx + "/" + CheckFirst.CompanyName + ".Journal.db3",
    WriteMode.Overwrite.Instance,
    body: stream);
    }
    }
    catch (Exception ex)
    {
    //Code for user to know Some Exception has occurred.
    }
    }

    I am telling you, you can optimize and make it better in your own ways. It's just a way of mine achieving the same.
    Hope this finds helpful to you.

  • tayyipemretayyipemre Member ✭✭

    thank you for detail answer. I will try this solution but I found another solution waiting answer. You know Mega cloud and using MegaCloudApi your email - password login then upload or download any file. You search MegaCloudApi find doc. I put the sample code below.

        void Main()
        {
          var client = new MegaApiClient();
          client.Login("[email protected]", "passw0rd");
    
          IEnumerable<INode> nodes = client.GetNodes();
    
          INode root = nodes.Single(x => x.Type == NodeType.Root);
          INode myFolder = client.CreateFolder("Upload", root);
    
          INode myFile = client.UploadFile("MyFile.ext", myFolder);
          Uri downloadLink = client.GetDownloadLink(myFile);
          Console.WriteLine(downloadLink);
    
          client.Logout();
        }
    
  • CheegerCheeger RUMember ✭✭

    @tayyipemre said:
    thank you for detail answer. I will try this solution but I found another solution waiting answer. You know Mega cloud and using MegaCloudApi your email - password login then upload or download any file. You search MegaCloudApi find doc. I put the sample code below.

        void Main()
        {
          var client = new MegaApiClient();
          client.Login("[email protected]", "passw0rd");
        
          IEnumerable<INode> nodes = client.GetNodes();
        
          INode root = nodes.Single(x => x.Type == NodeType.Root);
          INode myFolder = client.CreateFolder("Upload", root);
        
          INode myFile = client.UploadFile("MyFile.ext", myFolder);
          Uri downloadLink = client.GetDownloadLink(myFile);
          Console.WriteLine(downloadLink);
        
          client.Logout();
        }
        
    

    Did you manage to use this code?
    In my app, everything works(folder is created in the cloud) up to line:
    INode myFile = client.UploadFile("MyFile.ext", myFolder);
    If I specify the wrong path to the file pops exception, if the path correct, this line is executed indefinitely(file not uploading)

Sign In or Register to comment.