add*_*ctd 5 javascript api curl node.js google-adwords
我正在使用Node JS服务器并尝试从Adwords API获取访问令牌.要执行POST请求,请使用npm(https://www.npmjs.org/package/curler)中的curler .这是代码示例:
var login = 'mymail@gmail.com';
var pass = 'mypassword';
var data = JSON.stringify({
Email: login,
Passwd : pass,
accountType: 'GOOGLE',
service: 'adwords',
source: 'adwordstest'
});
var options = {
method: "POST",
url: 'https://www.google.com/accounts/ClientLogin',
headers: {
'Content-Type': 'application/json'
},
data: data,
timeout: 5000,
connectionTimeout: 5000
};
var startDate = Date.now();
curl.request(options, function(err, res, bodyData) {
var duration = (Date.now() - startDate);
if (err) {
console.log(err);
}
else {
console.log('statusCode: %s', res.statusCode);
console.log('bodyData: %s', bodyData);
}
console.log("curler (libcurl) performed http request in %s ms. dnsTime: %s, connectTime: %s, preTransferTime: %s, startTransferTime: %s, totalTime: %s", duration, res.dnsTime, res.connectTime, res.preTransferTime, res.startTransferTime, res.totalTime);
});
Run Code Online (Sandbox Code Playgroud)
我得到的回应是
statusCode: 403
bodyData: Error=BadAuthentication
Run Code Online (Sandbox Code Playgroud)
这基本上说登录信息是错误的,但事实并非如此.无法弄清楚我是否选择了错误的实现,或者只是错过了标题或其他内容.
这是我用来获取新访问令牌的代码:https://github.com/ErikEvenson/googleads-node-lib/blob/v0.0.17/services/adWordsService.js#L206-L241。访问令牌最终以credentials.access_token.
self.refresh = function(done) {
// check if current credentials haven't expired
if (self.credentials && Date.now() < self.credentials.expires) {
// behave like an async
setTimeout(function() {done(null);}, 0);
return;
} else {
// throw away cached client
self.client = null;
var qs = {
refresh_token: self.options.ADWORDS_REFRESH_TOKEN,
client_id: self.options.ADWORDS_CLIENT_ID,
client_secret: self.options.ADWORDS_SECRET,
grant_type: 'refresh_token'
};
request.post(
{
qs: qs,
url: self.tokenUrl
},
function(error, response, body) {
self.credentials = JSON.parse(body);
self.credentials.issued = Date.now();
self.credentials.expires = self.credentials.issued -
self.credentials.expires_in;
done(error);
}
);
return;
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1067 次 |
| 最近记录: |