RobinBobin / react-native-google-drive-api-wrapper

This wrapper facilitates the use of the Google Drive API in React Native projects.
110 stars 43 forks source link

Store Application Specific Data #89

Open Khaptikma opened 1 year ago

Khaptikma commented 1 year ago

Greetings. Can this library store application specific data as in:

https://developers.google.com/drive/api/guides/appdata ?

RobinBobin commented 1 year ago

Hi there, Yes, it can 🙂. Any problem with that?

Khaptikma commented 1 year ago

I have succeeded to list all my files in drive, but I think I have a trouble finding out how to get list of my apps backup files in "backup" section. So can you help me out with that, please?

RobinBobin commented 1 year ago

What's the trouble?

hansyulian commented 1 year ago

hi, i need help, would you like to share me a simple json file upload for scope of `auth/drive.appdata'? i'm stuck here. thx

RobinBobin commented 1 year ago

Hi, Did you have a look at the docs and my sample project?

hansyulian commented 1 year ago

ok i somehow manage to create file, but i have some problem to retrieve the content. here is what i use:

  const saveFileContent = await gDrive.files.getJson(saveFileId, {
    spaces: 'appDataFolder',
  });

and the error is:

HttpError: {
  "error": {
    "code": 404,
    "message": "File not found: 1NrP0jSV5MvvnZ2Mwd-quMkXnBQFehWiiD6FP-tHbTtVy5Atuhh8.",
    "errors": [
      {
        "message": "File not found: 1NrP0jSV5MvvnZ2Mwd-quMkXnBQFehWiiD6FP-tHbTtVy5Atuhh8.",
        "domain": "global",
        "reason": "notFound",
        "location": "fileId",
        "locationType": "parameter"
      }
    ]
  }
}

I'm using only auth/drive.appdata scope

i created the file using:

const file = await gDrive.files.newMultipartUploader().setRequestBody({
  name: saveFileName,
  parents: ['appDataFolder']
}).setData(
  JSON.stringify(getDefaultAuthenticatorData()), MimeTypes.JSON
).execute();

i can see the file if i use

    const filesResponse = await gDrive.files.list({
      spaces: 'appDataFolder',
    });
RobinBobin commented 1 year ago

Sorry for the long delay. Is the issue still relevant for you?

hansyulian commented 1 year ago

hi, yes it still relevant. Actually would you able to create a snippet of code that demonstrate the usage of per-app data folder usage? that would be helpful

hansyulian commented 1 year ago

ok actually my issue is that i have the following code:

    return this.driveInstance.files.newMultipartUploader().setRequestBody({
      name: 'my-test.json',
      parents: ['appDataFolder']
    }).setData(
      JSON.stringify({timestamp: now.getTime()}), MimeTypes.JSON
    ).execute();

to always get [AbortError: Aborted], any idea?

RobinBobin commented 1 year ago

Hi @hansyulian, I'll try to find some time to create a sample snippet.

As for the error you get, I don't have an idea yet, sorry. Please, let's wait for a sample.

suchoX commented 2 months ago

@hansyulian I am not sure if you are stuck, but it works for me. The following code checks existing backup, uploads new backup, deletes old backup and also fetches the new backup

 if (GoogleSignin.getCurrentUser() === null) {
        setGoogleUser(await signInGoogleForDrive())
      }
const gDrive = new GDrive()
gDrive.accessToken = (await GoogleSignin.getTokens()).accessToken
gDrive.fetchCoercesTypes = true
gDrive.fetchRejectsOnHttpErrors = true
gDrive.fetchTimeout = 3000

const backupFilesData = await gDrive.files.list({
  spaces: 'appDataFolder',
})

const filesToDelete: string[] = []

if (backupFilesData.files.length > 0) {
  for (const file of backupFilesData.files) {
    filesToDelete.push(file.id)
  }
}
await gDrive.files
  .newMultipartUploader()
  .setRequestBody({
    name: 'backup.json',
    parents: ['appDataFolder'],
  })
  .setData(JSON.stringify(backupData), MimeTypes.JSON)
  .execute()

for (const fileId of filesToDelete) {
  await gDrive.files.delete(fileId)
}

const f = await gDrive.files.list({
  spaces: 'appDataFolder',
})

console.log(f)

const fileId = f.files[0].id
console.log('Backup file ID:', fileId)
const downloadResponse = await gDrive.files.getJson(fileId)
console.log('Backup JSON data:', downloadResponse)
suchoX commented 2 months ago

@RobinBobin I get the error in Android

[AbortError: Aborted]

Works fine in ios

suchoX commented 2 months ago

On further investigation, The issue is with Timeout. Increase it, and it will work.

RobinBobin commented 1 week ago

It looks like everything's sorted. Can I close the issue?