Skip to content

Commit

Permalink
feat: added microsoft dynamics 365 business central oauth endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
oberoi-gaurav committed Jun 12, 2024
1 parent 4c49803 commit ba45718
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import axios from "axios";
import qs from "qs";
import { DataObject, OAuthResponse } from "../../lib/types";

export const init = async ({ body }: DataObject): Promise<OAuthResponse> => {
try {
const requestBody = {
grant_type: "client_credentials",
client_id: body.clientId,
client_secret: body.clientSecret,
scope: "https://api.businesscentral.dynamics.com/.default",
};

const response = await axios({
url: `https://login.microsoftonline.com/${body.metadata?.formData?.BUSINESS_CENTRAL_TENANT_ID}/oauth2/v2.0/token`,
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
data: qs.stringify(requestBody),
});

const {
data: { access_token, expires_in, token_type },
} = response;

return {
accessToken: access_token,
refreshToken: "",
expiresIn: +expires_in,
tokenType: token_type,
meta: {},
};
} catch (error) {
throw new Error(
`Error fetching access token for Microsoft Dynamics 365 Business Central: ${error}`
);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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_REQUEST_PAYLOAD: { formData },
} = body;
const requestBody = {
grant_type: "client_credentials",
client_id,
client_secret,
scope: "https://api.businesscentral.dynamics.com/.default",
};

const response = await axios({
url: `https://login.microsoftonline.com/${formData?.BUSINESS_CENTRAL_TENANT_ID}/oauth2/v2.0/token`,
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
data: qs.stringify(requestBody),
});

const {
data: { access_token, expires_in, token_type },
} = response;

return {
accessToken: access_token,
refreshToken: "",
expiresIn: +expires_in,
tokenType: token_type,
meta: {},
};
} catch (error) {
throw new Error(
`Error fetching refresh token for Microsoft Dynamics 365 Business Central: ${error}`
);
}
};

0 comments on commit ba45718

Please sign in to comment.