@37teams/adbutler 中文文档教程
AdButler node.js bindings
这些绑定旨在用于服务器端,因为它们处理 显式秘密密钥。 不要在浏览器中使用它们。
Installation
使用以下 NPM 命令进行安装:
npm install --save adbutler
这会将 AdButler 依赖项保存在 package.json
中。
Usage
第一步是通过要求加载库。
var AdButler = require('adbutler');
接下来,创建一个 adButler
实例。 您可以在配置对象中传递您的秘密 API 密钥,如下所示。
var adButler = new AdButler({
'apiKey': 'YOUR_API_KEY'
})
每个资源都可以通过adButler
实例访问,并且支持create
, 读取
、更新
、删除
和列表
操作。 该库支持基于回调和基于承诺的使用。 选择您喜欢的编程风格。 两者的简要概述如下。
Callback based usage
每个资源方法都接受一个回调函数作为最后一个参数。 遵循 Node.js 约定,错误对象是 总是作为第一个参数传递给回调函数,并且是 null
如果 没有错误发生。 这是一个简单的例子。
adButler.advertisers.read(123, function(error, data) {
// error is null on success
if (error) {
console.log(error); // some error occurred
} else {
console.log(data); // the response data
}
});
Promise based usage
如果没有给出回调函数,每个资源方法都会返回一个承诺。
// Create a new media group
adButler.mediaGroups.create({
"name": "Demo Media Group"
}).then(function(mediaGroup) {
// Create a new image creative
return adButler.creatives.image.create({
'file': 'image-ad.png', // this file must exist where the script is executed
'group': mediaGroup.id,
'name': 'My Image Creative',
'description': 'This is a description of my image creative.'
});
}).then(function(imageCreative){
// Create a new image banner
return adButler.banners.image.create({
'name': 'My Image Banner',
'width': 300,
'height': 250,
'creative': imageCreative.id
});
}).then(function(imageBanner) {
// New image banner created in a new media group
}).catch(function(error) {
// Deal with the error
});
Configuration
adButler.set('apiKey', 'your_SECRET_api_key ');
adButler.set('timeout', 20000); // in ms
(default is node's default:120000ms
)
Documentation
请参阅我们的详细的 API 文档。
Development
使用 npm
$ npm install
$ npm test
运行测试:如果您愿意,可以使用 AdButler Test< 运行测试/em> 通过设置 API 密钥 运行测试之前的环境变量 ADBUTLER_TEST_API_KEY
:
$ export ADBUTLER_TEST_API_KEY='your_api_key'
$ npm test
注意:在 Windows 上使用 SET
而不是 export
来设置 ADBUTLER_TEST_API_KEY
环境变量。
License
© 2016–2017 SparkLIT。 在麻省理工学院许可下发布。
AdButler node.js bindings
These bindings are intended for use on the server side because they deal with secret keys explicitly. DO NOT use them in the browser.
Installation
Install using the following NPM command:
npm install --save adbutler
This will save the AdButler dependency in package.json
.
Usage
The first step is to load the library by requiring it.
var AdButler = require('adbutler');
Next, create an adButler
instance. You can pass in your secret API key in the configuration object as shown below.
var adButler = new AdButler({
'apiKey': 'YOUR_API_KEY'
})
Every resource can be accessed via the adButler
instance and supports create
, read
, update
, delete
, and list
operations. The library supports callback based and promise based usage. Pick the programming style you like. A brief overview of both follows.
Callback based usage
Every resource method accepts a callback function as the last argument. Following Node.js convention, the error object is always passed as the first argument to the callback function, and is null
if no error occurred. Here is a simple example.
adButler.advertisers.read(123, function(error, data) {
// error is null on success
if (error) {
console.log(error); // some error occurred
} else {
console.log(data); // the response data
}
});
Promise based usage
Every resource method returns a promise if no callback function is given.
// Create a new media group
adButler.mediaGroups.create({
"name": "Demo Media Group"
}).then(function(mediaGroup) {
// Create a new image creative
return adButler.creatives.image.create({
'file': 'image-ad.png', // this file must exist where the script is executed
'group': mediaGroup.id,
'name': 'My Image Creative',
'description': 'This is a description of my image creative.'
});
}).then(function(imageCreative){
// Create a new image banner
return adButler.banners.image.create({
'name': 'My Image Banner',
'width': 300,
'height': 250,
'creative': imageCreative.id
});
}).then(function(imageBanner) {
// New image banner created in a new media group
}).catch(function(error) {
// Deal with the error
});
Configuration
adButler.set('apiKey', 'your_SECRET_api_key ');
adButler.set('timeout', 20000); // in ms
(default is node's default:120000ms
)
Documentation
See our detailed API documentation.
Development
Run the tests using npm
:
$ npm install
$ npm test
If you wish, you may run tests using your AdButler Test API key by setting the environment variable ADBUTLER_TEST_API_KEY
before running tests:
$ export ADBUTLER_TEST_API_KEY='your_api_key'
$ npm test
Note: On Windows use SET
instead of export
for setting the ADBUTLER_TEST_API_KEY
environment variable.
License
© 2016–2017 SparkLIT. Released under the MIT license.