在为Node使用webmaster.searchanalytics api时获取backendError 500

时间:2017-10-27 14:35:45

标签: javascript node.js google-api

我在尝试使用webmaster.searchanalytics api时遇到以下错误:

{ Error: Backend Error
    at Request._callback (/home/tom/Programming/MagpieMicroService/node_modules/google-auth-library/lib/transporters.js:85:15)
    at Request.self.callback (/home/tom/Programming/MagpieMicroService/node_modules/request/request.js:186:22)
    at emitTwo (events.js:125:13)
    at Request.emit (events.js:213:7)
    at Request.<anonymous> (/home/tom/Programming/MagpieMicroService/node_modules/request/request.js:1163:10)
    at emitOne (events.js:115:13)
    at Request.emit (events.js:210:7)
    at IncomingMessage.<anonymous> (/home/tom/Programming/MagpieMicroService/node_modules/request/request.js:1085:12)
    at Object.onceWrapper (events.js:314:30)
    at emitNone (events.js:110:20)
  code: 500,
  errors: 
   [ { domain: 'global',
       reason: 'backendError',
       message: 'Backend Error' } ] }

代码如下:

'use strict';

var google = require('googleapis');
var fs = require('fs');
var key = require('./client_secret.json');
var prompt = require('prompt');

var tokens_file = './tokens/tokens.json';
var scopes = [
    'https://www.googleapis.com/auth/webmasters'
];

var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(
    key.installed.client_id,
    key.installed.client_secret,
    key.installed.redirect_uris[0],
);



fs.readFile(tokens_file, 'utf8', (err, data)=>{
    if (err) {
        console.log('No token file: ',err);
        var url = oauth2Client.generateAuthUrl({
            access_type: 'offline',
            scope: scopes,
        });
        console.log(
            'Head to the URL ', 
            url, 
            ' and paste the authorization code in:'
        );
        prompt.start();
        prompt.get(['authorizationCode'],(err, result)=>{
            if (err) {return console.log(err);}
            var code = result.authorizationCode;
            oauth2Client.getToken(code, (err, tokens)=>{
                if (err) {return console.log(err);}
                console.log(tokens)
                fs.writeFile(
                    tokens_file, 
                    JSON.stringify(tokens), 
                    (err)=>{
                        if (err) {return console.log('Error ', err);}
                        console.log('Tokens Saved')
                    }
                );
                oauth2Client.setCredentials({
                    access_token: tokens.access_token,
                    refresh_token: tokens.refresh_token
                });
                //Make the authorised call here
                const webmasters = google.webmasters('v3');
                let date = new Date();
                let startDate = new Date(date-7);
                let endDate = new Date(date-7);
                let params = {
                    'startDate': "2017-09-25",//startDate.toISOString(),
                    'endDate': "2017-09-26",//endDate.toISOString(),
                    'dimensions': ['query', 'device'],
                    'rowLimit': 5000,
                    'startRow': 0,
                    'siteUrl': encodeURIComponent('http://www.url.com/de/'),
                    'auth': oauth2Client,
                };
                console.log(params.siteUrl);
                console.log(params);
                webmasters.searchanalytics.query(params,function(err, data){
                    if (err) return console.log('There was an error: ', err);
                    console.log('This is the data: ', data);
                });
            });

        });
        return;
    };
    console.log('onfile')
    var tokens = JSON.parse(data);
    console.log('tokens: ', tokens);
    oauth2Client.setCredentials({
        access_token: tokens.access_token,
        refresh_token: tokens.refresh_token
    });
    console.log(oauth2Client)
    //Make the authorised call here
    // google.options({auth:oauth2Client});
    const webmasters = google.webmasters('v3');
    let date = new Date();
    let startDate = new Date(date-7);
    let endDate = new Date(date-7);
    let params = {
        'startDate': "2017-09-25",//startDate.toISOString(),
        'endDate': "2017-09-26",//endDate.toISOString(),
        'dimensions': ['query', 'device'],        
        'rowLimit': 5000,
        'startRow': 0,
        'siteUrl': encodeURIComponent('http://www.url.com/de/'),
        'auth': oauth2Client,
    };
    console.log(params.siteUrl);
    console.log(params);
    webmasters.searchanalytics.query(params,function(err, data){
        if (err) return console.log('There was an error: ', err);
        console.log('This is the data: ', data);
    });
});

我没有看到任何我做错的事。我在google auth api中有错误,所以认为可能就是这样。我使用节点5.0并且所有auth的东西似乎都正常工作并返回正确的密钥。验证后令牌保存,但oauth框架似乎不起作用。

0 个答案:

没有答案