@20minutes/node-google-dfp 中文文档教程
Google DFP API Client for NodeJS
这与 node-google-dfp 但依赖项是最新的。
yarn add @20minutes/node-google-dfp
Basics
初始化 DFP 实例。
import Dfp from 'node-google-dfp'
dfpUser = new Dfp.User(NETWORK_CODE, APP_NAME, VERSION)
接下来,设置您的客户端设置和您用户的 OAUTH 令牌信息。
dfpUser.setSettings({
client_id: "YOUR CLIENT ID",
client_secret: "YOUR CLIENT SECRET",
refresh_token: "A REFRESH TOKEN",
redirect_url: "YOUR OAUTH REDIRECT URL"
})
您可以实例化任何 DFP 的 API 服务:https://developers.google.com/doubleclick-publishers/docs/start
dfpUser.getService('LineItemService', function (err, lineItemService) {
if (err) {
return console.error(err)
}
const statement = new DfpClass.Statement('WHERE id = 103207340')
lineItemService.getLineItemsByStatement(statement, function (err, results) {
console.log(results)
})
})
Service accounts example
如果您想使用 Google Service Account 以访问 DFP,您可以通过创建 JWT 身份验证客户端的实例来实现。
import { JWT } from 'google-auth-library'
const jwtClient = new JWT(
SERVICE_ACCOUNT_EMAIL,
'path/to/key.pem',
null,
['https://www.googleapis.com/auth/dfp'],
)
dfpUser.setClient(jwtClient)
oAuth setup
此应用程序需要有效的 oAuth 刷新令牌才能发出请求。 如果您不包含刷新令牌,您将收到“未设置刷新令牌”错误。 如果您包含错误的令牌,您将收到“非法访问”错误。 不支持服务帐户。
要手动设置刷新令牌,请按照 Google 的说明使用 cURL。 主要步骤如下:
在 Google Developer Console 中设置一个 oAuth“已安装的应用程序”。
使用此已安装应用程序的客户端 ID 创建验证请求。 (如果您错过了这一步,您将在下一步收到来自 Google 的
authorization_pending
错误。)请注意,device_code
中的任何斜线都需要转义。curl -d "client_id={YOUR_OAUTH_CLIENT_ID}&scope=https://www.googleapis.com/auth/dfp" https://accounts.google.com/o/oauth2/device/code
{ “设备代码”:“ABCD-EFGH4/MEiMYvOO1THXLVfHGGN8obAgb5XFs1Uctj-QsyYsQk”, “用户代码”:“ABCD-EFGH”, "验证url" : "https://www.google.com/device", “expires_in”:1800, “间隔”:5 }
在浏览器中访问验证请求响应中包含的
verification_url
(例如https://www.google.com/device)并输入验证中提供的user_code
回复。 将给出验证码作为响应。使用 Google oAuth 应用程序提供的
code
以及您的客户端 ID 和密码来创建刷新令牌。curl -d "client_id={YOUR_OAUTH_CLIENT_ID}&client_secret={YOUR_OAUTH_CLIENT_SECRET}&code={YOUR_VERIFICATION_CODE}&grant_type=http://oauth.net/grant_type/device/1.0" https://accounts. google.com/o/oauth2/token
{ “访问令牌”:“ya29.JAHynQpVpjBFhvg-7VKdQ7nmD0DkmCYoWTWo535TP8QsKa6j2rFOI1i0pdclFepvGZo9A2SrN41dA”, "tokentype" : "Bearer", “过期时间”:3600, "idtoken" : "eyJhbGciOiJSUzI1NiIsImtpZCI6IjdjZmM3YTJhMDkwYWJjOGYxMDU5MjJmMzFiN2FjZGUzYzA2NmU1NTYifQ..T9mTcSl5bJLKrFhldXOd1L1CnGFfZHNF1eQOmJYyp7wR3vKbz8ATTNAfyo82hSGt9kGrHDBcdgaq18RYS72Tt0MclNy020romjl6rYRjs6GH93S3ZMiwra3UI3kmDXym9kyntedMS5gIPgJWfcoh0J0CTDNPBisLNrZntJv7Y", “刷新令牌”:“1/CGpCHgTgJ28PMnh84PgQBOgHHHaLCDbDQ0ZiINmOg” 您可以使用<
code>urn:ietf:wg:oauth:2.0:oob 作为非公开应用程序的重定向 URL。
Known Issues
- OAuth Support for more than just Refresh Token
- No unit tests
Google DFP API Client for NodeJS
This is exactly the same plugin as node-google-dfp but with dependencies up to date.
yarn add @20minutes/node-google-dfp
Basics
Initialize the DFP Instance.
import Dfp from 'node-google-dfp'
dfpUser = new Dfp.User(NETWORK_CODE, APP_NAME, VERSION)
Next, setup your client settings and your user's OAUTH token information.
dfpUser.setSettings({
client_id: "YOUR CLIENT ID",
client_secret: "YOUR CLIENT SECRET",
refresh_token: "A REFRESH TOKEN",
redirect_url: "YOUR OAUTH REDIRECT URL"
})
You can instance any of DFP's API Services: https://developers.google.com/doubleclick-publishers/docs/start
dfpUser.getService('LineItemService', function (err, lineItemService) {
if (err) {
return console.error(err)
}
const statement = new DfpClass.Statement('WHERE id = 103207340')
lineItemService.getLineItemsByStatement(statement, function (err, results) {
console.log(results)
})
})
Service accounts example
If you would like to use a Google Service Account to access DFP, you can do so by creating an instance of the JWT auth client.
import { JWT } from 'google-auth-library'
const jwtClient = new JWT(
SERVICE_ACCOUNT_EMAIL,
'path/to/key.pem',
null,
['https://www.googleapis.com/auth/dfp'],
)
dfpUser.setClient(jwtClient)
oAuth setup
This application requires a working oAuth refresh token to make requests. If you don't include a refresh token you will get an "No refresh token is set" error. If you include a bad token, you'll get an "illegal access" error. Service accounts are not supported.
To setup a refresh token manually, follow Google's instructions for using cURL. The main steps are included below:
Setup a oAuth "installed application" in the Google Developer Console.
Create a verification request using this installed application's client ID. (If you miss this step you'll get an
authorization_pending
error from Google on the next step.) Note that any slashes in adevice_code
will need to be escaped.curl -d "client_id={YOUR_OAUTH_CLIENT_ID}&scope=https://www.googleapis.com/auth/dfp" https://accounts.google.com/o/oauth2/device/code
{ "devicecode" : "ABCD-EFGH4/MEiMYvOO1THXLVfHGGN8obAgb5XFs1Uctj-QsyYsQk", "usercode" : "ABCD-EFGH", "verificationurl" : "https://www.google.com/device", "expires_in" : 1800, "interval" : 5 }
Visit the
verification_url
contained in the verification request response (e.g. https://www.google.com/device) in a browser and type in theuser_code
provided in the verification response. A verification code will be given as a response.Use the provided
code
and your client ID and secret from the Google oAuth application to create a refresh token.curl -d "client_id={YOUR_OAUTH_CLIENT_ID}&client_secret={YOUR_OAUTH_CLIENT_SECRET}&code={YOUR_VERIFICATION_CODE}&grant_type=http://oauth.net/grant_type/device/1.0" https://accounts.google.com/o/oauth2/token
{ "accesstoken" : "ya29.JAHynQpVpjBFhvg-7VKdQ7nmD0DkmCYoWTWo535TP8QsKa6j2rFOI1i0pdclFepvGZo9A2SrN41dA", "tokentype" : "Bearer", "expiresin" : 3600, "idtoken" : "eyJhbGciOiJSU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URL of non-public apps.
Known Issues
- OAuth Support for more than just Refresh Token
- No unit tests