0

The scenario is: with a OAuth 2.0 client created in https://console.developers.google.com/apis/credentials, I want to pick some file from a user Google Drive (user space) in G Suite Business and then copy it to some folder in a Team Drive of another owner and then change the owner of the recently copied file.

This last operation causes a File not found error. Even with this error the file is copied file and has the ID that response tells me that it does not exist. The code and stack follows:

const google = require('googleapis'); // https://github.com/google/google-api-nodejs-client
const oauth2Client = new OAuth2('client_id', 'client_secret', 'redirect_url');

oauth2Client.setCredentials({'access_token': '' ,'token_type': '','expiry_date': ''});

const sourceFileId = '';

const googleDrive = google.drive({
    version: 'v3',
    auth: oauth2Client
});

googleDrive.permissions.create({
    fileId: sourceFileId,
    resource: {
        role: 'reader',
        type: 'domain',
        domain: 'example.com',
    }
}, (error, permissionResponse) => {

    if (error) {

        console.log(error);

    } else {

        googleDrive.files.copy({
            fileId: sourceFileId,
            supportsTeamDrives: true,
            ignoreDefaultVisibility: true,
            resource: {
                description: 'Copy of a file from user to store in Team Driver',
                parents: ['_driveteam_folder_id_']
            }
        }, (copyError, copyResponse) => {

            if (copyError) {

                console.log(copyError);

            } else {

                console.log(copyResponse, copyResponse.id);

                // Even with this timeout the error occurrs
                // 1elLwBg0urdz5z23_83tjpDSBt7YxD1c8 is the copyResponse.id show in the error below
                setTimeout(() => {

                    googleDrive.permissions.create({
                        fileId: copyResponse.id,
                        transferOwnership: true,
                        resource: {
                            role: 'owner',
                            type: 'user',
                            emailAddress: 'someone@example.com'
                        }
                    }, (ownerPermissionError, ownerPermissionResponse) => {

                        if (ownerPermissionError) {

                            console.log(ownerPermissionError);

                        } else {

                            console.log({permissionResponse, copyResponse, ownerPermissionResponse});   

                        }

                    });

                }, 10000);

            }

        });

    }

});

{ Error: File not found: 1elLwBg0urdz5z23_83tjpDSBt7YxD1c8. at Request._callback (googleapis\node_modules\google-auth-library\lib\transporters.js:85:15 at Request.self.callback (node_modules\request\request.js:186:22) at emitTwo (events.js:106:13) at Request.emit (events.js:191:7) at Request. (node_modules\request\request.js:1163:10) at emitOne (events.js:96:13) at Request.emit (events.js:188:7) at IncomingMessage. (node_modules\request\request.js:1085:12) at IncomingMessage.g (events.js:292:16) at emitNone (events.js:91:20) code: 404, errors: [ { domain: 'global', reason: 'notFound', message: 'File not found: 1elLwBg0urdz5z23_83tjpDSBt7YxD1c8.', locationType: 'parameter', location: 'fileId' } ] }

lordshark
  • 83
  • 9
  • Can you try making it two separate requests? One request to copy the file. Validate if it exists. If it does, add permission. – ReyAnthonyRenacia Feb 09 '18 at 15:15
  • In some terms are separated. I set this timeout just to give me time to check its existing directly in Google Drive and it does. Could you meaning to "separate" requests by reauthenticate again? – lordshark Feb 09 '18 at 16:22

0 Answers0