Skip to content

Commit

Permalink
Change api_url & scopes for sharepoint and update sync functions
Browse files Browse the repository at this point in the history
  • Loading branch information
amuwal committed Sep 13, 2024
1 parent 091e72f commit 74fac19
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export class SharepointConnectionService extends AbstractBaseConnectionService {
grant_type: 'authorization_code',
});
const res = await axios.post(
// `https://app.sharepoint.com/oauth2/tokens`,
`https://login.microsoftonline.com/common/oauth2/v2.0/token`,
formData.toString(),
{
Expand All @@ -140,6 +139,17 @@ export class SharepointConnectionService extends AbstractBaseConnectionService {
'OAuth credentials : sharepoint filestorage ' + JSON.stringify(data),
);

// get site_id from tenant and sitename
const site_details = await axios.get(
`https://graph.microsoft.com/v1.0/sites/${tenant}.sharepoint.com:/sites/${site}`,
{
headers: {
Authorization: `Bearer ${data.access_token}`,
},
},
);
const site_id = site_details.data.id;

let db_res;
const connection_token = uuidv4();

Expand All @@ -154,7 +164,7 @@ export class SharepointConnectionService extends AbstractBaseConnectionService {
account_url: (
CONNECTORS_METADATA['filestorage']['sharepoint'].urls
.apiUrl as DynamicApiUrl
)(site),
)(site_id),
expiration_timestamp: new Date(
new Date().getTime() + Number(data.expires_in) * 1000,
),
Expand All @@ -173,7 +183,7 @@ export class SharepointConnectionService extends AbstractBaseConnectionService {
account_url: (
CONNECTORS_METADATA['filestorage']['sharepoint'].urls
.apiUrl as DynamicApiUrl
)(site),
)(site_id),
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
expiration_timestamp: new Date(
Expand Down Expand Up @@ -220,7 +230,7 @@ export class SharepointConnectionService extends AbstractBaseConnectionService {
)) as OAuth2AuthData;

const res = await axios.post(
`https://app.sharepoint.com/oauth2/tokens`,
`https://login.microsoftonline.com/common/oauth2/v2.0/token`,
formData.toString(),
{
headers: {
Expand Down
9 changes: 7 additions & 2 deletions packages/api/src/filestorage/group/services/onedrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class OnedriveService implements IGroupService {
private registry: ServiceRegistry,
) {
this.logger.setContext(
FileStorageObject.group.toUpperCase() + ':' + OnedriveService.name,
`${FileStorageObject.group.toUpperCase()}:${OnedriveService.name}`,
);
this.registry.registerService('onedrive', this);
}
Expand All @@ -34,7 +34,12 @@ export class OnedriveService implements IGroupService {
vertical: 'filestorage',
},
});
const resp = await axios.get(`${connection.account_url}/v1.0/groups`, {

// remove /sites/site_id from account_url
const url = connection.account_url.replace(/\/sites\/.+$/, '');

// ref: https://learn.microsoft.com/en-us/graph/api/group-list?view=graph-rest-1.0&tabs=http
const resp = await axios.get(`${url}/groups`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.cryptoService.decrypt(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export class SharepointService implements IUserService {
},
});

const resp = await axios.get(`${connection.account_url}/users`, {
// remove /sites/site_id from account_url
const url = connection.account_url.replace(/\/sites\/.+$/, '');

// ref: https://learn.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=http
const resp = await axios.get(`${url}/users`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.cryptoService.decrypt(
Expand Down
10 changes: 5 additions & 5 deletions packages/shared/src/connectors/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2718,7 +2718,7 @@ export const CONNECTORS_METADATA: ProvidersConfig = {
}
},
'onedrive': {
scopes: 'Files.Read.All offline_access openid User.Read',
scopes: 'Files.Read.All offline_access openid User.Read.All Group.Read.All',
urls: {
docsUrl: 'https://learn.microsoft.com/en-us/graph/api/overview?view=graph-rest-1.0',
apiUrl: 'https://graph.microsoft.com',
Expand All @@ -2745,15 +2745,15 @@ export const CONNECTORS_METADATA: ProvidersConfig = {
}
},
'sharepoint': {
scopes: 'Files.Read.All offline_access openid User.Read Sites.Read.All Sites.ReadWrite.All',
scopes: 'Files.Read.All offline_access openid User.Read.All Group.Read.All Sites.Read.All Sites.ReadWrite.All',
urls: {
docsUrl: 'https://learn.microsoft.com/en-us/graph/api/overview?view=graph-rest-1.0',
apiUrl: (site) => `https://graph.microsoft.com/v1.0/sites/${site}`,
authBaseUrl: (site, tenant) => `https://login.microsoftonline.com/${tenant}/oauth2/v2.0/authorize`
apiUrl: (site_id) => `https://graph.microsoft.com/v1.0/sites/${site_id}`,
authBaseUrl: `https://login.microsoftonline.com/common/oauth2/v2.0/authorize`
},
logoPath: 'https://pnghq.com/wp-content/uploads/pnghq.com-microsoft-sharepoint-logo-9.png',
description: 'Sync & Create contacts, deals, companies, notes, engagements, stages, tasks and users',
active: false,
active: true,
primaryColor: '#6EA5A8',
options: {
local_redirect_uri_in_https: true,
Expand Down

0 comments on commit 74fac19

Please sign in to comment.