2014-04-09 16 views
5

Tôi đang sử dụng máy chủ Node JS và cố gắng lấy mã thông báo truy cập từ Adwords API. Để thực hiện yêu cầu POST, tôi sử dụng curler từ npm (https://www.npmjs.org/package/curler). Dưới đây là các mẫu mã:Node js Adwords Api

var login = '[email protected]'; 
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); 
}); 

Câu trả lời tôi nhận được là

statusCode: 403 
bodyData: Error=BadAuthentication 

mà về cơ bản nói rằng thông tin đăng nhập là sai, mà không phải là. Không thể tìm ra liệu tôi đã chọn triển khai sai hay chỉ thiếu tiêu đề hay gì đó.

+0

Tôi cho rằng thông tin đăng nhập thông thường của bạn không đủ và bạn cần phải đăng ký mã thông báo API hoặc một số phương pháp xác thực khác. –

+0

Vấn đề là, cùng một phương thức hoạt động trong php thông qua curl, mà không sử dụng ứng dụng adwords hoặc khóa api của nhà phát triển –

+1

Bạn đã từng làm việc này chưa? Một ví dụ chức năng sẽ là tuyệt vời. – mz3

Trả lời

0

Đây là mã tôi sử dụng để nhận mã thông báo truy cập mới: https://github.com/ErikEvenson/googleads-node-lib/blob/v0.0.17/services/adWordsService.js#L206-L241. Mã thông báo truy cập kết thúc bằng 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; 
    } 
    };