如何将数据库上传到google gmail

发布于 2025-01-09 06:29:40 字数 2697 浏览 0 评论 0原文

我想使用以下教程将应用程序数据库保存到 gmail:
备份房间数据库文件在 Android 中 | REST API v3

当我想使用以下代码上传数据库时:

private void upload(){
    File storageFile = new File();
    storageFile.setParents(Collections.singletonList("appDataFolder"));
    storageFile.setName("classmanager");

    File storageFileShm = new File();
    storageFileShm.setParents(Collections.singletonList("appDataFolder"));
    storageFileShm.setName("classmanager-shm");

    File storageFileWal = new File();
    storageFileWal.setParents(Collections.singletonList("appDataFolder"));
    storageFileWal.setName("classmanager-wal");

    java.io.File filePath = new java.io.File(dbPath);
    java.io.File filePathShm = new java.io.File(dbPathShm);
    java.io.File filePathWal = new java.io.File(dbPathWal);
    FileContent mediaContent = new FileContent("",filePath);
    FileContent mediaContentShm = new FileContent("",filePathShm);
    FileContent mediaContentWal = new FileContent("",filePathWal);
    try {
        File file = googleDriveService.files().create(storageFile, mediaContent).execute();
        Log.i("TestGoogleDrive", "Filename: " + file.getName() + " File ID: " + file.getId());

        File fileShm = googleDriveService.files().create(storageFileShm, mediaContentShm).execute();
        Log.i("TestGoogleDrive", "FilenameShm: " + fileShm.getName() + " File IDShm: " + fileShm.getId());


        File fileWal = googleDriveService.files().create(storageFileWal, mediaContentWal).execute();
        Log.i("TestGoogleDrive", "FilenameWal: " + fileWal.getName() + " File IDWal: " + fileWal.getId());

    }
    catch(UserRecoverableAuthIOException e){
        Log.i("TestGoogleDrive"," UserRecoverableAuthIOException");
        startActivityForResult(e.getIntent(), 1);
    }
    catch(Exception e){
        Log.i("TestGoogleDrive"," Exception");
        e.printStackTrace();
        e.printStackTrace();
    }
}

但它不起作用并出现以下错误:

2022-02-22 09:25:46.201 20037-26860/? I/TestGoogleDrive:  Exception
2022-02-22 09:25:46.201 20037-26860/? W/System.err: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
2022-02-22 09:25:46.202 20037-26860/? W/System.err:     at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
2022-02-22 09:25:46.202 20037-26860/? W/System.err:     at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)

I want to save app database to gmail with the following tutorial :
Backup of Room Database files in Android | REST API v3

When I want to upload the database with the following code:

private void upload(){
    File storageFile = new File();
    storageFile.setParents(Collections.singletonList("appDataFolder"));
    storageFile.setName("classmanager");

    File storageFileShm = new File();
    storageFileShm.setParents(Collections.singletonList("appDataFolder"));
    storageFileShm.setName("classmanager-shm");

    File storageFileWal = new File();
    storageFileWal.setParents(Collections.singletonList("appDataFolder"));
    storageFileWal.setName("classmanager-wal");

    java.io.File filePath = new java.io.File(dbPath);
    java.io.File filePathShm = new java.io.File(dbPathShm);
    java.io.File filePathWal = new java.io.File(dbPathWal);
    FileContent mediaContent = new FileContent("",filePath);
    FileContent mediaContentShm = new FileContent("",filePathShm);
    FileContent mediaContentWal = new FileContent("",filePathWal);
    try {
        File file = googleDriveService.files().create(storageFile, mediaContent).execute();
        Log.i("TestGoogleDrive", "Filename: " + file.getName() + " File ID: " + file.getId());

        File fileShm = googleDriveService.files().create(storageFileShm, mediaContentShm).execute();
        Log.i("TestGoogleDrive", "FilenameShm: " + fileShm.getName() + " File IDShm: " + fileShm.getId());


        File fileWal = googleDriveService.files().create(storageFileWal, mediaContentWal).execute();
        Log.i("TestGoogleDrive", "FilenameWal: " + fileWal.getName() + " File IDWal: " + fileWal.getId());

    }
    catch(UserRecoverableAuthIOException e){
        Log.i("TestGoogleDrive"," UserRecoverableAuthIOException");
        startActivityForResult(e.getIntent(), 1);
    }
    catch(Exception e){
        Log.i("TestGoogleDrive"," Exception");
        e.printStackTrace();
        e.printStackTrace();
    }
}

But It didn't work and got below error :

2022-02-22 09:25:46.201 20037-26860/? I/TestGoogleDrive:  Exception
2022-02-22 09:25:46.201 20037-26860/? W/System.err: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
2022-02-22 09:25:46.202 20037-26860/? W/System.err:     at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
2022-02-22 09:25:46.202 20037-26860/? W/System.err:     at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文