Hey I have followed the steps from the Drive quick start guides and generated a token.json using the following scopes:
const SCOPES = [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.appdata',
'https://www.googleapis.com/auth/drive.file'
];
I am then running the following code to try and create/upload a file to google drive.
async function testing() {
let driveFolder = 'FOLDERID';
let res;
try {
const oauth2Client = new google.auth.OAuth2(
credentials.client_id,
credentials.client_secret,
credentials.redirect_uris
);
await oauth2Client.setCredentials({
...tokenInfo
});
console.log('=== completed drive authentication', oauth2Client);
const drive = await google.drive({
version: 'v3',
auth: await oauth2Client
});
console.log('=== Initialised Drive API');
res = await drive.files.create({
parameters :{
uploadType: 'resumable'
},
requestBody: {
name: 'Testing.txt',
mimeType: 'text/plain',
parents: [driveFolder]
},
media: {
mimeType: 'text/plain',
body: 'Hello world!!!'
}
});
console.log('=== Completed Report UPLOAD:', res);
return;
} catch (err) {
console.error('ERROR: \n', err);
}
}
The request completes without catching an error but the response that returns is undefined. The last two logs show the following
=== completed drive authentication OAuth2Client {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
transporter: DefaultTransporter {},
credentials:
{ access_token:
'ACCESS TOKEN STRING',
refresh_token:
'REFRESH TOKEN STRING',
scope:
'https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.appdata',
token_type: 'Bearer',
expiry_date: 1551070810157 },
certificateCache: null,
certificateExpiry: null,
refreshTokenPromises: Map {},
_clientId:'CLIENT ID STRING VALUE',
_clientSecret: 'CLIENT SECRET VALUE',
redirectUri: [ 'REDIRECT URL', 'http://localhost' ],
authBaseUrl: undefined,
tokenUrl: undefined,
eagerRefreshThresholdMillis: 300000 }
=== Initialised Drive API
=== Completed Report UPLOAD: undefined
You can try the quickstart using NodeJS from the documentation:
Uploaded file in my drive:
Also, please check this SO post for more details.