-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcrud-ad-accounts.ts
More file actions
112 lines (102 loc) · 2.76 KB
/
crud-ad-accounts.ts
File metadata and controls
112 lines (102 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Example calls to perform CRUD and finder operations on ad accounts using versioned APIs.
* The 3-legged member access token should include the 'rw_ads' scope, which
* is part of the Advertising APIs product.
*/
import { RestliClient } from 'linkedin-api-client';
import dotenv from 'dotenv';
dotenv.config();
const API_VERSION = '202212';
const AD_ACCOUNTS_RESOURCE = '/adAccounts';
const AD_ACCOUNTS_ENTITY_RESOURCE = '/adAccounts/{id}';
async function main(): Promise<void> {
const restliClient = new RestliClient();
restliClient.setDebugParams({ enabled: true });
const accessToken = process.env.ACCESS_TOKEN || '';
/**
* Create a test ad account
*/
const createResponse = await restliClient.create({
resourcePath: AD_ACCOUNTS_RESOURCE,
entity: {
name: 'Test Ad Account',
reference: 'urn:li:organization:123',
status: 'DRAFT',
type: 'BUSINESS',
test: true
},
accessToken,
versionString: API_VERSION
});
const adAccountId = createResponse.createdEntityId as string;
console.log(`Successfully created ad account: ${adAccountId}\n`);
/**
* Get the created ad account
*/
const getResponse = await restliClient.get({
resourcePath: AD_ACCOUNTS_ENTITY_RESOURCE,
pathKeys: {
id: adAccountId
},
accessToken,
versionString: API_VERSION
});
console.log(`Successfully fetched ad acccount: ${JSON.stringify(getResponse.data, null, 2)}\n`);
/**
* Partial update on ad account
*/
await restliClient.partialUpdate({
resourcePath: AD_ACCOUNTS_ENTITY_RESOURCE,
pathKeys: {
id: adAccountId
},
patchSetObject: {
name: 'Modified Test Ad Account'
},
accessToken,
versionString: API_VERSION
});
console.log('Successfully did partial update of ad account\n');
/**
* Find all ad accounts according to a specified search criteria
*/
const finderResponse = await restliClient.finder({
resourcePath: AD_ACCOUNTS_RESOURCE,
finderName: 'search',
queryParams: {
search: {
reference: {
values: ['urn:li:organization:123']
},
name: {
values: ['Modified Test Ad Account']
},
test: true
}
},
accessToken,
versionString: API_VERSION
});
console.log(
`Successfully searched ad accounts: ${JSON.stringify(finderResponse.data.elements, null, 2)}\n`
);
/**
* Delete ad account
*/
await restliClient.delete({
resourcePath: AD_ACCOUNTS_ENTITY_RESOURCE,
pathKeys: {
id: adAccountId
},
accessToken,
versionString: API_VERSION
});
console.log('Successfully deleted ad account\n');
}
main()
.then(() => {
console.log('Completed');
})
.catch((error) => {
console.log(`Error encountered: ${error.message}`);
});