UNPKG

dfp-lib

Version:

This project hosts the Node.JS client library for the SOAP-based DFP API at Google.

47 lines (46 loc) 2.38 kB
"use strict"; 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"); const config_1 = require("../auth/config"); (function () { return __awaiter(this, void 0, void 0, function* () { const user = new src_1.dfp.DfpUser(config_1.default); user.logAll(); const oauth2Info = yield getOAuth2Credential(user); console.log(sprintf_js_1.sprintf("Your refresh token is: %s\n\n", oauth2Info['refresh_token'])); console.log(sprintf_js_1.sprintf("In your config file, edit the refresh_token line to be:\n" + "refresh_token = \"%s\"\n", oauth2Info['refresh_token'])); console.log("DONE"); }); })().catch((err) => console.error(err)); function getOAuth2Credential(user) { 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) => { const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question("After approving the token enter the authorization code here: ", function (code) { rl.close(); resolve(code.trim()); }); })); console.log("\n"); user.setOAuth2Info(oauth2Handler.getAccessToken(user.getOAuth2Info(), code, redirectUri)); return user.getOAuth2Info(); }); }