Skip to content

Commit

Permalink
feat: Fixing Box OAuth (picahq#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharth-bhansali authored Sep 9, 2024
1 parent 47aad3c commit 44b7150
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 81 deletions.
80 changes: 40 additions & 40 deletions integrationos-oauth/src/connections/box/init.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import axios from "axios";
import qs from "qs";
import { DataObject, OAuthResponse } from "../../lib/types";
import axios from 'axios';
import qs from 'qs';
import { DataObject, OAuthResponse } from '../../lib/types';

export const init = async ({ body }: DataObject): Promise<OAuthResponse> => {
try {
const {
clientId: client_id,
clientSecret: client_secret,
metadata: { code },
} = body;
try {
const {
clientId: client_id,
clientSecret: client_secret,
metadata: { code },
} = body;

const requestBody = {
grant_type: "authorization_code",
code,
client_id,
client_secret,
};
const requestBody = {
grant_type: 'authorization_code',
code,
client_id,
client_secret,
};

const response = await axios({
url: "https://api.box.com/oauth2/token",
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: 'application/json',
},
data: qs.stringify(requestBody),
});
const response = await axios({
url: 'https://api.box.com/oauth2/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
},
data: qs.stringify(requestBody),
});

const {
access_token: accessToken,
refresh_token: refreshToken,
token_type: tokenType,
expires_in: expiresIn,
} = response.data;
const {
access_token: accessToken,
refresh_token: refreshToken,
token_type: tokenType,
expires_in: expiresIn,
} = response.data;

return {
accessToken,
refreshToken,
expiresIn,
tokenType,
meta: {},
};
} catch (error) {
throw new Error(`Error fetching access token for Box: ${error}`);
}
return {
accessToken,
refreshToken,
expiresIn,
tokenType: tokenType === 'bearer' ? 'Bearer' : tokenType,
meta: {},
};
} catch (error) {
throw new Error(`Error fetching access token for Box: ${error}`);
}
};
82 changes: 41 additions & 41 deletions integrationos-oauth/src/connections/box/refresh.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
import axios from "axios";
import qs from "qs";
import { DataObject, OAuthResponse } from "../../lib/types";
import axios from 'axios';
import qs from 'qs';
import { DataObject, OAuthResponse } from '../../lib/types';

export const refresh = async ({ body }: DataObject): Promise<OAuthResponse> => {
try {
const {
OAUTH_CLIENT_ID: client_id,
OAUTH_CLIENT_SECRET: client_secret,
OAUTH_REFRESH_TOKEN: refresh_token,
OAUTH_METADATA: { meta },
} = body;
try {
const {
OAUTH_CLIENT_ID: client_id,
OAUTH_CLIENT_SECRET: client_secret,
OAUTH_REFRESH_TOKEN: refresh_token,
OAUTH_METADATA: { meta },
} = body;

const requestBody = {
grant_type: "refresh_token",
refresh_token,
client_id,
client_secret,
};
const requestBody = {
grant_type: 'refresh_token',
refresh_token,
client_id,
client_secret,
};

const response = await axios({
url: "https://api.box.com/oauth2/token",
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: 'application/json',
},
data: qs.stringify(requestBody),
});
const response = await axios({
url: 'https://api.box.com/oauth2/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
},
data: qs.stringify(requestBody),
});

const {
access_token: accessToken,
refresh_token: refreshToken,
token_type: tokenType,
expires_in: expiresIn,
} = response.data;
const {
access_token: accessToken,
refresh_token: refreshToken,
token_type: tokenType,
expires_in: expiresIn,
} = response.data;

return {
accessToken,
refreshToken,
expiresIn,
tokenType,
meta,
};
} catch (error) {
throw new Error(`Error fetching access token for Box: ${error}`);
}
return {
accessToken,
refreshToken,
expiresIn,
tokenType: tokenType === 'bearer' ? 'Bearer' : tokenType,
meta,
};
} catch (error) {
throw new Error(`Error fetching access token for Box: ${error}`);
}
};

0 comments on commit 44b7150

Please sign in to comment.