Skip to content

Commit 071e3e4

Browse files
author
Flintlozk
committed
Add Service for Create Account
1 parent 8569c18 commit 071e3e4

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

Diff for: get-auth.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
const { OAuth2Client } = require("google-auth-library");
44

55
class GoogleAdsAuth {
6-
/**
7-
* @param {object} configs
8-
* @property {string} developerToken
9-
* @property {string} userAgent
10-
* @property {string} clientId
11-
* @property {string} clientSecret
12-
* @property {string} refreshToken
13-
* @property {string} loginCustomerId
14-
*/
6+
/**
7+
* @param {object} configs
8+
* @property {string} developerToken
9+
* @property {string} userAgent
10+
* @property {string} clientId
11+
* @property {string} clientSecret
12+
* @property {string} refreshToken
13+
* @property {string} loginCustomerId
14+
*/
1515
constructor(configs) {
1616
this.configs = configs;
1717

Diff for: get-service.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"use strict";
2+
3+
const GoogleAdsAuth = require("./get-auth");
4+
const { NodeFetch } = require("./library/node-fetch");
5+
6+
class GoogleAdsReport {
7+
/**
8+
* @param {object} configs
9+
* @property {string} developerToken
10+
* @property {string} userAgent
11+
* @property {string} clientId
12+
* @property {string} clientSecret
13+
* @property {string} refreshToken
14+
* @property {string} loginCustomerId
15+
* @param {string} accessToken
16+
*/
17+
constructor(configs, accessToken) {
18+
this.configs = configs;
19+
this.accessToken = accessToken
20+
this.version = "v2";
21+
this.auth = new GoogleAdsAuth(configs);
22+
}
23+
24+
setOauthHeader() {
25+
return {
26+
"User-Agent": this.configs.userAgent,
27+
"Content-Type": "application/json",
28+
charset: "utf-8",
29+
Accept: "application/json",
30+
Authorization: `Bearer ${this.accessToken}`,
31+
"developer-token": this.configs.developerToken,
32+
"login-customer-id": this.configs.loginCustomerId
33+
};
34+
}
35+
36+
/**
37+
*
38+
* @param {*} cid
39+
* @param {*} operations
40+
*/
41+
async customerCreateAccount(cid, operations) {
42+
if (!cid) throw new Error("CID Not Found");
43+
if (!operations) throw new Error("Must Provide Operations");
44+
cid = cid.replace(/-/g, "");
45+
46+
try {
47+
const header = this.setOauthHeader();
48+
// Docs : https://developers.google.com/google-ads/api/reference/rpc/google.ads.googleads.v2.services#google.ads.googleads.v2.services.CreateCustomerClientRequest
49+
const endpoint = `https://googleads.googleapis.com/${this.version}/customers/${cid}:createCustomerClient`;
50+
const option = {
51+
method: "POST",
52+
body: JSON.stringify(operations),
53+
headers: header
54+
};
55+
const response = await NodeFetch(endpoint, option);
56+
const json = await response.json();
57+
58+
return json;
59+
} catch (err) {
60+
console.log("Error ::>", err);
61+
throw new Error(err.message);
62+
}
63+
}
64+
}
65+
66+
module.exports = GoogleAdsReport;

Diff for: index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
22
GoogleAdsAuth: require("./get-auth"),
33
GoogleAdsReport: require("./get-report"),
4+
GoogleAdsService: require("./get-service"),
45
};

0 commit comments

Comments
 (0)