Skip to content

Commit

Permalink
fix(oauth): 400 error while refreshing access token
Browse files Browse the repository at this point in the history
- send auth metadata in query param or request body
based on send-auth-metadata-in variable in provider-fields.json
- rename axios imports to axios.default

Signed-off-by: Vedant K <gamemaker0042@gmail.com>
  • Loading branch information
gamemaker1 committed Apr 6, 2021
1 parent 3054fed commit cca941e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
const fs = require('fs-extra')
const ora = require('ora')
const chalk = require('chalk')
const axios = require('axios')
const axios = require('axios').default
const prompt = require('readcommand')
const express = require('express')
const open = require('open')
Expand Down Expand Up @@ -62,7 +62,7 @@ const listRequest = async (drive, folderPath, regex) => {
})

// Check if there is a response
if (result.data.content.length > 0) {
if (result.data.content && result.data.content.length > 0) {
// Get the files from the response
let files = result.data.content
if (regex) {
Expand Down Expand Up @@ -615,22 +615,22 @@ const Client = class {
.post(
tokenURL,
// In the body
`code=${code}&client_id=${get(
providerConfig.auth['send-auth-metadata-in'] === 'request-body' ? `code=${code}&client_id=${get(
`drives.${drive}.auth-meta.client-id`
)}&client_secret=${get(
`drives.${drive}.auth-meta.client-secret`
)}&redirect_uri=${get(
`drives.${drive}.auth-meta.redirect-uri`
)}&grant_type=${'authorization_code'}`,
)}&grant_type=${'authorization_code'}` : null,
// In the URL query parameters
{
params: {
params: providerConfig.auth['send-auth-metadata-in'] === 'query-param' ? {
code,
client_id: get(`drives.${drive}.auth-meta.client-id`), // eslint-disable-line camelcase
client_secret: get(`drives.${drive}.auth-meta.client-secret`), // eslint-disable-line camelcase
redirect_uri: get(`drives.${drive}.auth-meta.redirect-uri`), // eslint-disable-line camelcase
grant_type: 'authorization_code' // eslint-disable-line camelcase
}
} : {}
}
)
.then((result) => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

const chalk = require('chalk')
const axios = require('axios')
const axios = require('axios').default
const prompt = require('readcommand')

const Client = require('./client.js').Client
Expand Down
2 changes: 1 addition & 1 deletion src/knowledge.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
const fs = require('fs-extra')
const ora = require('ora')
const chalk = require('chalk')
const axios = require('axios')
const axios = require('axios').default
const prompt = require('readcommand')

const FormData = require('form-data')
Expand Down
11 changes: 6 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const fs = require('fs-extra')
const link = require('terminal-link')
const chalk = require('chalk')
const figlet = require('figlet')
const axios = require('axios')
const axios = require('axios').default

const Conf = require('conf')
const config = new Conf()
Expand Down Expand Up @@ -153,22 +153,22 @@ exports.refreshAccessToken = async (drive) => {
const result = await axios.post(
tokenURL,
// In the body
`refresh-token=${refreshToken}&client_id=${this.get(
providerConfig.auth['send-auth-metadata-in'] === 'request-body' ?`refresh_token=${refreshToken}&client_id=${this.get(
`drives.${drive}.auth-meta.client-id`
)}&client_secret=${this.get(
`drives.${drive}.auth-meta.client-secret`
)}&redirect_uri=${this.get(
`drives.${drive}.auth-meta.redirect-uri`
)}&grant_type=${'refresh_token'}`,
)}&grant_type=refresh_token` : null,
// In the URL query parameters
{
params: {
params: providerConfig.auth['send-auth-metadata-in'] === 'query-param' ? {
refresh_token: refreshToken, // eslint-disable-line camelcase
client_id: this.get(`drives.${drive}.auth-meta.client-id`), // eslint-disable-line camelcase
client_secret: this.get(`drives.${drive}.auth-meta.client-secret`), // eslint-disable-line camelcase
redirect_uri: this.get(`drives.${drive}.auth-meta.redirect-uri`), // eslint-disable-line camelcase
grant_type: 'refresh_token' // eslint-disable-line camelcase
}
} : {}
}
)
// Store the access token and update the expiry time
Expand Down Expand Up @@ -465,6 +465,7 @@ exports.printBright = (anything) => {

// Print out an error in red
exports.printError = (error) => {
console.error(error)
if (error.isAxiosError) {
if (error.code === 'ECONNRESET') {
this.print(
Expand Down

0 comments on commit cca941e

Please sign in to comment.