dfp-lib
Version:
This project hosts the Node.JS client library for the SOAP-based DFP API at Google.
64 lines (63 loc) • 2.94 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const sprintf_js_1 = require("sprintf-js");
const readline = require("readline");
const src_1 = require("../../../src");
(function () {
return __awaiter(this, void 0, void 0, function* () {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log("Please input your client ID and secret. "
+ "If you do not have a client ID or secret, please create one in "
+ "the API console: https://cloud.google.com/console \n");
const clientId = yield new Promise((resolve) => {
rl.question("Enter your client ID: ", (clientId) => {
resolve(clientId.trim());
});
});
const clientSecret = yield new Promise((resolve) => {
rl.question("Enter your client secret: ", (clientSecret) => {
resolve(clientSecret.trim());
});
});
const user = new src_1.dfp.DfpUser({
applicationName: null,
networkCode: null,
oauth2: {
client_id: clientId,
client_secret: clientSecret
}
});
user.logAll();
const oauth2Info = yield getOAuth2Credential(user, rl);
rl.close();
console.log(sprintf_js_1.sprintf("Your refresh token is: %s\n\n", oauth2Info['refresh_token']));
console.log("DONE");
});
})().catch((err) => console.error(err));
function getOAuth2Credential(user, rl) {
return __awaiter(this, void 0, void 0, function* () {
const offline = true;
const redirectUri = null;
const oauth2Handler = user.getOAuth2Handler();
const authorizationUrl = oauth2Handler.getAuthorizationUrl(user.getOAuth2Info(), redirectUri, offline);
console.log(sprintf_js_1.sprintf("Log in to your DFP account and open the following URL:\n%s\n\n", authorizationUrl));
const code = yield (new Promise((resolve) => {
rl.question("After approving the token enter the authorization code here: ", function (code) {
resolve(code.trim());
});
}));
console.log("\n");
user.setOAuth2Info(oauth2Handler.getAccessToken(user.getOAuth2Info(), code, redirectUri));
return user.getOAuth2Info();
});
}