Skip to content

Commit

Permalink
feat: fixed refresh token issue in Microsoft Sales (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
oberoi-gaurav authored Jul 17, 2024
1 parent 11e6135 commit fc2bdb4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,38 @@ 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,
resource: body.metadata?.formData?.MICROSOFT_DYNAMICS_365_SALES_ORGANIZATION_URI,
};
try {
const requestBody = {
grant_type: "client_credentials",
client_id: body.clientId,
client_secret: body.clientSecret,
resource:
body.metadata?.formData?.MICROSOFT_DYNAMICS_365_SALES_ORGANIZATION_URI,
};

const response = await axios({
url: `https://login.windows.net/${body.metadata?.formData?.MICROSOFT_DYNAMICS_365_SALES_TENANT_ID}/oauth2/token`,
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
data: qs.stringify(requestBody)
});
const response = await axios({
url: `https://login.windows.net/${body.metadata?.formData?.MICROSOFT_DYNAMICS_365_SALES_TENANT_ID}/oauth2/token`,
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
data: qs.stringify(requestBody),
});

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

return {
accessToken: access_token,
refreshToken: "",
expiresIn: +expires_in,
tokenType: token_type,
meta: {
resource,
}
};
} catch (error) {
throw new Error(`Error fetching access token for Microsoft Dynamics 365 Sales: ${error}`);
}
};
return {
accessToken: access_token,
refreshToken: "",
expiresIn: +expires_in,
tokenType: token_type,
meta: {
resource,
},
};
} catch (error) {
throw new Error(
`Error fetching access token for Microsoft Dynamics 365 Sales: ${error}`
);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,43 @@ import qs from "qs";
import { DataObject, OAuthResponse } from "../../lib/types";

export const refresh = async ({ body }: DataObject): Promise<OAuthResponse> => {
try {
const requestBody = {
grant_type: "client_credentials",
client_id: body.clientId,
client_secret: body.clientSecret,
resource: body.metadata?.formData?.MICROSOFT_DYNAMICS_365_SALES_ORGANIZATION_URI,
};
try {
const {
OAUTH_CLIENT_ID: client_id,
OAUTH_CLIENT_SECRET: client_secret,
OAUTH_REQUEST_PAYLOAD: { formData },
OAUTH_METADATA,
} = body;
const requestBody = {
grant_type: "client_credentials",
client_id,
client_secret,
resource: formData?.MICROSOFT_DYNAMICS_365_SALES_ORGANIZATION_URI,
};

const response = await axios({
url: `https://login.windows.net/${body.metadata?.formData?.MICROSOFT_DYNAMICS_365_SALES_TENANT_ID}/oauth2/token`,
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
data: qs.stringify(requestBody)
});
const response = await axios({
url: `https://login.windows.net/${formData?.MICROSOFT_DYNAMICS_365_SALES_TENANT_ID}/oauth2/token`,
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
data: qs.stringify(requestBody),
});

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

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

0 comments on commit fc2bdb4

Please sign in to comment.